path: root/builtin/submodule--helper.c
diff options
authorÆvar Arnfjörð Bjarmason <>2022-08-31 23:14:08 (GMT)
committerJunio C Hamano <>2022-09-02 16:18:12 (GMT)
commite77b3da6bb60e9af5963c9e42442afe53af1780b (patch)
tree044a80f2f7c651034027133b88f6573098607631 /builtin/submodule--helper.c
parent5647d743e39d51e79407869ba30decc64e736557 (diff)
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 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 <> Reviewed-by: Glen Choo <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/submodule--helper.c')
1 files changed, 5 insertions, 5 deletions
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 3a2a92d..d308b14 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -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(to_free);
return 0;