From ab09a3e933cb15572a101c7b7cf30e7810342130 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 19 Sep 2025 13:25:53 +0100 Subject: [PATCH] gutils: Fix deliberate-leak code in g_reload_user_special_dirs_cache() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It seems it wasn’t behaving as advertised: it was freeing all the old string values unless they were strcmp-equal to the new ones, in which case the new ones were discarded. What’s actually documented is that the old values are always leaked, unless they’re strcmp-equal to the new ones. Adjust the code to match the documentation. A unit test will be added in a following commit. Signed-off-by: Philip Withnall --- glib/gutils.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/glib/gutils.c b/glib/gutils.c index 541ad4a55..4d19ddb61 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -2337,18 +2337,18 @@ g_reload_user_special_dirs_cache (void) for (i = 0; i < G_USER_N_DIRECTORIES; i++) { old_val = old_g_user_special_dirs[i]; - if (g_user_special_dirs[i] == NULL) + + if (g_user_special_dirs[i] == NULL || + g_strcmp0 (old_val, g_user_special_dirs[i]) != 0) { - g_user_special_dirs[i] = old_val; + g_ignore_leak (old_val); } - else if (g_strcmp0 (old_val, g_user_special_dirs[i]) == 0) + else { /* don't leak */ g_free (g_user_special_dirs[i]); g_user_special_dirs[i] = old_val; } - else - g_free (old_val); } /* free the old array */