]> git.ipfire.org Git - thirdparty/git.git/commitdiff
submodule--helper: fix a leak in "clone_submodule"
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Wed, 31 Aug 2022 23:14:08 +0000 (01:14 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 2 Sep 2022 16:18:12 +0000 (09:18 -0700)
Fix a memory leak of the "clone_data_path" variable that we copy or
derive from the "struct module_clone_data" in clone_submodule(). This
code was refactored in preceding commits, but the leak has been with
us since f8eaa0ba98b (submodule--helper, module_clone: always operate
on absolute paths, 2016-03-31).

For the "else" case we don't need to xstrdup() the "clone_data->path",
and we don't need to free our own "clone_data_path". We can therefore
assign the "clone_data->path" to our own "clone_data_path" right away,
and only override it (and remember to free it!) if we need to
xstrfmt() a replacement.

In the case of the module_clone() caller it's from "argv", and doesn't
need to be free'd, and in the case of the add_submodule() caller we
get a pointer to "sm_path", which doesn't need to be directly free'd
either.

Fixing this leak makes several tests pass, so let's mark them as
passing with TEST_PASSES_SANITIZE_LEAK=true.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Reviewed-by: Glen Choo <chooglen@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/submodule--helper.c
t/t1500-rev-parse.sh
t/t6008-rev-list-submodule.sh
t/t7414-submodule-mistakes.sh
t/t7506-status-submodule.sh
t/t7507-commit-verbose.sh

index 3a2a92da5141561e3e58bd8aceac5927556692ff..d308b14904c867b2602e7d05417146ebf84e9990 100644 (file)
@@ -1587,13 +1587,12 @@ static int clone_submodule(const struct module_clone_data *clone_data,
        char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
        char *sm_alternate = NULL, *error_strategy = NULL;
        struct child_process cp = CHILD_PROCESS_INIT;
-       const char *clone_data_path;
+       const char *clone_data_path = clone_data->path;
+       char *to_free = NULL;
 
        if (!is_absolute_path(clone_data->path))
-               clone_data_path = xstrfmt("%s/%s", get_git_work_tree(),
-                                         clone_data->path);
-       else
-               clone_data_path = xstrdup(clone_data->path);
+               clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(),
+                                                   clone_data->path);
 
        if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
                die(_("refusing to create/use '%s' in another submodule's "
@@ -1678,6 +1677,7 @@ static int clone_submodule(const struct module_clone_data *clone_data,
 
        free(sm_gitdir);
        free(p);
+       free(to_free);
        return 0;
 }
 
index 1c2df08333bc600e4fc22f1f55c1d6950b876d67..0e13bcb4ebbf703337bda31e5714620dcc2cf636 100755 (executable)
@@ -4,6 +4,7 @@ test_description='test git rev-parse'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_one () {
index 3153a0d891046421f53ea6543182c47ca750e07a..12e67e187ef21456f79a2ebdcc19c641d8ec81a6 100755 (executable)
@@ -8,6 +8,7 @@ test_description='git rev-list involving submodules that this repo has'
 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' '
index f2e7df59cf24c254440a8bc1c7d5c195c8bfcd5b..3269298197c2c2dcf92a780d401d212101c8475f 100755 (executable)
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='handling of common mistakes people may make with submodules'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'create embedded repository' '
index 3fcb44767f51052f637683a34cbb8a2f21c777cc..f5426a8e589fb6a0beadd9ef793ed6be66b69355 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='git status for submodule'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_create_repo_with_commit () {
index ed2653d46fe6cd0ed1dd5dc2dfd73f7340b8fe31..92462a22374042603a885c4b52fb49b3d16e0373 100755 (executable)
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='verbose commit template'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 write_script "check-for-diff" <<\EOF &&