]> git.ipfire.org Git - thirdparty/git.git/commitdiff
hash-object: fix a trivial leak in --path
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Sat, 5 Feb 2022 00:04:29 +0000 (01:04 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 7 Feb 2022 01:55:55 +0000 (17:55 -0800)
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>
builtin/hash-object.c
t/t1007-hash-object.sh

index c7b3ad74c60cb914aa480b24ab7fc768903159bc..db9b253527103ab45965ac85f6ac761a1b451898 100644 (file)
@@ -92,6 +92,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
        int nongit = 0;
        unsigned flags = HASH_FORMAT_CHECK;
        const char *vpath = NULL;
+       char *vpath_free = NULL;
        const struct option hash_object_options[] = {
                OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
                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)
        else
                prefix = setup_git_directory_gently(&nongit);
 
-       if (vpath && prefix)
-               vpath = prefix_filename(prefix, vpath);
+       if (vpath && prefix) {
+               vpath_free = prefix_filename(prefix, vpath);
+               vpath = vpath_free;
+       }
 
        git_config(git_default_config, NULL);
 
@@ -156,5 +159,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
        if (stdin_paths)
                hash_stdin_paths(type, no_filters, flags, literally);
 
+       free(vpath_free);
+
        return 0;
 }
index 64b340f227274c28f00a564a682f2369a7b85d8d..ac5ad8c7402d2bd3f43d38e5a676d864ea415d37 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description="git hash-object"
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 echo_without_newline() {