Skip to content

Commit 99b6c45

Browse files
avargitster
authored andcommitted
check-ref-format: fix trivial memory leak
Fix a memory leak in "git check-ref-format" that's been present in the code in one form or another since 38eedc6 (git check-ref-format --print, 2009-10-12), the code got substantially refactored in cfbe22f (check-ref-format: handle subcommands in separate functions, 2010-08-05). 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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e4a4b31 commit 99b6c45

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

builtin/check-ref-format.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
5757
int normalize = 0;
5858
int flags = 0;
5959
const char *refname;
60+
char *to_free = NULL;
61+
int ret = 1;
6062

6163
if (argc == 2 && !strcmp(argv[1], "-h"))
6264
usage(builtin_check_ref_format_usage);
@@ -81,11 +83,14 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
8183

8284
refname = argv[i];
8385
if (normalize)
84-
refname = collapse_slashes(refname);
86+
refname = to_free = collapse_slashes(refname);
8587
if (check_refname_format(refname, flags))
86-
return 1;
88+
goto cleanup;
8789
if (normalize)
8890
printf("%s\n", refname);
8991

90-
return 0;
92+
ret = 0;
93+
cleanup:
94+
free(to_free);
95+
return ret;
9196
}

t/t1402-check-ref-format.sh

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

33
test_description='Test git check-ref-format'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
valid_ref() {

0 commit comments

Comments
 (0)