summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <philip@tecnocode.co.uk>2024-05-13 12:00:34 +0000
committerPhilip Withnall <philip@tecnocode.co.uk>2024-05-13 12:00:34 +0000
commitb9f9dd45f92a62c939e303fba0e854022f6a1553 (patch)
tree0567e39fb14c7399e27bc47edf7d4ba23ef2ba5f
parent86dd02f48762ae97c7bc805c45e8905cd969bbac (diff)
parentcaf2f2fbda97aeb325c7993bf7922f8734b78135 (diff)
downloadglib-upstream-main.tar.gz
Merge branch 'wip/3v1n0/leak-fixes' into 'main'upstream-main
tests: Fix various memory leaks and valgrind / ASAN errors See merge request GNOME/glib!4059
-rw-r--r--gio/glocalfile.c7
-rw-r--r--gio/tests/cancellable.c6
-rw-r--r--gio/tests/gsettings.c5
-rw-r--r--gio/tests/gsubprocess.c10
-rw-r--r--gio/tests/meson.build12
-rw-r--r--girepository/introspection/meson.build2
-rwxr-xr-xglib/tests/assert-msg-test.py4
-rw-r--r--glib/tests/mapping.c11
-rw-r--r--glib/tests/meson.build21
-rw-r--r--glib/tests/unicode.c1
-rw-r--r--meson.build5
11 files changed, 70 insertions, 14 deletions
diff --git a/gio/glocalfile.c b/gio/glocalfile.c
index 69dd60f81..bc33af644 100644
--- a/gio/glocalfile.c
+++ b/gio/glocalfile.c
@@ -2284,6 +2284,7 @@ g_local_file_trash (GFile *file,
data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
original_name_escaped, delete_time);
g_free (delete_time);
+ g_clear_pointer (&original_name_escaped, g_free);
if (!g_file_set_contents_full (infofile, data, -1,
G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING,
@@ -2291,6 +2292,7 @@ g_local_file_trash (GFile *file,
{
g_unlink (infofile);
+ g_free (data);
g_free (filesdir);
g_free (trashname);
g_free (infofile);
@@ -2298,6 +2300,8 @@ g_local_file_trash (GFile *file,
return FALSE;
}
+ g_clear_pointer (&data, g_free);
+
/* TODO: Maybe we should verify that you can delete the file from the trash
* before moving it? OTOH, that is hard, as it needs a recursive scan
*/
@@ -2341,9 +2345,6 @@ g_local_file_trash (GFile *file,
/* TODO: Do we need to update mtime/atime here after the move? */
g_free (infofile);
- g_free (data);
-
- g_free (original_name_escaped);
g_free (trashname);
return TRUE;
diff --git a/gio/tests/cancellable.c b/gio/tests/cancellable.c
index be7017dcb..f12a339cb 100644
--- a/gio/tests/cancellable.c
+++ b/gio/tests/cancellable.c
@@ -25,6 +25,7 @@
#include <gio/gio.h>
#include "glib/glib-private.h"
+#include "glib/gvalgrind.h"
/* How long to wait in ms for each iteration */
#define WAIT_ITERATION (10)
@@ -276,6 +277,11 @@ test_cancellable_source_threaded_dispose (void)
"to (in another thread)");
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/issues/1841");
+#ifdef ENABLE_VALGRIND
+ if (RUNNING_ON_VALGRIND)
+ g_test_incomplete ("FIXME: Leaks lots of GCancellableSource objects, see glib#2309");
+#endif
+
/* Create a new thread and wait until it’s ready to execute. Each iteration of
* the test will pass it a new #GCancellableSource. */
g_cond_init (&data.cond);
diff --git a/gio/tests/gsettings.c b/gio/tests/gsettings.c
index 4c25b3164..cb98dd322 100644
--- a/gio/tests/gsettings.c
+++ b/gio/tests/gsettings.c
@@ -924,6 +924,8 @@ test_l10n_time (void)
g_assert_true (original_locale != (locale_t) 0);
new_locale = duplocale (original_locale);
g_assert_true (new_locale != (locale_t) 0);
+ g_clear_pointer (&new_locale, freelocale);
+
new_locale = newlocale (LC_TIME_MASK, "C", new_locale);
g_assert_true (new_locale != (locale_t) 0);
result = uselocale (new_locale);
@@ -936,6 +938,7 @@ test_l10n_time (void)
g_assert_cmpstr (str, ==, "12:00 AM");
g_free (str);
+ g_clear_pointer (&new_locale, freelocale);
str = NULL;
new_locale = newlocale (LC_TIME_MASK, "de_DE.UTF-8", new_locale);
@@ -964,7 +967,7 @@ test_l10n_time (void)
result = uselocale (original_locale);
g_assert_true (result == new_locale);
- freelocale (new_locale);
+ g_clear_pointer (&new_locale, freelocale);
g_object_unref (settings);
#endif
diff --git a/gio/tests/gsubprocess.c b/gio/tests/gsubprocess.c
index 5baff93a4..2ba28d693 100644
--- a/gio/tests/gsubprocess.c
+++ b/gio/tests/gsubprocess.c
@@ -1,3 +1,4 @@
+#include "glib-private.h"
#include <gio/gio.h>
#include <string.h>
@@ -2073,6 +2074,8 @@ trace_children (pid_t main_child)
static void
test_exit_status_trapped (void)
{
+#ifndef _GLIB_ADDRESS_SANITIZER
+
#ifdef __linux__
GPtrArray *args = NULL;
pid_t test_child;
@@ -2103,6 +2106,13 @@ test_exit_status_trapped (void)
#else
g_test_skip ("ptrace() support for this test is only tested on Linux");
#endif
+
+#else /* if defined (_GLIB_ADDRESS_SANITIZER) */
+
+g_test_skip ("LeakSanitizer does not work under ptrace");
+(void) trace_children;
+
+#endif /* _GLIB_ADDRESS_SANITIZER */
}
#endif /* G_OS_UNIX */
diff --git a/gio/tests/meson.build b/gio/tests/meson.build
index d6b7f26d5..1c16619e5 100644
--- a/gio/tests/meson.build
+++ b/gio/tests/meson.build
@@ -309,8 +309,12 @@ if host_machine.system() != 'windows'
},
}
- # LD_PRELOAD modules don't work so well with AddressSanitizer
- if have_rtld_next and glib_build_shared and get_option('b_sanitize') == 'none'
+ if have_rtld_next and glib_build_shared
+ asan_env = {}
+ if 'address' in glib_sanitizers
+ asan_env = {'ASAN_OPTIONS': 'verify_asan_link_order=0'}
+ endif
+
gio_tests += {
'gsocketclient-slow' : {
'depends' : [
@@ -325,10 +329,10 @@ if host_machine.system() != 'windows'
],
'env' : {
'LD_PRELOAD': '@0@/slow-connect-preload.so'.format(meson.current_build_dir())
- },
+ } + asan_env,
'installed_tests_env' : {
'LD_PRELOAD': '@0@/slow-connect-preload.so'.format(installed_tests_execdir),
- },
+ } + asan_env,
},
}
endif
diff --git a/girepository/introspection/meson.build b/girepository/introspection/meson.build
index 862ca61cc..0a217e75f 100644
--- a/girepository/introspection/meson.build
+++ b/girepository/introspection/meson.build
@@ -9,7 +9,7 @@ gi_gen_shared_sources = [
gi_gen_env_variables = environment()
-if get_option('b_sanitize') != ''
+if 'address' in glib_sanitizers
gi_gen_env_variables.append(
'ASAN_OPTIONS', 'verify_asan_link_order=0', separator: ',')
endif
diff --git a/glib/tests/assert-msg-test.py b/glib/tests/assert-msg-test.py
index 33aa2249e..5e5293b5b 100755
--- a/glib/tests/assert-msg-test.py
+++ b/glib/tests/assert-msg-test.py
@@ -143,6 +143,10 @@ class TestAssertMessage(unittest.TestCase):
"""Test running g_assert() within gdb and fail the program."""
if self.__gdb is None:
self.skipTest("GDB is not installed, skipping this test!")
+ if {"thread", "address"} & set(
+ os.getenv("_GLIB_TEST_SANITIZERS", "").split(",")
+ ):
+ self.skipTest("GDB can't run under sanitizers")
with tempfile.NamedTemporaryFile(
prefix="assert-msg-test-", suffix=".gdb", mode="w", delete=False
diff --git a/glib/tests/mapping.c b/glib/tests/mapping.c
index 99cb8eee0..bbd4de8b0 100644
--- a/glib/tests/mapping.c
+++ b/glib/tests/mapping.c
@@ -22,6 +22,7 @@
#ifdef G_OS_UNIX
#include <unistd.h>
+#include <sys/wait.h>
#endif
#ifdef G_OS_WIN32
#include <process.h>
@@ -124,6 +125,7 @@ child_main (void)
g_free (childname);
g_free (global_filename);
g_free (dir);
+ g_mapped_file_unref (map);
signal_parent (NULL);
}
@@ -201,8 +203,10 @@ test_child_private (void)
gsize len;
gchar *child_argv[4];
GPid child_pid;
+ GSpawnFlags spawn_flags = G_SPAWN_DEFAULT;
#ifndef G_OS_WIN32
GMainLoop *loop;
+ int wait_status;
#endif
gchar pid[100];
gchar *dir, *global_filename, *childname;
@@ -221,6 +225,7 @@ test_child_private (void)
#ifndef G_OS_WIN32
signal (SIGUSR1, handle_usr1);
+ spawn_flags |= G_SPAWN_DO_NOT_REAP_CHILD;
#endif
g_snprintf (pid, sizeof(pid), "%d", getpid ());
@@ -230,7 +235,7 @@ test_child_private (void)
child_argv[3] = NULL;
result = g_spawn_async (dir, child_argv, NULL,
- 0, NULL, NULL, &child_pid, &error);
+ spawn_flags, NULL, NULL, &child_pid, &error);
g_assert_no_error (error);
g_assert_true (result);
g_test_message ("test_child_private: child spawned");
@@ -261,6 +266,10 @@ test_child_private (void)
#ifndef G_OS_WIN32
g_idle_add (check_stop, loop);
g_main_loop_run (loop);
+ waitpid (child_pid, &wait_status, 0);
+ g_test_message ("Child exited with status %d", wait_status);
+ g_spawn_check_wait_status (wait_status, &error);
+ g_assert_no_error (error);
#else
g_usleep (2000000);
#endif
diff --git a/glib/tests/meson.build b/glib/tests/meson.build
index cbce66faf..5fbaa854b 100644
--- a/glib/tests/meson.build
+++ b/glib/tests/meson.build
@@ -186,6 +186,8 @@ glib_tests = {
'1bit-emufutex' : {
'source' : '1bit-mutex.c',
'c_args' : ['-DTEST_EMULATED_FUTEX'],
+ # FIXME: https://gitlab.gnome.org/GNOME/glib/-/issues/3359
+ 'can_fail': 'undefined' in glib_sanitizers,
'install' : false,
'suite' : ['slow'],
},
@@ -263,15 +265,20 @@ else
var_preload = 'DYLD_INSERT_LIBRARIES'
endif
+ asan_env = {}
+ if 'address' in glib_sanitizers
+ asan_env = {'ASAN_OPTIONS': 'verify_asan_link_order=0'}
+ endif
+
glib_tests += {
'gutils-user-database' : {
- 'depends' : [],
+ 'depends' : getpwuid_preload,
'env' : {
var_preload: getpwuid_preload.full_path()
- },
+ } + asan_env,
'installed_tests_env' : {
var_preload: installed_tests_execdir / fs.name(getpwuid_preload.full_path())
- },
+ } + asan_env,
},
}
endif
@@ -484,6 +491,7 @@ python_tests = {
'assert-msg-test.py' : {
'can_fail' : host_system == 'windows',
'extra_programs': ['assert-msg-test'],
+ 'env': {'_GLIB_TEST_SANITIZERS': ','.join(glib_sanitizers)},
},
}
@@ -503,6 +511,11 @@ foreach test_name, extra_args : python_tests
suite += 'failing'
endif
+ local_test_env = test_env
+ foreach var, value : extra_args.get('env', {})
+ local_test_env.append(var, value)
+ endforeach
+
foreach program : extra_args.get('extra_programs', [])
depends += test_extra_programs_targets[program]
endforeach
@@ -513,7 +526,7 @@ foreach test_name, extra_args : python_tests
protocol : extra_args.get('protocol', test_protocol),
depends: depends,
args: ['-B', files(test_name)],
- env: test_env,
+ env: local_test_env,
suite: suite,
)
diff --git a/glib/tests/unicode.c b/glib/tests/unicode.c
index e91425aa7..cb74068a5 100644
--- a/glib/tests/unicode.c
+++ b/glib/tests/unicode.c
@@ -525,6 +525,7 @@ test_turkish_strupdown (void)
if (oldlocale == NULL)
{
g_test_skip ("locale tr_TR not available");
+ g_free (old_lang);
return;
}
diff --git a/meson.build b/meson.build
index 8b4294055..aad78f574 100644
--- a/meson.build
+++ b/meson.build
@@ -635,6 +635,11 @@ endif
# improve this.
glib_link_flags = cc.get_supported_link_arguments(warning_c_link_args)
+glib_sanitizers = get_option('b_sanitize').split(',')
+if glib_sanitizers == ['none']
+ glib_sanitizers = []
+endif
+
# Windows SDK requirements and checks
if host_system == 'windows'
# Check whether we're building for UWP apps