gtestutils: Handle empty argv array passed to g_test_init()

This can happen if a caller (ab)uses `execve()` to execute a gtest
process with an empty `argv` array. `g_test_init()` has to gracefully
handle such a situation.

Fix a few problem areas in the code, and add a simple test which checks
that `g_test_init()` doesn’t crash when called with an empty `argv`.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall
2022-01-31 14:55:50 +00:00
parent 44f4d55150
commit a6311f81ee
4 changed files with 64 additions and 7 deletions

View File

@@ -102,6 +102,24 @@ main (int argc,
argc -= 1;
argv[argc] = NULL;
if (g_strcmp0 (argv1, "init-null-argv0") == 0)
{
int test_argc = 0;
char *test_argva[1] = { NULL };
char **test_argv = test_argva;
/* Test that `g_test_init()` can handle being called with an empty argv
* and argc == 0. While this isnt recommended, it is possible for another
* process to use execve() to call a gtest process this way, so wed
* better handle it gracefully.
*
* This test cant be run after `g_test_init()` has been called normally,
* as it isnt allowed to be called more than once in a process. */
g_test_init (&test_argc, &test_argv, NULL);
return 0;
}
g_test_init (&argc, &argv, NULL);
g_test_set_nonfatal_assertions ();