Blob Blame History Raw
From 21fbcf665a7ad6b761e9342d116657d5c2353592 Mon Sep 17 00:00:00 2001
From: Frediano Ziglio <freddy77@gmail.com>
Date: Mon, 21 Sep 2020 07:00:39 +0100
Subject: [PATCH vd_agent_linux 06/17] vdagentd: Automatically release
 "agent_data"
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

It's not guaranteed that agent_disconnect will release the connection
so avoid to have a dandling pointer.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
Acked-by: Jakub Janků <jjanku@redhat.com>
---
 src/vdagentd/vdagentd.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/vdagentd/vdagentd.c b/src/vdagentd/vdagentd.c
index 051de74..94b8681 100644
--- a/src/vdagentd/vdagentd.c
+++ b/src/vdagentd/vdagentd.c
@@ -86,6 +86,13 @@ static uint32_t clipboard_serial[256];
 
 static GMainLoop *loop;
 
+static void agent_data_destroy(struct agent_data *agent_data)
+{
+    g_free(agent_data->session);
+    g_free(agent_data->screen_info);
+    g_free(agent_data);
+}
+
 static void vdagentd_quit(gint exit_code)
 {
     retval = exit_code;
@@ -930,7 +937,7 @@ static void agent_connect(UdscsConnection *conn)
             syslog(LOG_ERR, "Could not get peer PID, disconnecting new client: %s",
                             err->message);
             g_error_free(err);
-            g_free(agent_data);
+            agent_data_destroy(agent_data);
             udscs_server_destroy_connection(server, conn);
             return;
         }
@@ -938,7 +945,8 @@ static void agent_connect(UdscsConnection *conn)
         agent_data->session = session_info_session_for_pid(session_info, pid);
     }
 
-    g_object_set_data(G_OBJECT(conn), "agent_data", agent_data);
+    g_object_set_data_full(G_OBJECT(conn), "agent_data", agent_data,
+                           (GDestroyNotify) agent_data_destroy);
     udscs_write(conn, VDAGENTD_VERSION, 0, 0,
                 (uint8_t *)VERSION, strlen(VERSION) + 1);
     update_active_session_connection(conn);
@@ -951,13 +959,8 @@ static void agent_connect(UdscsConnection *conn)
 
 static void agent_disconnect(VDAgentConnection *conn, GError *err)
 {
-    struct agent_data *agent_data = g_object_get_data(G_OBJECT(conn), "agent_data");
-
     g_hash_table_foreach_remove(active_xfers, remove_active_xfers, conn);
 
-    g_clear_pointer(&agent_data->session, g_free);
-    g_free(agent_data->screen_info);
-    g_free(agent_data);
     if (err) {
         syslog(LOG_ERR, "%s", err->message);
         g_error_free(err);
-- 
2.26.2