From 23738a36f6491773a1a8c99ff80a977ccc2903e4 Mon Sep 17 00:00:00 2001 From: Adam Michalski Date: Tue, 8 Feb 2022 16:01:35 +0100 Subject: [PATCH] gdbus, p2p-gdbus: fix a memory leak 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 | 6 +++++- benchmark/p2p-gdbus.c | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/benchmark/gdbus.c b/benchmark/gdbus.c index 75dc701..ca334b5 100644 --- a/benchmark/gdbus.c +++ b/benchmark/gdbus.c @@ -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 */ } } diff --git a/benchmark/p2p-gdbus.c b/benchmark/p2p-gdbus.c index 01d28ed..661092e 100644 --- a/benchmark/p2p-gdbus.c +++ b/benchmark/p2p-gdbus.c @@ -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 = { -- 2.34.1