test-tool path-utils: fix a memory leak

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jul 1, 2022 at 12:37 UTC e287a5b0a434e05de39ede507e5b3fc24f67cbb0
2 files changed +8 -4
t/helper/test-path-utils.c
+7 -4
@@ -296,9 +296,8 @@ int cmd__path_utils(int argc, const char **argv)
296 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
297 char *buf = xmallocz(strlen(argv[2]));
298 int rv = normalize_path_copy(buf, argv[2]);
299 - if (rv)
300 - buf = "++failed++";
301 - puts(buf);
299 + puts(rv ? "++failed++" : buf);
300 + free(buf);
301 return 0;
302 }
303
@@ -356,7 +355,10 @@ int cmd__path_utils(int argc, const char **argv)
355 int nongit_ok;
356 setup_git_directory_gently(&nongit_ok);
357 while (argc > 3) {
359 - puts(prefix_path(prefix, prefix_len, argv[3]));
358 + char *pfx = prefix_path(prefix, prefix_len, argv[3]);
359 +
360 + puts(pfx);
361 + free(pfx);
362 argc--;
363 argv++;
364 }
@@ -366,6 +368,7 @@ int cmd__path_utils(int argc, const char **argv)
368 if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
369 char *prefix = strip_path_suffix(argv[2], argv[3]);
370 printf("%s\n", prefix ? prefix : "(null)");
371 + free(prefix);
372 return 0;
373 }
374
t/t0060-path-utils.sh
+1
@@ -5,6 +5,7 @@
5
6 test_description='Test various path utilities'
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 norm_path() {