]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/submodule--helper: fix leaking buffer in `is_tip_reachable`
authorPatrick Steinhardt <ps@pks.im>
Thu, 1 Aug 2024 10:39:57 +0000 (12:39 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 1 Aug 2024 15:47:35 +0000 (08:47 -0700)
The `rev` buffer in `is_tip_reachable()` is being populated with the
output of git-rev-list(1) -- if either the command fails or the buffer
contains any data, then the input commit is not reachable.

The buffer isn't used for anything else, but neither do we free it,
causing a memory leak. Fix this.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/submodule--helper.c
t/t7400-submodule-basic.sh

index 863b01627c21c0c9fc5e26ee770aa106f63c3b9e..673810d2c06ac6bf73c3d7f0e77affd4d8bcb7fc 100644 (file)
@@ -2268,6 +2268,7 @@ static int is_tip_reachable(const char *path, const struct object_id *oid)
        struct child_process cp = CHILD_PROCESS_INIT;
        struct strbuf rev = STRBUF_INIT;
        char *hex = oid_to_hex(oid);
+       int reachable;
 
        cp.git_cmd = 1;
        cp.dir = path;
@@ -2277,9 +2278,12 @@ static int is_tip_reachable(const char *path, const struct object_id *oid)
        prepare_submodule_repo_env(&cp.env);
 
        if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
-               return 0;
+               reachable = 0;
+       else
+               reachable = 1;
 
-       return 1;
+       strbuf_release(&rev);
+       return reachable;
 }
 
 static int fetch_in_submodule(const char *module_path, int depth, int quiet,
@@ -3222,6 +3226,7 @@ static int add_submodule(const struct add_data *add_data)
                        die(_("unable to checkout submodule '%s'"), add_data->sm_path);
        }
        ret = 0;
+
 cleanup:
        string_list_clear(&reference, 1);
        return ret;
index 981488885fba49c57176b8d5054b06ac6bb8f4bc..098d8833b6599d935e095453b3b3a865f20b6bc2 100755 (executable)
@@ -12,6 +12,7 @@ subcommands of git submodule.
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup - enable local submodules' '