git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/7] tests: fix a memory leak in test-parse-options.c

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Oct 6, 2021, 10:02 UTC
Message-ID
<patch-2.7-c24e115aa49-20211006T095426Z-avarab@gmail.com>
In-Reply-To
<cover-0.7-00000000000-20211006T095426Z-avarab@gmail.com>

Fix a memory leak in t/helper/test-parse-options.c, we were not freeing the allocated "struct string_list" or its items. While I'm at it move the declaration of the "list" string_list the cmd__parse_options() function.

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>
---
 t/helper/test-parse-options.c | 7 ++++++-
 t/t0040-parse-options.sh      | 1 +
 2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c
index a282b6ff13e..48d3cf6692d 100644
--- a/t/helper/test-parse-options.c
+++ b/t/helper/test-parse-options.c
@@ -14,7 +14,6 @@ static int dry_run = 0, quiet = 0;
 static char *string = NULL;
 static char *file = NULL;
 static int ambiguous;
-static struct string_list list = STRING_LIST_INIT_NODUP;
 
 static struct {
 	int called;
@@ -107,6 +106,8 @@ int cmd__parse_options(int argc, const char **argv)
 		NULL
 	};
 	struct string_list expect = STRING_LIST_INIT_NODUP;
+	struct string_list list = STRING_LIST_INIT_NODUP;
+
 	struct option options[] = {
 		OPT_BOOL(0, "yes", &boolean, "get a boolean"),
 		OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
@@ -185,5 +186,9 @@ int cmd__parse_options(int argc, const char **argv)
 	for (i = 0; i < argc; i++)
 		show(&expect, &ret, "arg %02d: %s", i, argv[i]);
 
+	expect.strdup_strings = 1;
+	string_list_clear(&expect, 0);
+	string_list_clear(&list, 0);
+
 	return ret;
 }
diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh
index da310ed29b1..ed422a1a616 100755
--- a/t/t0040-parse-options.sh
+++ b/t/t0040-parse-options.sh
@@ -5,6 +5,7 @@
 
 test_description='our own option parser'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 cat >expect <<\EOF
-- 
2.33.0.1441.gbbcdb4c3c66
Previous: Ævar Arnfjörð BjarmasonNext: Elijah Newren
Message 3 of 23 in “leak tests: fix "test-tool" & other small leaks”
  1. 0/7 leak tests: fix "test-tool" & other small leaksÆvar Arnfjörð Bjarmason, Oct 6, 2021
  2. 1/7 tests: fix a memory leak in test-prio-queue.cÆvar Arnfjörð Bjarmason, Oct 6, 2021
  3. 2/7 tests: fix a memory leak in test-parse-options.cÆvar Arnfjörð Bjarmason, Oct 6, 2021
  4. Elijah NewrenOct 6, 2021
  5. 3/7 tests: fix a memory leak in test-oidtree.cÆvar Arnfjörð Bjarmason, Oct 6, 2021
  6. 5/7 ls-files: fix a trivial dir_clear() leakÆvar Arnfjörð Bjarmason, Oct 6, 2021
  7. 4/7 tests: fix test-oid-array leak, test in SANITIZE=leakÆvar Arnfjörð Bjarmason, Oct 6, 2021
  8. 6/7 ls-files: add missing string_list_clear()Ævar Arnfjörð Bjarmason, Oct 6, 2021
  9. 7/7 merge: add missing strbuf_release()Ævar Arnfjörð Bjarmason, Oct 6, 2021
  10. Elijah NewrenOct 6, 2021
  11. 0/7 leak tests: fix "test-tool" & other small leaksÆvar Arnfjörð Bjarmason, Oct 7, 2021
  12. 1/7 tests: fix a memory leak in test-prio-queue.cÆvar Arnfjörð Bjarmason, Oct 7, 2021
  13. 2/7 tests: fix a memory leak in test-parse-options.cÆvar Arnfjörð Bjarmason, Oct 7, 2021
  14. 3/7 tests: fix a memory leak in test-oidtree.cÆvar Arnfjörð Bjarmason, Oct 7, 2021
  15. 4/7 tests: fix test-oid-array leak, test in SANITIZE=leakÆvar Arnfjörð Bjarmason, Oct 7, 2021
  16. 7/7 merge: add missing strbuf_release()Ævar Arnfjörð Bjarmason, Oct 7, 2021
  17. 6/7 ls-files: add missing string_list_clear()Ævar Arnfjörð Bjarmason, Oct 7, 2021
  18. 5/7 ls-files: fix a trivial dir_clear() leakÆvar Arnfjörð Bjarmason, Oct 7, 2021
  19. Junio C HamanoOct 7, 2021
  20. Ævar Arnfjörð BjarmasonOct 13, 2021
  21. Junio C HamanoOct 13, 2021
  22. Ævar Arnfjörð BjarmasonOct 14, 2021
  23. Junio C HamanoOct 14, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.