From 683cf0a1bae46371f7d37bb81658b7e743e27bc4 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 12 Sep 2024 22:31:15 +0100 Subject: [PATCH] tests: Fix a scan-build warning about uninitialised variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s a false positive, but points to a slightly unnecessary use of a global variable to store something which could be computed per-test. scan-build thought that arrays of size `n_handlers` could have uninitialised values accessed in them if `n_handlers` were to change value part-way through a test (which it didn’t). Signed-off-by: Philip Withnall --- gobject/tests/signal-handler.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/gobject/tests/signal-handler.c b/gobject/tests/signal-handler.c index de38e8d65..81db9ccf9 100644 --- a/gobject/tests/signal-handler.c +++ b/gobject/tests/signal-handler.c @@ -45,7 +45,11 @@ nop (void) { } -static guint n_handlers = 0; +static guint +choose_n_handlers (void) +{ + return g_test_perf () ? 500000 : 1; +} static void test_connect_many (void) @@ -53,6 +57,7 @@ test_connect_many (void) MyObj *o; gdouble time_elapsed; guint i; + const guint n_handlers = choose_n_handlers (); o = g_object_new (my_obj_get_type (), NULL); @@ -75,6 +80,7 @@ test_disconnect_many_ordered (void) gulong *handlers; gdouble time_elapsed; guint i; + const guint n_handlers = choose_n_handlers (); handlers = g_malloc_n (n_handlers, sizeof (*handlers)); o = g_object_new (my_obj_get_type (), NULL); @@ -102,6 +108,7 @@ test_disconnect_many_inverse (void) gulong *handlers; gdouble time_elapsed; guint i; + const guint n_handlers = choose_n_handlers (); handlers = g_malloc_n (n_handlers, sizeof (*handlers)); o = g_object_new (my_obj_get_type (), NULL); @@ -130,6 +137,7 @@ test_disconnect_many_random (void) gulong id; gdouble time_elapsed; guint i, j; + const guint n_handlers = choose_n_handlers (); handlers = g_malloc_n (n_handlers, sizeof (*handlers)); o = g_object_new (my_obj_get_type (), NULL); @@ -166,6 +174,7 @@ test_disconnect_2_signals (void) gulong id; gdouble time_elapsed; guint i, j; + const guint n_handlers = choose_n_handlers (); handlers = g_malloc_n (n_handlers, sizeof (*handlers)); o = g_object_new (my_obj_get_type (), NULL); @@ -208,6 +217,7 @@ test_disconnect_2_objects (void) gulong id; gdouble time_elapsed; guint i, j; + const guint n_handlers = choose_n_handlers (); handlers = g_malloc_n (n_handlers, sizeof (*handlers)); objects = g_malloc_n (n_handlers, sizeof (*objects)); @@ -262,6 +272,7 @@ test_block_many (void) gulong id; gdouble time_elapsed; guint i, j; + const guint n_handlers = choose_n_handlers (); handlers = g_malloc_n (n_handlers, sizeof (*handlers)); o = g_object_new (my_obj_get_type (), NULL); @@ -298,8 +309,6 @@ main (int argc, char *argv[]) { g_test_init (&argc, &argv, NULL); - n_handlers = g_test_perf () ? 500000 : 1; - g_test_add_func ("/signal/handler/connect-many", test_connect_many); g_test_add_func ("/signal/handler/disconnect-many-ordered", test_disconnect_many_ordered); g_test_add_func ("/signal/handler/disconnect-many-inverse", test_disconnect_many_inverse);