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

Follow-up 465028e0e2 (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>
This commit is contained in:
Ævar Arnfjörð Bjarmason 2023-02-07 00:07:48 +01:00 committed by Junio C Hamano
parent 81559612a9
commit 345e216f63
2 changed files with 5 additions and 7 deletions

View file

@ -1560,7 +1560,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
!common->next &&
oideq(&common->item->object.oid, &head_commit->object.oid)) {
/* Again the most common case of merging one remote. */
struct strbuf msg = STRBUF_INIT;
const char *msg = have_message ?
"Fast-forward (no commit created; -m option ignored)" :
"Fast-forward";
struct commit *commit;
if (verbosity >= 0) {
@ -1570,10 +1572,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
find_unique_abbrev(&remoteheads->item->object.oid,
DEFAULT_ABBREV));
}
strbuf_addstr(&msg, "Fast-forward");
if (have_message)
strbuf_addstr(&msg,
" (no commit created; -m option ignored)");
commit = remoteheads->item;
if (!commit) {
ret = 1;
@ -1592,9 +1590,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
goto done;
}
finish(head_commit, remoteheads, &commit->object.oid, msg.buf);
finish(head_commit, remoteheads, &commit->object.oid, msg);
remove_merge_branch_state(the_repository);
strbuf_release(&msg);
goto done;
} else if (!remoteheads->next && common->next)
;

View file

@ -5,6 +5,7 @@ test_description='unpack-trees error messages'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh