tests: Fix a leak in environment.c with undefined behaviour tests on

`g_environ_setenv()` and `g_environ_unsetenv()` were correctly returning
`NULL` when their preconditions failed (as the test is supposed to be
exercising). That overwrote the value of `env` without freeing it,
resulting in a leak.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2022-02-18 02:01:12 +00:00
parent 2249da981a
commit 12a4fcdbed

View File

@ -180,25 +180,31 @@ test_environ_array (void)
if (g_test_undefined ()) if (g_test_undefined ())
{ {
gchar **undefined_env;
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*assertion* != NULL*"); "*assertion* != NULL*");
env = g_environ_setenv (env, NULL, "bar", TRUE); undefined_env = g_environ_setenv (env, NULL, "bar", TRUE);
g_test_assert_expected_messages (); g_test_assert_expected_messages ();
g_strfreev (undefined_env);
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*assertion* == NULL*"); "*assertion* == NULL*");
env = g_environ_setenv (env, "foo=fuz", "bar", TRUE); undefined_env = g_environ_setenv (env, "foo=fuz", "bar", TRUE);
g_test_assert_expected_messages (); g_test_assert_expected_messages ();
g_strfreev (undefined_env);
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*assertion* != NULL*"); "*assertion* != NULL*");
env = g_environ_setenv (env, "foo", NULL, TRUE); undefined_env = g_environ_setenv (env, "foo", NULL, TRUE);
g_test_assert_expected_messages (); g_test_assert_expected_messages ();
g_strfreev (undefined_env);
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*assertion* != NULL*"); "*assertion* != NULL*");
env = g_environ_unsetenv (env, NULL); undefined_env = g_environ_unsetenv (env, NULL);
g_test_assert_expected_messages (); g_test_assert_expected_messages ();
g_strfreev (undefined_env);
} }
env = g_environ_setenv (env, "foo", "bar", TRUE); env = g_environ_setenv (env, "foo", "bar", TRUE);