builtin/log: fix leaking branch name when creating cover letters

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>

Patrick Steinhardt committed Aug 1, 2024 at 12:38 UTC 7935a026130f7dd430539685de0742dabb8e6047
2 files changed +4 -1
builtin/log.c
+3 -1
@@ -1434,6 +1434,7 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
1434 int need_8bit_cte = 0;
1435 struct pretty_print_context pp = {0};
1436 struct commit *head = list[0];
1437 + char *to_free = NULL;
1438
1439 if (!cmit_fmt_is_mail(rev->commit_format))
1440 die(_("cover letter needs email format"));
@@ -1455,7 +1456,7 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
1456 }
1457
1458 if (!branch_name)
1458 - branch_name = find_branch_name(rev);
1459 + branch_name = to_free = find_branch_name(rev);
1460
1461 pp.fmt = CMIT_FMT_EMAIL;
1462 pp.date_mode.type = DATE_RFC2822;
@@ -1466,6 +1467,7 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
1467 encoding, need_8bit_cte, cfg);
1468 fprintf(rev->diffopt.file, "%s\n", sb.buf);
1469
1470 + free(to_free);
1471 free(pp.after_subject);
1472 strbuf_release(&sb);
1473
t/t3206-range-diff.sh
+1
@@ -5,6 +5,7 @@ test_description='range-diff 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
11 # Note that because of the range-diff's heuristics, test_commit does more