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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 30, 2024 at 11:13 UTC 5cca114973c602c9f8516e9ed34c76fd75fb999e
4 files changed +24 -3
branch.c
+6 -2
@@ -738,6 +738,7 @@ static int submodule_create_branch(struct repository *r,
738
739 strbuf_release(&child_err);
740 strbuf_release(&out_buf);
741 + free(out_prefix);
742 return ret;
743 }
744
@@ -794,7 +795,7 @@ void create_branches_recursively(struct repository *r, const char *name,
795 create_branch(r, name, start_committish, force, 0, reflog, quiet,
796 BRANCH_TRACK_NEVER, dry_run);
797 if (dry_run)
797 - return;
798 + goto out;
799 /*
800 * NEEDSWORK If tracking was set up in the superproject but not the
801 * submodule, users might expect "git branch --recurse-submodules" to
@@ -815,8 +816,11 @@ void create_branches_recursively(struct repository *r, const char *name,
816 die(_("submodule '%s': cannot create branch '%s'"),
817 submodule_entry_list.entries[i].submodule->name,
818 name);
818 - repo_clear(submodule_entry_list.entries[i].repo);
819 }
820 +
821 +out:
822 + submodule_entry_list_release(&submodule_entry_list);
823 + free(branch_point);
824 }
825
826 void remove_merge_branch_state(struct repository *r)
submodule-config.c
+14 -1
@@ -901,8 +901,9 @@ static void traverse_tree_submodules(struct repository *r,
901 struct submodule_tree_entry *st_entry;
902 struct name_entry name_entry;
903 char *tree_path = NULL;
904 + char *tree_buf;
905
905 - fill_tree_descriptor(r, &tree, treeish_name);
906 + tree_buf = fill_tree_descriptor(r, &tree, treeish_name);
907 while (tree_entry(&tree, &name_entry)) {
908 if (prefix)
909 tree_path =
@@ -930,6 +931,8 @@ static void traverse_tree_submodules(struct repository *r,
931 &name_entry.oid, out);
932 free(tree_path);
933 }
934 +
935 + free(tree_buf);
936 }
937
938 void submodules_of_tree(struct repository *r,
@@ -943,6 +946,16 @@ void submodules_of_tree(struct repository *r,
946 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
947 }
948
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 +
959 void submodule_free(struct repository *r)
960 {
961 if (r->submodule_cache)
submodule-config.h
+3
@@ -136,4 +136,7 @@ struct submodule_entry_list {
136 void submodules_of_tree(struct repository *r,
137 const struct object_id *treeish_name,
138 struct submodule_entry_list *ret);
139 +
140 +void submodule_entry_list_release(struct submodule_entry_list *list);
141 +
142 #endif /* SUBMODULE_CONFIG_H */
t/t3207-branch-submodule.sh
+1
@@ -5,6 +5,7 @@ test_description='git branch submodule tests'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10 . "$TEST_DIRECTORY"/lib-rebase.sh
11