Skip to content

Commit 5cca114

Browse files
pks-tgitster
authored andcommitted
submodule: fix leaking submodule entry list
The submodule entry list returned by `submodules_of_tree()` is never completely free'd by its only caller. Introduce a new function that free's the list for us and call it. While at it, also fix the leaking `branch_point` string. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 666643f commit 5cca114

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

branch.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ static int submodule_create_branch(struct repository *r,
738738

739739
strbuf_release(&child_err);
740740
strbuf_release(&out_buf);
741+
free(out_prefix);
741742
return ret;
742743
}
743744

@@ -794,7 +795,7 @@ void create_branches_recursively(struct repository *r, const char *name,
794795
create_branch(r, name, start_committish, force, 0, reflog, quiet,
795796
BRANCH_TRACK_NEVER, dry_run);
796797
if (dry_run)
797-
return;
798+
goto out;
798799
/*
799800
* NEEDSWORK If tracking was set up in the superproject but not the
800801
* submodule, users might expect "git branch --recurse-submodules" to
@@ -815,8 +816,11 @@ void create_branches_recursively(struct repository *r, const char *name,
815816
die(_("submodule '%s': cannot create branch '%s'"),
816817
submodule_entry_list.entries[i].submodule->name,
817818
name);
818-
repo_clear(submodule_entry_list.entries[i].repo);
819819
}
820+
821+
out:
822+
submodule_entry_list_release(&submodule_entry_list);
823+
free(branch_point);
820824
}
821825

822826
void remove_merge_branch_state(struct repository *r)

submodule-config.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,9 @@ static void traverse_tree_submodules(struct repository *r,
901901
struct submodule_tree_entry *st_entry;
902902
struct name_entry name_entry;
903903
char *tree_path = NULL;
904+
char *tree_buf;
904905

905-
fill_tree_descriptor(r, &tree, treeish_name);
906+
tree_buf = fill_tree_descriptor(r, &tree, treeish_name);
906907
while (tree_entry(&tree, &name_entry)) {
907908
if (prefix)
908909
tree_path =
@@ -930,6 +931,8 @@ static void traverse_tree_submodules(struct repository *r,
930931
&name_entry.oid, out);
931932
free(tree_path);
932933
}
934+
935+
free(tree_buf);
933936
}
934937

935938
void submodules_of_tree(struct repository *r,
@@ -943,6 +946,16 @@ void submodules_of_tree(struct repository *r,
943946
traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
944947
}
945948

949+
void submodule_entry_list_release(struct submodule_entry_list *list)
950+
{
951+
for (size_t i = 0; i < list->entry_nr; i++) {
952+
free(list->entries[i].name_entry);
953+
repo_clear(list->entries[i].repo);
954+
free(list->entries[i].repo);
955+
}
956+
free(list->entries);
957+
}
958+
946959
void submodule_free(struct repository *r)
947960
{
948961
if (r->submodule_cache)

submodule-config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,7 @@ struct submodule_entry_list {
136136
void submodules_of_tree(struct repository *r,
137137
const struct object_id *treeish_name,
138138
struct submodule_entry_list *ret);
139+
140+
void submodule_entry_list_release(struct submodule_entry_list *list);
141+
139142
#endif /* SUBMODULE_CONFIG_H */

t/t3207-branch-submodule.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='git branch submodule tests'
55
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
66
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910
. "$TEST_DIRECTORY"/lib-rebase.sh
1011

0 commit comments

Comments
 (0)