submodule--helper: fix "sm_path" and other "module_cb_list" leaks

Fix leaks in "struct module_cb_list" and the "struct module_cb" which it contains, these fix leaks in e83e3333b57 (submodule: port submodule subcommand 'summary' from shell to C, 2020-08-13). The "sm_path" should always have been a "char *", not a "const char *", we always create it with xstrdup(). We can't mark any tests passing passing with SANITIZE=leak using "TEST_PASSES_SANITIZE_LEAK=true" as a result of this change, but "t7401-submodule-summary.sh" gets closer to passing as a result of this change. 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>

Ævar Arnfjörð Bjarmason committed Sep 1, 2022 at 01:14 UTC 980416e469ad2ed0d38630cf4c25390ae9731416
2 files changed +21 -1
builtin/submodule--helper.c
+20 -1
@@ -748,16 +748,34 @@ struct module_cb {
748 struct object_id oid_src;
749 struct object_id oid_dst;
750 char status;
751 - const char *sm_path;
751 + char *sm_path;
752 };
753 #define MODULE_CB_INIT { 0 }
754
755 +static void module_cb_release(struct module_cb *mcb)
756 +{
757 + free(mcb->sm_path);
758 +}
759 +
760 struct module_cb_list {
761 struct module_cb **entries;
762 int alloc, nr;
763 };
764 #define MODULE_CB_LIST_INIT { 0 }
765
766 +static void module_cb_list_release(struct module_cb_list *mcbl)
767 +{
768 + int i;
769 +
770 + for (i = 0; i < mcbl->nr; i++) {
771 + struct module_cb *mcb = mcbl->entries[i];
772 +
773 + module_cb_release(mcb);
774 + free(mcb);
775 + }
776 + free(mcbl->entries);
777 +}
778 +
779 struct summary_cb {
780 int argc;
781 const char **argv;
@@ -1101,6 +1119,7 @@ static int compute_summary_module_list(struct object_id *head_oid,
1119 cleanup:
1120 strvec_clear(&diff_args);
1121 release_revisions(&rev);
1122 + module_cb_list_release(&list);
1123 return ret;
1124 }
1125
t/t7401-submodule-summary.sh
+1
@@ -17,6 +17,7 @@ This test script tries to verify the sanity of summary subcommand of git submodu
17 # various reasons, one of them being that there are lots of commands taking place
18 # outside of 'test_expect_success' block, which is no longer in good-style.
19
20 +TEST_PASSES_SANITIZE_LEAK=true
21 . ./test-lib.sh
22
23 add_file () {