[gnome-remote-desktop] clipboard-rdp: Also ensure that the mime type tables are freed



commit 5bb43cc172a0141a332d38335c1efd4e4ed8cadb
Author: Pascal Nowack <Pascal Nowack gmx de>
Date:   Fri Aug 6 09:57:55 2021 +0200

    clipboard-rdp: Also ensure that the mime type tables are freed
    
    Commit 982b4db3cc06d86138cb49322d55ebc990671410 fixes a memory leak,
    that can happen, if there is still a list of pending mime type tables,
    when the session ends.
    While the commit ensures that the update context is freed, it does not
    clear the list of pending mime type tables itself.
    
    Do this now in this commit. Use however here g_idle_add_full(), instead
    of g_idle_add(), as that function allows the caller to pass a destroy
    function, which is called, when the GSource is removed.
    When the source function is called, steal the mime type tables, to
    ensure that the pointer is NULL to avoid a double free.
    
    Fixes 982b4db3cc06d86138cb49322d55ebc990671410

 src/grd-clipboard-rdp.c | 33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)
---
diff --git a/src/grd-clipboard-rdp.c b/src/grd-clipboard-rdp.c
index 040c632..95f3418 100644
--- a/src/grd-clipboard-rdp.c
+++ b/src/grd-clipboard-rdp.c
@@ -32,6 +32,7 @@
 
 typedef struct _ServerFormatListUpdateContext
 {
+  GrdClipboardRdp *clipboard_rdp;
   GList *mime_type_tables;
 } ServerFormatListUpdateContext;
 
@@ -82,8 +83,6 @@ struct _GrdClipboardRdp
   gboolean server_file_contents_requests_allowed;
   uint16_t format_list_response_msg_flags;
 
-  ServerFormatListUpdateContext *format_list_update_context;
-
   GHashTable *serial_entry_table;
   GHashTable *clip_data_table;
   struct
@@ -1089,19 +1088,17 @@ cliprdr_temp_directory (CliprdrServerContext         *cliprdr_context,
 static gboolean
 update_server_format_list (gpointer user_data)
 {
-  GrdClipboardRdp *clipboard_rdp = user_data;
-  GrdClipboard *clipboard = GRD_CLIPBOARD (clipboard_rdp);
+  ServerFormatListUpdateContext *update_context = user_data;
+  GrdClipboardRdp *clipboard_rdp = update_context->clipboard_rdp;
+  GrdClipboard *clipboard = GRD_CLIPBOARD (update_context->clipboard_rdp);
   GrdRdpFuseClipboard *rdp_fuse_clipboard = clipboard_rdp->rdp_fuse_clipboard;
   CliprdrServerContext *cliprdr_context = clipboard_rdp->cliprdr_context;
-  ServerFormatListUpdateContext *update_context;
   CLIPRDR_FORMAT_LIST_RESPONSE format_list_response = {0};
   GList *mime_type_tables;
   GrdMimeTypeTable *mime_type_table;
   GList *l;
 
-  update_context = g_steal_pointer (&clipboard_rdp->format_list_update_context);
-  mime_type_tables = update_context->mime_type_tables;
-
+  mime_type_tables = g_steal_pointer (&update_context->mime_type_tables);
   for (l = mime_type_tables; l; l = l->next)
     {
       mime_type_table = l->data;
@@ -1137,8 +1134,6 @@ update_server_format_list (gpointer user_data)
   if (!cliprdr_context->canLockClipData)
     grd_rdp_fuse_clipboard_dismiss_all_no_cdi_requests (rdp_fuse_clipboard);
 
-  g_free (update_context);
-
   WaitForSingleObject (clipboard_rdp->format_list_received_event, INFINITE);
   clipboard_rdp->server_format_list_update_id = 0;
   ResetEvent (clipboard_rdp->format_list_received_event);
@@ -1147,6 +1142,16 @@ update_server_format_list (gpointer user_data)
   return G_SOURCE_REMOVE;
 }
 
+static void
+update_context_free (gpointer data)
+{
+  ServerFormatListUpdateContext *update_context = data;
+
+  g_clear_list (&update_context->mime_type_tables, g_free);
+
+  g_free (update_context);
+}
+
 /**
  * Client notifies us that its clipboard is updated with new clipboard data
  */
@@ -1303,13 +1308,12 @@ cliprdr_client_format_list (CliprdrServerContext      *cliprdr_context,
 
   ResetEvent (clipboard_rdp->completed_format_list_event);
   update_context = g_malloc0 (sizeof (ServerFormatListUpdateContext));
+  update_context->clipboard_rdp = clipboard_rdp;
   update_context->mime_type_tables = mime_type_tables;
 
-  g_assert (!clipboard_rdp->format_list_update_context);
-  clipboard_rdp->format_list_update_context = update_context;
-
   clipboard_rdp->server_format_list_update_id =
-    g_idle_add (update_server_format_list, clipboard_rdp);
+    g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, update_server_format_list,
+                     update_context, update_context_free);
   SetEvent (clipboard_rdp->format_list_received_event);
 
   return CHANNEL_RC_OK;
@@ -2044,7 +2048,6 @@ grd_clipboard_rdp_dispose (GObject *object)
     g_clear_pointer (&clipboard_rdp->clipboard_retrieval_context.entry, g_free);
 
   g_clear_pointer (&clipboard_rdp->format_data_request_context, g_free);
-  g_clear_pointer (&clipboard_rdp->format_list_update_context, g_free);
   g_clear_pointer (&clipboard_rdp->queued_server_formats, g_list_free);
   g_clear_pointer (&clipboard_rdp->pending_server_formats, g_list_free);
   g_hash_table_foreach_remove (clipboard_rdp->format_data_cache,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]