Skip to content

Commit e77b3da

Browse files
avargitster
authored andcommitted
submodule--helper: fix a leak in "clone_submodule"
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 f8eaa0b (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 <[email protected]> Reviewed-by: Glen Choo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5647d74 commit e77b3da

File tree

6 files changed

+12
-5
lines changed

6 files changed

+12
-5
lines changed

builtin/submodule--helper.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,13 +1587,12 @@ static int clone_submodule(const struct module_clone_data *clone_data,
15871587
char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
15881588
char *sm_alternate = NULL, *error_strategy = NULL;
15891589
struct child_process cp = CHILD_PROCESS_INIT;
1590-
const char *clone_data_path;
1590+
const char *clone_data_path = clone_data->path;
1591+
char *to_free = NULL;
15911592

15921593
if (!is_absolute_path(clone_data->path))
1593-
clone_data_path = xstrfmt("%s/%s", get_git_work_tree(),
1594-
clone_data->path);
1595-
else
1596-
clone_data_path = xstrdup(clone_data->path);
1594+
clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(),
1595+
clone_data->path);
15971596

15981597
if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
15991598
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,
16781677

16791678
free(sm_gitdir);
16801679
free(p);
1680+
free(to_free);
16811681
return 0;
16821682
}
16831683

t/t1500-rev-parse.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ test_description='test git rev-parse'
44
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
55
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
66

7+
TEST_PASSES_SANITIZE_LEAK=true
78
. ./test-lib.sh
89

910
test_one () {

t/t6008-rev-list-submodule.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test_description='git rev-list involving submodules that this repo has'
88
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
99
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1010

11+
TEST_PASSES_SANITIZE_LEAK=true
1112
. ./test-lib.sh
1213

1314
test_expect_success 'setup' '

t/t7414-submodule-mistakes.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='handling of common mistakes people may make with submodules'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
test_expect_success 'create embedded repository' '

t/t7506-status-submodule.sh

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

33
test_description='git status for submodule'
44

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

78
test_create_repo_with_commit () {

t/t7507-commit-verbose.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='verbose commit template'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
write_script "check-for-diff" <<\EOF &&

0 commit comments

Comments
 (0)