Skip to content

Commit 345e216

Browse files
avargitster
authored andcommitted
builtin/merge.c: use fixed strings, not "strbuf", fix leak
Follow-up 465028e (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 <[email protected]> Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8155961 commit 345e216

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

builtin/merge.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
15601560
!common->next &&
15611561
oideq(&common->item->object.oid, &head_commit->object.oid)) {
15621562
/* 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";
15641566
struct commit *commit;
15651567

15661568
if (verbosity >= 0) {
@@ -1570,10 +1572,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
15701572
find_unique_abbrev(&remoteheads->item->object.oid,
15711573
DEFAULT_ABBREV));
15721574
}
1573-
strbuf_addstr(&msg, "Fast-forward");
1574-
if (have_message)
1575-
strbuf_addstr(&msg,
1576-
" (no commit created; -m option ignored)");
15771575
commit = remoteheads->item;
15781576
if (!commit) {
15791577
ret = 1;
@@ -1592,9 +1590,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
15921590
goto done;
15931591
}
15941592

1595-
finish(head_commit, remoteheads, &commit->object.oid, msg.buf);
1593+
finish(head_commit, remoteheads, &commit->object.oid, msg);
15961594
remove_merge_branch_state(the_repository);
1597-
strbuf_release(&msg);
15981595
goto done;
15991596
} else if (!remoteheads->next && common->next)
16001597
;

t/t6439-merge-co-error-msgs.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='unpack-trees error messages'
55
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
66
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011

0 commit comments

Comments
 (0)