builtin/merge.c: use fixed strings, not "strbuf", fix leak

Follow-up 465028e0e25 (merge: add missing strbuf_release(), 2021-10-07) and address the "msg" memory leak in this block. We could free "&msg" before the "goto done" here, but even better is to avoid allocating it in the first place. By repeating the "Fast-forward" string here we can avoid using a "struct strbuf" altogether. Suggested-by: René Scharfe <l.s.r@web.de> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Feb 7, 2023 at 00:07 UTC 345e216f63a71ab58f81cf4eae1eb2a458304ca1
2 files changed +5 -7
builtin/merge.c
+4 -7
@@ -1560,7 +1560,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1560 !common->next &&
1561 oideq(&common->item->object.oid, &head_commit->object.oid)) {
1562 /* Again the most common case of merging one remote. */
1563 - struct strbuf msg = STRBUF_INIT;
1563 + const char *msg = have_message ?
1564 + "Fast-forward (no commit created; -m option ignored)" :
1565 + "Fast-forward";
1566 struct commit *commit;
1567
1568 if (verbosity >= 0) {
@@ -1570,10 +1572,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1572 find_unique_abbrev(&remoteheads->item->object.oid,
1573 DEFAULT_ABBREV));
1574 }
1573 - strbuf_addstr(&msg, "Fast-forward");
1574 - if (have_message)
1575 - strbuf_addstr(&msg,
1576 - " (no commit created; -m option ignored)");
1575 commit = remoteheads->item;
1576 if (!commit) {
1577 ret = 1;
@@ -1592,9 +1590,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1590 goto done;
1591 }
1592
1595 - finish(head_commit, remoteheads, &commit->object.oid, msg.buf);
1593 + finish(head_commit, remoteheads, &commit->object.oid, msg);
1594 remove_merge_branch_state(the_repository);
1597 - strbuf_release(&msg);
1595 goto done;
1596 } else if (!remoteheads->next && common->next)
1597 ;
t/t6439-merge-co-error-msgs.sh
+1
@@ -5,6 +5,7 @@ test_description='unpack-trees error messages'
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