From 46f1427bd13357f5fa14dd155fed23ba77771aa1 Mon Sep 17 00:00:00 2001 From: Pieter Duchi Date: Wed, 21 Oct 2026 21:41:52 +0200 Subject: [PATCH 1/3] gtk.cfg: g_idle_add and g_timeout_add take the data pointer The data given to g_idle_add(), g_timeout_add() and their _seconds/_full variants is passed to the callback (and the destroy notify), which usually frees it. With every such call was reported as Memory leak: data Configure them as instead. --- cfg/gtk.cfg | 10 ++++++---- test/cfg/gtk.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/cfg/gtk.cfg b/cfg/gtk.cfg index 024f567c922..2c02c567f30 100644 --- a/cfg/gtk.cfg +++ b/cfg/gtk.cfg @@ -717,6 +717,12 @@ g_variant_type_dup_string g_value_dup_string g_register_data + g_idle_add + g_idle_add_full + g_timeout_add + g_timeout_add_full + g_timeout_add_seconds + g_timeout_add_seconds_full g_free @@ -4237,7 +4243,6 @@ - false @@ -4249,7 +4254,6 @@ - false @@ -5652,7 +5656,6 @@ - false @@ -5669,7 +5672,6 @@ - false diff --git a/test/cfg/gtk.c b/test/cfg/gtk.c index 2612b572638..1a19e6c9929 100644 --- a/test/cfg/gtk.c +++ b/test/cfg/gtk.c @@ -730,3 +730,45 @@ int g_define_enum_and_flags_type_test(void) { return e + f; } #endif + +static gboolean source_func_frees_data(gpointer data) +{ + g_free(data); + return FALSE; +} + +static void destroy_notify_frees_data(gpointer data) +{ + g_free(data); +} + +// The data passed to a GSource is owned by the callback or the destroy notify +// from then on, so it does not leak. +void g_idle_add_takes_data_test(void) +{ + // cppcheck-suppress constVariablePointer + gchar *data = g_strdup("data"); + g_idle_add(source_func_frees_data, data); + + gchar *data_full = g_strdup("data"); + g_idle_add_full(200, source_func_frees_data, data_full, NULL); +} + +void g_timeout_add_takes_data_test(void) +{ + // cppcheck-suppress constVariablePointer + gchar *data = g_strdup("data"); + g_timeout_add(10, source_func_frees_data, data); + + // cppcheck-suppress constVariablePointer + gchar *data_seconds = g_strdup("data"); + g_timeout_add_seconds(1, source_func_frees_data, data_seconds); + + // cppcheck-suppress constVariablePointer + gchar *data_full = g_strdup("data"); + g_timeout_add_full(0, 10, source_func_frees_data, data_full, destroy_notify_frees_data); + + // cppcheck-suppress constVariablePointer + gchar *data_seconds_full = g_strdup("data"); + g_timeout_add_seconds_full(0, 1, source_func_frees_data, data_seconds_full, destroy_notify_frees_data); +} From fb46279d450185fd5ed95d2a0ebd7af222905fa1 Mon Sep 17 00:00:00 2001 From: Pieter Duchi Date: Fri, 13 Nov 2026 10:06:52 +0100 Subject: [PATCH 2/3] gtk.cfg: the data of g_idle_add and g_timeout_add is not const direction="in" on the gpointer data argument made cppcheck suggest a pointer to const (constVariablePointer) for the data, although the function hands it to the callback and takes a non-const gpointer. g_idle_add_full already had no direction on it. --- cfg/gtk.cfg | 6 +++--- test/cfg/gtk.c | 5 ----- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/cfg/gtk.cfg b/cfg/gtk.cfg index 2c02c567f30..c7cd4793ad2 100644 --- a/cfg/gtk.cfg +++ b/cfg/gtk.cfg @@ -4248,7 +4248,7 @@ - + @@ -5666,7 +5666,7 @@ - + @@ -5686,7 +5686,7 @@ - + diff --git a/test/cfg/gtk.c b/test/cfg/gtk.c index 1a19e6c9929..a2a32b814a8 100644 --- a/test/cfg/gtk.c +++ b/test/cfg/gtk.c @@ -746,7 +746,6 @@ static void destroy_notify_frees_data(gpointer data) // from then on, so it does not leak. void g_idle_add_takes_data_test(void) { - // cppcheck-suppress constVariablePointer gchar *data = g_strdup("data"); g_idle_add(source_func_frees_data, data); @@ -756,19 +755,15 @@ void g_idle_add_takes_data_test(void) void g_timeout_add_takes_data_test(void) { - // cppcheck-suppress constVariablePointer gchar *data = g_strdup("data"); g_timeout_add(10, source_func_frees_data, data); - // cppcheck-suppress constVariablePointer gchar *data_seconds = g_strdup("data"); g_timeout_add_seconds(1, source_func_frees_data, data_seconds); - // cppcheck-suppress constVariablePointer gchar *data_full = g_strdup("data"); g_timeout_add_full(0, 10, source_func_frees_data, data_full, destroy_notify_frees_data); - // cppcheck-suppress constVariablePointer gchar *data_seconds_full = g_strdup("data"); g_timeout_add_seconds_full(0, 1, source_func_frees_data, data_seconds_full, destroy_notify_frees_data); } From c10a655a1e93b203511d5a48973e63543250a2b5 Mon Sep 17 00:00:00 2001 From: Pieter Duchi Date: Fri, 13 Nov 2026 10:06:52 +0100 Subject: [PATCH 3/3] test/cfg/gtk.c: g_idle_add of freed data is not reported A function does not get the deallocuse check that a function gets, so passing freed data to g_idle_add() is no longer reported. Record it as a TODO suppression. --- test/cfg/gtk.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/cfg/gtk.c b/test/cfg/gtk.c index a2a32b814a8..c28eb483f45 100644 --- a/test/cfg/gtk.c +++ b/test/cfg/gtk.c @@ -767,3 +767,13 @@ void g_timeout_add_takes_data_test(void) gchar *data_seconds_full = g_strdup("data"); g_timeout_add_seconds_full(0, 1, source_func_frees_data, data_seconds_full, destroy_notify_frees_data); } + +// Handing over data that is already freed is a use after free. +// TODO: functions don't get the deallocuse check yet. +void g_idle_add_freed_data_test(void) +{ + gchar *data = g_strdup("data"); + g_free(data); + // TODO cppcheck-suppress deallocuse + g_idle_add(source_func_frees_data, data); +}