tests: fix a memory leak in test-parse-options.c

Fix a memory leak in t/helper/test-parse-options.c, we were not freeing the allocated "struct string_list" or its items. Let's move the declaration of the "list" variable into the cmd__parse_options() and release it at the end. In c8ba1639165 (parse-options: add OPT_STRING_LIST helper, 2011-06-09) the "list" variable was added, and later on in c8ba1639165 (parse-options: add OPT_STRING_LIST helper, 2011-06-09) the "expect" was added. The "list" variable was last touched in 2721ce21e43 (use string_list initializer consistently, 2016-06-13), but it was still left at the static scope, it's better to move it to the function for consistency. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 7, 2021 at 12:01 UTC c0b80e05f79a3a81af202203237687b1f4b25605
2 files changed +7 -1
t/helper/test-parse-options.c
+6 -1
@@ -14,7 +14,6 @@ static int dry_run = 0, quiet = 0;
14 static char *string = NULL;
15 static char *file = NULL;
16 static int ambiguous;
17 -static struct string_list list = STRING_LIST_INIT_NODUP;
17
18 static struct {
19 int called;
@@ -107,6 +106,8 @@ int cmd__parse_options(int argc, const char **argv)
106 NULL
107 };
108 struct string_list expect = STRING_LIST_INIT_NODUP;
109 + struct string_list list = STRING_LIST_INIT_NODUP;
110 +
111 struct option options[] = {
112 OPT_BOOL(0, "yes", &boolean, "get a boolean"),
113 OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
@@ -185,5 +186,9 @@ int cmd__parse_options(int argc, const char **argv)
186 for (i = 0; i < argc; i++)
187 show(&expect, &ret, "arg %02d: %s", i, argv[i]);
188
189 + expect.strdup_strings = 1;
190 + string_list_clear(&expect, 0);
191 + string_list_clear(&list, 0);
192 +
193 return ret;
194 }
t/t0040-parse-options.sh
+1
@@ -5,6 +5,7 @@
5
6 test_description='our own option parser'
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 cat >expect <<\EOF