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

[PATCH 2/9] test-tool path-utils: fix a memory leak

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jun 30, 2022, 23:47 UTC
Message-ID
<patch-2.9-06b2dcf4f12-20220630T180129Z-avarab@gmail.com>
In-Reply-To
<cover-0.9-00000000000-20220630T180129Z-avarab@gmail.com>

Fix a memory leak in "test-tool path-utils", as a result we can mark the corresponding test as passing with SANITIZE=leak using "TEST_PASSES_SANITIZE_LEAK=true".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/helper/test-path-utils.c | 10 ++++++++--
 t/t0060-path-utils.sh      |  1 +
 2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c
index 229ed416b0e..380437cfe02 100644
--- a/t/helper/test-path-utils.c
+++ b/t/helper/test-path-utils.c
@@ -294,11 +294,13 @@ static int protect_ntfs_hfs_benchmark(int argc, const char **argv)
 int cmd__path_utils(int argc, const char **argv)
 {
 	if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
-		char *buf = xmallocz(strlen(argv[2]));
+		char *to_free = NULL;
+		char *buf = to_free = xmallocz(strlen(argv[2]));
 		int rv = normalize_path_copy(buf, argv[2]);
 		if (rv)
 			buf = "++failed++";
 		puts(buf);
+		free(to_free);
 		return 0;
 	}
 
@@ -356,7 +358,10 @@ int cmd__path_utils(int argc, const char **argv)
 		int nongit_ok;
 		setup_git_directory_gently(&nongit_ok);
 		while (argc > 3) {
-			puts(prefix_path(prefix, prefix_len, argv[3]));
+			char *pfx = prefix_path(prefix, prefix_len, argv[3]);
+
+			puts(pfx);
+			free(pfx);
 			argc--;
 			argv++;
 		}
@@ -366,6 +371,7 @@ int cmd__path_utils(int argc, const char **argv)
 	if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
 		char *prefix = strip_path_suffix(argv[2], argv[3]);
 		printf("%s\n", prefix ? prefix : "(null)");
+		free(prefix);
 		return 0;
 	}
 
diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh
index aa35350b6f3..1f2007e62b7 100755
--- a/t/t0060-path-utils.sh
+++ b/t/t0060-path-utils.sh
@@ -5,6 +5,7 @@
 
 test_description='Test various path utilities'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 norm_path() {
-- 
2.37.0.874.g7d3439f13c4
Previous: Ævar Arnfjörð BjarmasonNext: Eric Sunshine
Message 3 of 26 in “test-tool: fix memory leaks”
  1. 0/9 test-tool: fix memory leaksÆvar Arnfjörð Bjarmason, Jun 30, 2022
  2. 3/9 test-tool {dump,scrap}-cache-tree: fix memory leaksÆvar Arnfjörð Bjarmason, Jun 30, 2022
  3. 2/9 test-tool path-utils: fix a memory leakÆvar Arnfjörð Bjarmason, Jun 30, 2022
  4. Eric SunshineJul 1, 2022
  5. Ævar Arnfjörð BjarmasonJul 1, 2022
  6. 1/9 test-tool test-hash: fix a memory leakÆvar Arnfjörð Bjarmason, Jun 30, 2022
  7. 8/9 test-tool ref-store: fix a memory leakÆvar Arnfjörð Bjarmason, Jun 30, 2022
  8. 4/9 test-tool urlmatch-normalization: fix a memory leakÆvar Arnfjörð Bjarmason, Jun 30, 2022
  9. 5/9 test-tool regex: call regfree(), fix memory leaksÆvar Arnfjörð Bjarmason, Jun 30, 2022
  10. Junio C HamanoJul 1, 2022
  11. 7/9 test-tool bloom: fix a memory leakÆvar Arnfjörð Bjarmason, Jun 30, 2022
  12. Eric SunshineJul 1, 2022
  13. Ævar Arnfjörð BjarmasonJul 1, 2022
  14. 9/9 test-tool delta: fix a memory leakÆvar Arnfjörð Bjarmason, Jun 30, 2022
  15. 6/9 test-tool json-writer: fix memory leaksÆvar Arnfjörð Bjarmason, Jun 30, 2022
  16. 0/9 test-tool: fix memory leaksÆvar Arnfjörð Bjarmason, Jul 1, 2022
  17. 1/9 test-tool test-hash: fix a memory leakÆvar Arnfjörð Bjarmason, Jul 1, 2022
  18. 2/9 test-tool path-utils: fix a memory leakÆvar Arnfjörð Bjarmason, Jul 1, 2022
  19. Junio C HamanoJul 1, 2022
  20. 3/9 test-tool {dump,scrap}-cache-tree: fix memory leaksÆvar Arnfjörð Bjarmason, Jul 1, 2022
  21. 4/9 test-tool urlmatch-normalization: fix a memory leakÆvar Arnfjörð Bjarmason, Jul 1, 2022
  22. 5/9 test-tool regex: call regfree(), fix memory leaksÆvar Arnfjörð Bjarmason, Jul 1, 2022
  23. 6/9 test-tool json-writer: fix memory leaksÆvar Arnfjörð Bjarmason, Jul 1, 2022
  24. 7/9 test-tool bloom: fix memory leaksÆvar Arnfjörð Bjarmason, Jul 1, 2022
  25. 8/9 test-tool ref-store: fix a memory leakÆvar Arnfjörð Bjarmason, Jul 1, 2022
  26. 9/9 test-tool delta: fix a memory leakÆvar Arnfjörð Bjarmason, Jul 1, 2022

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.