Skip to content

Commit d17294a

Browse files
avargitster
authored andcommitted
hash-object: fix a trivial leak in --path
Fix a memory leak that happened when the --path option was provided. This leak has been with us ever since the option was added in 3970243 (add --path option to git hash-object, 2008-08-03). We can now mark "t1007-hash-object.sh" as passing when git is compiled with SANITIZE=leak. It'll now run in the the "GIT_TEST_PASSING_SANITIZE_LEAK=true" test mode (the "linux-leaks" CI target). Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4c53a8c commit d17294a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

builtin/hash-object.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
9292
int nongit = 0;
9393
unsigned flags = HASH_FORMAT_CHECK;
9494
const char *vpath = NULL;
95+
char *vpath_free = NULL;
9596
const struct option hash_object_options[] = {
9697
OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
9798
OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
@@ -114,8 +115,10 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
114115
else
115116
prefix = setup_git_directory_gently(&nongit);
116117

117-
if (vpath && prefix)
118-
vpath = prefix_filename(prefix, vpath);
118+
if (vpath && prefix) {
119+
vpath_free = prefix_filename(prefix, vpath);
120+
vpath = vpath_free;
121+
}
119122

120123
git_config(git_default_config, NULL);
121124

@@ -156,5 +159,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
156159
if (stdin_paths)
157160
hash_stdin_paths(type, no_filters, flags, literally);
158161

162+
free(vpath_free);
163+
159164
return 0;
160165
}

t/t1007-hash-object.sh

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

33
test_description="git hash-object"
44

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

78
echo_without_newline() {

0 commit comments

Comments
 (0)