Repository: libsdl-org/SDL
File: src/core/linux/SDL_dbus.c (C)

Code before:
            }

            msg = dbus.message_new_method_call(bus_name, path, interface, "Inhibit");
            if (!msg) {
                return false;
            }

            if (!dbus.message_append_args(msg, DBUS_TYPE_STRING, &window, DBUS_TYPE_UINT32, &INHIBIT_IDLE, DBUS_TYPE_INVALID)) {
                dbus.message_unref(msg);
                return false;
            }

            dbus.message_iter_init_append(msg, &iterInit);

            // a{sv}
            if (!SDL_DBus_AppendDictWithKeyValue(&iterInit, key, reason)) {
                dbus.message_unref(msg);
                return false;
            }

            DBusMessage *reply = NULL;
            if (SDL_DBus_CallWithBasicReply(dbus.session_conn, msg, &reply, DBUS_TYPE_OBJECT_PATH, &reply_path)) {
                inhibit_handle = SDL_strdup(reply_path);
                result = true;
            }
            dbus.message_unref(reply);
            dbus.message_unref(msg);
            return result;
        } else {
            if (!SDL_DBus_CallVoidMethod(bus_name, inhibit_handle, "org.freedesktop.portal.Request", "Close", DBUS_TYPE_INVALID)) {
                return false;
            }
            SDL_free(inhibit_handle);
            inhibit_handle = NULL;
        }
    } else {
        const char *bus_name = "org.freedesktop.ScreenSaver";
        const char *path = "/org/freedesktop/ScreenSaver";
        const char *interface = "org.freedesktop.ScreenSaver";

        if (inhibit) {
            const char *app = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING);
            const char *reason = SDL_GetHint(SDL_HINT_SCREENSAVER_INHIBIT_ACTIVITY_NAME);
            if (!reason || !reason[0]) {
                reason = default_inhibit_reason;
            }

            if (!SDL_DBus_CallMethod(bus_name, path, interface, "Inhibit",
                                     DBUS_TYPE_STRING, &app, DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID,
                                     DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
                return false;

Review comment (refactor):
```suggestion
            if (reply) {
                dbus.message_unref(reply);
            }
```

or move it into the "call succeeded" block. It's considered invalid to unref NULL.

I personally prefer having the cleanup path be reached unconditionally, so that you don't have to keep thinking about whether the pointer has been set. In recent libdbus we have an inline helper `dbus_clear_message(DBusMessage **)` which you use like this:

```
DBusMessage *reply = NULL;

... do something which might set reply non-NULL, or not ...

dbus_clear_message(&reply);
/* now reply == NULL */
```

You probably can't use that in SDL because of how you dlopen libdbus, but you could do something like:

```
static void SDL_DBusClearMessage(DBusMessage **message_p)
{
    DBusMessage *message = *message_p;
    *message_p = NULL;
    if (message)
        dbus.message_unref(message);
}
```

Code after:
            const char *reason = SDL_GetHint(SDL_HINT_SCREENSAVER_INHIBIT_ACTIVITY_NAME);
            if (!reason || !reason[0]) {
                reason = default_inhibit_reason;
            }

            msg = dbus.message_new_method_call(bus_name, path, interface, "Inhibit");
            if (!msg) {
                return false;
            }

            if (!dbus.message_append_args(msg, DBUS_TYPE_STRING, &window, DBUS_TYPE_UINT32, &INHIBIT_IDLE, DBUS_TYPE_INVALID)) {
                dbus.message_unref(msg);
                return false;
            }

            dbus.message_iter_init_append(msg, &iterInit);

            // a{sv}
            if (!SDL_DBus_AppendDictWithKeyValue(&iterInit, key, reason)) {
                dbus.message_unref(msg);
                return false;
            }

            DBusMessage *reply = NULL;
            if (SDL_DBus_CallWithBasicReply(dbus.session_conn, &reply, msg, DBUS_TYPE_OBJECT_PATH, &reply_path)) {
                inhibit_handle = SDL_strdup(reply_path);
                result = true;
            }
            SDL_DBus_FreeReply(&reply);
            dbus.message_unref(msg);
            return result;
        } else {
            if (!SDL_DBus_CallVoidMethod(bus_name, inhibit_handle, "org.freedesktop.portal.Request", "Close", DBUS_TYPE_INVALID)) {
                return false;
            }
            SDL_free(inhibit_handle);
            inhibit_handle = NULL;
        }
    } else {
        const char *bus_name = "org.freedesktop.ScreenSaver";
        const char *path = "/org/freedesktop/ScreenSaver";
        const char *interface = "org.freedesktop.ScreenSaver";

        if (inhibit) {
            const char *app = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING);
            const char *reason = SDL_GetHint(SDL_HINT_SCREENSAVER_INHIBIT_ACTIVITY_NAME);
            if (!reason || !reason[0]) {
                reason = default_inhibit_reason;
            }

            if (!SDL_DBus_CallMethod(NULL, bus_name, path, interface, "Inhibit",