Repository: libsdl-org/SDL
File: src/dialog/unix/SDL_portaldialog.c (C)

Code before:
        if (save_file_existing && location_name) {
            /* Open a save dialog at an existing file */
            DBus_AppendByteArray(dbus, &options, "current_file", default_location);
            /* Setting "current_name" should not be necessary however the kde-desktop-portal sets the filename without an extension.
             * An alternative would be to match the extension to a filter and set "current_filter".
             */
            DBus_AppendStringOption(dbus, &options, "current_name", location_name);
        } else if (save_file_new_named && location_folder && location_name) {
            /* Open a save dialog at a location with a suggested name */
            DBus_AppendByteArray(dbus, &options, "current_folder", location_folder);
            DBus_AppendStringOption(dbus, &options, "current_name", location_name);
        } else {
            DBus_AppendByteArray(dbus, &options, "current_folder", default_location);
        }
    }
    if (accept) {
        DBus_AppendStringOption(dbus, &options, "accept_label", accept);
    }
    dbus->message_iter_close_container(&params, &options);

    DBusMessage *reply = dbus->connection_send_with_reply_and_block(dbus->session_conn, msg, DBUS_TIMEOUT_INFINITE, &error);
    if (dbus->error_is_set(&error)) {
        SDL_SetError("Failed to open dialog via DBus, %s: %s", error.name, error.message);
        dbus->error_free(&error);
        goto cleanup;
    }

    if (reply) {
        DBusMessageIter reply_iter;
        dbus->message_iter_init(reply, &reply_iter);

        if (dbus->message_iter_get_arg_type(&reply_iter) == DBUS_TYPE_OBJECT_PATH) {
            dbus->message_iter_get_basic(&reply_iter, &signal_id);
        }
    }

    if (!signal_id) {
        SDL_SetError("Invalid response received by DBus");
        callback(userdata, NULL, -1);
        goto incorrect_type;
    }

    dbus->message_unref(msg);

    filter_len = SDL_strlen(SIGNAL_FILTER) + SDL_strlen(signal_id) + 2;
    filter = SDL_malloc(sizeof(char) * filter_len);
    if (!filter) {
        callback(userdata, NULL, -1);
        goto incorrect_type;
    }


Review comment (suggestion):
The callback should be always be called before this function returns. See above for how error handling is done.

Code after:
        if (save_file_existing && location_name) {
            /* Open a save dialog at an existing file */
            DBus_AppendByteArray(dbus, &options, "current_file", default_location);
            /* Setting "current_name" should not be necessary however the kde-desktop-portal sets the filename without an extension.
             * An alternative would be to match the extension to a filter and set "current_filter".
             */
            DBus_AppendStringOption(dbus, &options, "current_name", location_name);
        } else if (save_file_new_named && location_folder && location_name) {
            /* Open a save dialog at a location with a suggested name */
            DBus_AppendByteArray(dbus, &options, "current_folder", location_folder);
            DBus_AppendStringOption(dbus, &options, "current_name", location_name);
        } else {
            DBus_AppendByteArray(dbus, &options, "current_folder", default_location);
        }
    }
    if (accept) {
        DBus_AppendStringOption(dbus, &options, "accept_label", accept);
    }
    dbus->message_iter_close_container(&params, &options);

    DBusMessage *reply = dbus->connection_send_with_reply_and_block(dbus->session_conn, msg, DBUS_TIMEOUT_INFINITE, &error);
    if (dbus->error_is_set(&error)) {
        SDL_SetError("Failed to open dialog via DBus, %s: %s", error.name, error.message);
        dbus->error_free(&error);
        callback(userdata, NULL, -1);
        goto cleanup;
    }

    if (reply) {
        DBusMessageIter reply_iter;
        dbus->message_iter_init(reply, &reply_iter);

        if (dbus->message_iter_get_arg_type(&reply_iter) == DBUS_TYPE_OBJECT_PATH) {
            dbus->message_iter_get_basic(&reply_iter, &signal_id);
        }
    }

    if (!signal_id) {
        SDL_SetError("Invalid response received by DBus");
        callback(userdata, NULL, -1);
        goto incorrect_type;
    }

    dbus->message_unref(msg);

    filter_len = SDL_strlen(SIGNAL_FILTER) + SDL_strlen(signal_id) + 2;
    filter = SDL_malloc(sizeof(char) * filter_len);
    if (!filter) {
        callback(userdata, NULL, -1);
        goto incorrect_type;
    }