Browse Source

merge-file: fix memory leaks on error path

Fix a memory leak in "merge-file", we need to loop over the "mmfs"
array and free() what we've got so far when we error out. As a result
we can mark a 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>
maint
Ævar Arnfjörð Bjarmason 2 years ago committed by Junio C Hamano
parent
commit
e72e12cc02
  1. 10
      builtin/merge-file.c
  2. 2
      t/t6403-merge-file.sh

10
builtin/merge-file.c

@ -87,7 +87,8 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix) @@ -87,7 +87,8 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)

free(fname);
if (ret)
return ret;
goto cleanup;

}

xmp.ancestor = names[1];
@ -95,9 +96,6 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix) @@ -95,9 +96,6 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
xmp.file2 = names[2];
ret = xdl_merge(mmfs + 1, mmfs + 0, mmfs + 2, &xmp, &result);

for (i = 0; i < 3; i++)
free(mmfs[i].ptr);

if (ret >= 0) {
const char *filename = argv[0];
char *fpath = prefix_filename(prefix, argv[0]);
@ -118,5 +116,9 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix) @@ -118,5 +116,9 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
if (ret > 127)
ret = 127;

cleanup:
for (i = 0; i < 3; i++)
free(mmfs[i].ptr);

return ret;
}

2
t/t6403-merge-file.sh

@ -1,6 +1,8 @@ @@ -1,6 +1,8 @@
#!/bin/sh

test_description='RCS merge replacement: merge-file'

TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh

test_expect_success 'setup' '

Loading…
Cancel
Save