Skip to content

Commit

Permalink
Always close fd returned from g_unix_fd_list_get
Browse files Browse the repository at this point in the history
Mostly by assining to an owning g_autofd variable. It also makes sure
the variables get cleared to -1 where applicable.

This also includes a change to take a lock before accessing the data
protected by the lock.
  • Loading branch information
swick authored and jadahl committed Nov 14, 2024
1 parent 41ca139 commit bf56e85
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/email.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ handle_compose_email (XdpDbusEmail *object,
{
g_autofree char *path = NULL;
int fd_id;
int fd;
g_autofd int fd = -1;

g_variant_get_child (attachment_fds, i, "h", &fd_id);
if (fd_id >= g_unix_fd_list_get_length (fd_list))
Expand Down
9 changes: 4 additions & 5 deletions src/open-uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,15 @@ handle_open_in_thread_func (GTask *task,
g_auto(GVariantBuilder) opts_builder =
G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT);
gboolean skip_app_chooser = FALSE;
g_auto(XdpFd) fd = -1;
g_autofd int fd = -1;
gboolean writable = FALSE;
gboolean ask = FALSE;
gboolean open_dir = FALSE;
gboolean use_default_app = FALSE;
const char *reason;

REQUEST_AUTOLOCK (request);

parent_window = (const char *)g_object_get_data (G_OBJECT (request), "parent-window");
uri = g_strdup ((const char *)g_object_get_data (G_OBJECT (request), "uri"));
fd = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (request), "fd"));
Expand All @@ -625,7 +627,7 @@ handle_open_in_thread_func (GTask *task,
open_dir = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (request), "open-dir"));
activation_token = (const char *)g_object_get_data (G_OBJECT (request), "activation-token");

REQUEST_AUTOLOCK (request);
g_object_set_data (G_OBJECT (request), "fd", GINT_TO_POINTER (-1));

/* Verify that either uri or fd is set, not both */
if (uri != NULL && fd != -1)
Expand Down Expand Up @@ -779,9 +781,6 @@ handle_open_in_thread_func (GTask *task,
scheme = g_strdup ("file");
uri = g_filename_to_uri (path, NULL, NULL);
g_object_set_data_full (G_OBJECT (request), "uri", g_strdup (uri), g_free);
close (fd);
fd = -1;
g_object_set_data (G_OBJECT (request), "fd", GINT_TO_POINTER (-1));
}

g_object_set_data_full (G_OBJECT (request), "scheme", g_strdup (scheme), g_free);
Expand Down
3 changes: 2 additions & 1 deletion src/trash.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ handle_trash_file (XdpDbusTrash *object,
GVariant *arg_fd)
{
Call *call = call_from_invocation (invocation);
int idx, fd;
int idx;
g_autofd int fd = -1;
guint result;

g_debug ("Handling TrashFile");
Expand Down
6 changes: 3 additions & 3 deletions src/wallpaper.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ handle_set_wallpaper_in_thread_func (GTask *task,
g_autoptr(XdpDbusImplRequest) impl_request = NULL;
GVariant *options;
gboolean show_preview = FALSE;
int fd;
g_autofd int fd = -1;
Permission permission;

REQUEST_AUTOLOCK (request);
Expand All @@ -145,6 +145,8 @@ handle_set_wallpaper_in_thread_func (GTask *task,
fd = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (request), "fd"));
options = ((GVariant *)g_object_get_data (G_OBJECT (request), "options"));

g_object_set_data (G_OBJECT (request), "fd", GINT_TO_POINTER (-1));

if (uri != NULL && fd != -1)
{
g_warning ("Rejecting invalid set-wallpaper request (both URI and fd are set)");
Expand Down Expand Up @@ -267,8 +269,6 @@ handle_set_wallpaper_in_thread_func (GTask *task,

uri = g_filename_to_uri (path, NULL, NULL);
g_object_set_data_full (G_OBJECT (request), "uri", g_strdup (uri), g_free);
close (fd);
g_object_set_data (G_OBJECT (request), "fd", GINT_TO_POINTER (-1));
}

impl_request = xdp_dbus_impl_request_proxy_new_sync (g_dbus_proxy_get_connection (G_DBUS_PROXY (impl)),
Expand Down

0 comments on commit bf56e85

Please sign in to comment.