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 39702431500 (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 <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Feb 5, 2022 at 01:04 UTC
d17294a05e7601b5139e09609fd0f805ac78271b
2 files changed
+8
-2
builtin/hash-object.c
+7
-2
@@ -92,6 +92,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
92
int nongit = 0;
93
unsigned flags = HASH_FORMAT_CHECK;
94
const char *vpath = NULL;
95
+ char *vpath_free = NULL;
96
const struct option hash_object_options[] = {
97
OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
98
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)
115
else
116
prefix = setup_git_directory_gently(&nongit);
117
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
+ }
122
123
git_config(git_default_config, NULL);
124
@@ -156,5 +159,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
159
if (stdin_paths)
160
hash_stdin_paths(type, no_filters, flags, literally);
161
162
+ free(vpath_free);
163
+
164
return 0;
165
}
t/t1007-hash-object.sh
+1
@@ -2,6 +2,7 @@
2
3
test_description="git hash-object"
4
5
+TEST_PASSES_SANITIZE_LEAK=true
6
. ./test-lib.sh
7
8
echo_without_newline() {