]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/log: fix leaking branch name when creating cover letters
authorPatrick Steinhardt <ps@pks.im>
Thu, 1 Aug 2024 10:38:16 +0000 (12:38 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 1 Aug 2024 15:47:35 +0000 (08:47 -0700)
When calling `make_cover_letter()` without a branch name, we try to
derive the branch name by calling `find_branch_name()`. But while this
function returns an allocated string, we never free the result and thus
have a memory leak. Fix this.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/log.c
t/t3206-range-diff.sh

index 4d4b60caa76a059fef5e7d95d1610331147708e5..a73a76760658fe8870cf14c540c229c748b81077 100644 (file)
@@ -1434,6 +1434,7 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
        int need_8bit_cte = 0;
        struct pretty_print_context pp = {0};
        struct commit *head = list[0];
+       char *to_free = NULL;
 
        if (!cmit_fmt_is_mail(rev->commit_format))
                die(_("cover letter needs email format"));
@@ -1455,7 +1456,7 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
        }
 
        if (!branch_name)
-               branch_name = find_branch_name(rev);
+               branch_name = to_free = find_branch_name(rev);
 
        pp.fmt = CMIT_FMT_EMAIL;
        pp.date_mode.type = DATE_RFC2822;
@@ -1466,6 +1467,7 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
                           encoding, need_8bit_cte, cfg);
        fprintf(rev->diffopt.file, "%s\n", sb.buf);
 
+       free(to_free);
        free(pp.after_subject);
        strbuf_release(&sb);
 
index a767c3520ef6b77e1cbf0d136b448b7f89b2ec3e..973e20254b813b7997cb8b75ab19924276521887 100755 (executable)
@@ -5,6 +5,7 @@ test_description='range-diff tests'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 # Note that because of the range-diff's heuristics, test_commit does more