]> git.ipfire.org Git - thirdparty/git.git/commitdiff
submodule--helper: fix "sm_path" and other "module_cb_list" leaks
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Wed, 31 Aug 2022 23:14:15 +0000 (01:14 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 2 Sep 2022 16:18:12 +0000 (09:18 -0700)
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>
builtin/submodule--helper.c
t/t7401-submodule-summary.sh

index c95098e62155816afbb58af9e27802964ba967d7..3453faabd39fb6596f3cccf34e722e25dd6ac2b5 100644 (file)
@@ -748,16 +748,34 @@ struct module_cb {
        struct object_id oid_src;
        struct object_id oid_dst;
        char status;
-       const char *sm_path;
+       char *sm_path;
 };
 #define MODULE_CB_INIT { 0 }
 
+static void module_cb_release(struct module_cb *mcb)
+{
+       free(mcb->sm_path);
+}
+
 struct module_cb_list {
        struct module_cb **entries;
        int alloc, nr;
 };
 #define MODULE_CB_LIST_INIT { 0 }
 
+static void module_cb_list_release(struct module_cb_list *mcbl)
+{
+       int i;
+
+       for (i = 0; i < mcbl->nr; i++) {
+               struct module_cb *mcb = mcbl->entries[i];
+
+               module_cb_release(mcb);
+               free(mcb);
+       }
+       free(mcbl->entries);
+}
+
 struct summary_cb {
        int argc;
        const char **argv;
@@ -1101,6 +1119,7 @@ static int compute_summary_module_list(struct object_id *head_oid,
 cleanup:
        strvec_clear(&diff_args);
        release_revisions(&rev);
+       module_cb_list_release(&list);
        return ret;
 }
 
index 9c3cc4cf4046befd76e0239c831d61b33196ee86..542b3331a78f4db27dc5d99d3364f780f4db4548 100755 (executable)
@@ -17,6 +17,7 @@ This test script tries to verify the sanity of summary subcommand of git submodu
 # various reasons, one of them being that there are lots of commands taking place
 # outside of 'test_expect_success' block, which is no longer in good-style.
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 add_file () {