Skip to content

Commit c0b80e0

Browse files
avargitster
authored andcommitted
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 c8ba163 (parse-options: add OPT_STRING_LIST helper, 2011-06-09) the "list" variable was added, and later on in c8ba163 (parse-options: add OPT_STRING_LIST helper, 2011-06-09) the "expect" was added. The "list" variable was last touched in 2721ce2 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6a75658 commit c0b80e0

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

t/helper/test-parse-options.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ static int dry_run = 0, quiet = 0;
1414
static char *string = NULL;
1515
static char *file = NULL;
1616
static int ambiguous;
17-
static struct string_list list = STRING_LIST_INIT_NODUP;
1817

1918
static struct {
2019
int called;
@@ -107,6 +106,8 @@ int cmd__parse_options(int argc, const char **argv)
107106
NULL
108107
};
109108
struct string_list expect = STRING_LIST_INIT_NODUP;
109+
struct string_list list = STRING_LIST_INIT_NODUP;
110+
110111
struct option options[] = {
111112
OPT_BOOL(0, "yes", &boolean, "get a boolean"),
112113
OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
@@ -185,5 +186,9 @@ int cmd__parse_options(int argc, const char **argv)
185186
for (i = 0; i < argc; i++)
186187
show(&expect, &ret, "arg %02d: %s", i, argv[i]);
187188

189+
expect.strdup_strings = 1;
190+
string_list_clear(&expect, 0);
191+
string_list_clear(&list, 0);
192+
188193
return ret;
189194
}

t/t0040-parse-options.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
test_description='our own option parser'
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
cat >expect <<\EOF

0 commit comments

Comments
 (0)