Skip to content

Commit

Permalink
portals: Work with owned, non-floating GVariants where possible
Browse files Browse the repository at this point in the history
By g_variant_ref_sink'ing floating GVariants as early as possible. This
fixes a few cases where the variant would be leaked, where the floating
ref would be owned by a g_autoptr and makes it easier to see who owns
the reference.

Cases where the floating ref is passed into a function which immediately
sinks the floating ref are left untouched.
  • Loading branch information
swick committed Oct 4, 2024
1 parent 2c9a981 commit 543178d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ handle_set_selection (XdpDbusClipboard *object,
Call *call = call_from_invocation (invocation);
Session *session;
GVariantBuilder options_builder;
GVariant *options;
g_autoptr(GVariant) options = NULL;
g_autoptr(GError) error = NULL;

session = acquire_session_from_call (arg_session_handle, call);
Expand Down Expand Up @@ -158,7 +158,7 @@ handle_set_selection (XdpDbusClipboard *object,
g_dbus_method_invocation_return_gerror (invocation, error);
return G_DBUS_METHOD_INVOCATION_HANDLED;
}
options = g_variant_builder_end (&options_builder);
options = g_variant_ref_sink (g_variant_builder_end (&options_builder));

xdp_dbus_impl_clipboard_call_set_selection (
impl, arg_session_handle, options, NULL, NULL, NULL);
Expand Down
13 changes: 8 additions & 5 deletions src/dynamic-launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ set_launcher_data_for_token (const char *token,
{
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&transient_permissions_lock);
guint timeout_id;
GVariant *launcher_data_wrapped;
g_autoptr(GVariant) launcher_data_wrapped = NULL;

if (!transient_permissions)
{
Expand All @@ -466,11 +466,12 @@ set_launcher_data_for_token (const char *token,
*/
timeout_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, 300, install_token_timeout,
g_strdup (token), g_free);
launcher_data_wrapped = g_variant_new ("(vu)", launcher_data, timeout_id);
launcher_data_wrapped =
g_variant_ref_sink (g_variant_new ("(vu)", launcher_data, timeout_id));

g_hash_table_insert (transient_permissions,
g_strdup (token),
g_variant_ref_sink (launcher_data_wrapped));
g_steal_pointer (&launcher_data_wrapped));
}

static void
Expand All @@ -479,7 +480,6 @@ prepare_install_done (GObject *source,
gpointer data)
{
g_autoptr(Request) request = data;
GVariant *launcher_data;
guint response = 2;
g_autoptr(GVariant) results = NULL;
g_autoptr(GError) error = NULL;
Expand Down Expand Up @@ -526,6 +526,8 @@ prepare_install_done (GObject *source,
}
else
{
GVariant *launcher_data;

/* Save the token in memory and return it to the caller */
launcher_data = g_variant_new ("(svss)", chosen_name, chosen_icon, icon_format, icon_size);
set_launcher_data_for_token (token, launcher_data);
Expand Down Expand Up @@ -688,7 +690,6 @@ handle_request_install_token (XdpDbusDynamicLauncher *object,
Call *call = call_from_invocation (invocation);
const char *app_id = xdp_app_info_get_id (call->app_info);
g_autoptr(GError) error = NULL;
GVariant *launcher_data;
g_autofree char *token = NULL;
g_autofree char *icon_format = NULL;
g_autofree char *icon_size = NULL;
Expand Down Expand Up @@ -718,6 +719,8 @@ handle_request_install_token (XdpDbusDynamicLauncher *object,

if (response == 0)
{
GVariant *launcher_data;

/* Do some validation on the icon before saving it */
if (!validate_serialized_icon (arg_icon_v, &icon_format, &icon_size))
{
Expand Down
8 changes: 4 additions & 4 deletions src/global-shortcuts.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ handle_create_session (XdpDbusGlobalShortcuts *object,
return G_DBUS_METHOD_INVOCATION_HANDLED;
}

options = g_variant_builder_end (&options_builder);
options = g_variant_ref_sink (g_variant_builder_end (&options_builder));
impl_request =
xdp_dbus_impl_request_proxy_new_sync (g_dbus_proxy_get_connection (G_DBUS_PROXY (impl)),
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
Expand Down Expand Up @@ -392,7 +392,7 @@ handle_bind_shortcuts (XdpDbusGlobalShortcuts *object,
return G_DBUS_METHOD_INVOCATION_HANDLED;
}

options = g_variant_builder_end (&options_builder);
options = g_variant_ref_sink (g_variant_builder_end (&options_builder));

g_variant_builder_init (&shortcuts_builder, G_VARIANT_TYPE_ARRAY);
if (!xdp_verify_shortcuts (arg_shortcuts, &shortcuts_builder,
Expand All @@ -402,7 +402,7 @@ handle_bind_shortcuts (XdpDbusGlobalShortcuts *object,
g_dbus_method_invocation_return_gerror (invocation, error);
return G_DBUS_METHOD_INVOCATION_HANDLED;
}
shortcuts = g_variant_builder_end (&shortcuts_builder);
shortcuts = g_variant_ref_sink (g_variant_builder_end (&shortcuts_builder));

session = acquire_session (arg_session_handle, request);
if (!session)
Expand Down Expand Up @@ -519,7 +519,7 @@ handle_list_shortcuts (XdpDbusGlobalShortcuts *object,
return G_DBUS_METHOD_INVOCATION_HANDLED;
}

options = g_variant_builder_end (&options_builder);
options = g_variant_ref_sink (g_variant_builder_end (&options_builder));

session = acquire_session (arg_session_handle, request);
if (!session)
Expand Down
18 changes: 9 additions & 9 deletions src/input-capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ handle_create_session (XdpDbusInputCapture *object,
g_autoptr(XdpDbusImplRequest) impl_request = NULL;
g_autoptr(GError) error = NULL;
GVariantBuilder options_builder;
GVariant *options;
g_autoptr(GVariant) options = NULL;
Session *session;

REQUEST_AUTOLOCK (request);
Expand Down Expand Up @@ -271,7 +271,7 @@ handle_create_session (XdpDbusInputCapture *object,
g_dbus_method_invocation_return_gerror (invocation, error);
return G_DBUS_METHOD_INVOCATION_HANDLED;
}
options = g_variant_builder_end (&options_builder);
options = g_variant_ref_sink (g_variant_builder_end (&options_builder));

g_object_set_qdata_full (G_OBJECT (request),
quark_request_session,
Expand Down Expand Up @@ -327,12 +327,12 @@ get_zones_done (GObject *source_object,

if (request->exported)
{
if (response != 0)
if (!results)
{
GVariantBuilder results_builder;

g_variant_builder_init (&results_builder, G_VARIANT_TYPE_VARDICT);
results = g_variant_builder_end (&results_builder);
results = g_variant_ref_sink (g_variant_builder_end (&results_builder));
}

xdp_dbus_request_emit_response (XDP_DBUS_REQUEST (request), response, results);
Expand All @@ -359,7 +359,7 @@ handle_get_zones (XdpDbusInputCapture *object,
InputCaptureSession *input_capture_session;
g_autoptr(GError) error = NULL;
GVariantBuilder options_builder;
GVariant *options;
g_autoptr(GVariant) options = NULL;
Session *session;

REQUEST_AUTOLOCK (request);
Expand Down Expand Up @@ -427,7 +427,7 @@ handle_get_zones (XdpDbusInputCapture *object,
return G_DBUS_METHOD_INVOCATION_HANDLED;
}

options = g_variant_builder_end (&options_builder);
options = g_variant_ref_sink (g_variant_builder_end (&options_builder));

g_object_set_qdata_full (G_OBJECT (request),
quark_request_session,
Expand Down Expand Up @@ -488,7 +488,7 @@ set_pointer_barriers_done (GObject *source_object,
GVariantBuilder results_builder;

g_variant_builder_init (&results_builder, G_VARIANT_TYPE_VARDICT);
results = g_variant_builder_end (&results_builder);
results = g_variant_ref_sink (g_variant_builder_end (&results_builder));
}

xdp_dbus_request_emit_response (XDP_DBUS_REQUEST (request), response, results);
Expand All @@ -515,7 +515,7 @@ handle_set_pointer_barriers (XdpDbusInputCapture *object,
InputCaptureSession *input_capture_session;
g_autoptr(GError) error = NULL;
GVariantBuilder options_builder;
GVariant *options;
g_autoptr(GVariant) options = NULL;
Session *session;

REQUEST_AUTOLOCK (request);
Expand Down Expand Up @@ -583,7 +583,7 @@ handle_set_pointer_barriers (XdpDbusInputCapture *object,
return G_DBUS_METHOD_INVOCATION_HANDLED;
}

options = g_variant_builder_end (&options_builder);
options = g_variant_ref_sink (g_variant_builder_end (&options_builder));

g_object_set_qdata_full (G_OBJECT (request),
quark_request_session,
Expand Down
Loading

0 comments on commit 543178d

Please sign in to comment.