gdbus, p2p-gdbus: fix a memory leak 70/270770/1
authorAdam Michalski <a.michalski2@partner.samsung.com>
Tue, 8 Feb 2022 15:01:35 +0000 (16:01 +0100)
committerAdam Michalski <a.michalski2@partner.samsung.com>
Tue, 8 Feb 2022 15:01:35 +0000 (16:01 +0100)
Upon encountering an 's' in a format string, `g_variant_get()` takes
a pointer to a (gchar *) (i.e., (gchar **)) and sets it to
a newly-allocated copy of the string. It's the user's responsibility
to free up memory by using `g_free()`.

Change-Id: Ieec7ca25af592dfcdcca34dbe288ea347b80b102

benchmark/gdbus.c
benchmark/p2p-gdbus.c

index 75dc701a3885dc74a02f458e1194dc101d5b3c9d..ca334b5ff82482b1703122a01274b7905c7091df 100644 (file)
@@ -105,8 +105,10 @@ static void handle_method_call (GDBusConnection       *connection,
 
                if(++cnt == state->iters) {cnt = 0; g_main_loop_quit (state->main_loop); }
 
-               if(state->buf[0] != 'r')
+               if(state->buf[0] != 'r') {
+                       g_free(state->buf);     /* free memory allocated by g_variant_get */
                        return;
+               }
 
                if (state->is_lt) {
                        if (raw_data_on)
@@ -122,6 +124,8 @@ static void handle_method_call (GDBusConnection       *connection,
                                        min = lt;
                        }
                }
+
+               g_free(state->buf);     /* free memory allocated by g_variant_get */
        }
 }
 
index 01d28edc56c4f29a3269ec1ebc4ddb366bcc019e..661092e1896de74df98c866d02ce0df0ea5cc18f 100644 (file)
@@ -91,8 +91,10 @@ static void handle_method_call(GDBusConnection *connection,
                g_main_loop_quit(state->main_loop);
        }
 
-       if (state->buf[0] != 'R')       /* skip warm-up tries */
+       if (state->buf[0] != 'R') {     /* skip warm-up tries */
+               g_free(state->buf);     /* free memory allocated by g_variant_get */
                return;
+       }
 
        if (state->is_lt) {
                if (state->verbose)
@@ -121,6 +123,8 @@ static void handle_method_call(GDBusConnection *connection,
                else
                        g_print("Client said: %s %s\n", state->buf, timestamp);
        }
+
+       g_free(state->buf);     /* free memory allocated by g_variant_get */
 }
 
 static const GDBusInterfaceVTable interface_vtable = {