]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/merge.c: use fixed strings, not "strbuf", fix leak
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Mon, 6 Feb 2023 23:07:48 +0000 (00:07 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 6 Feb 2023 23:34:39 +0000 (15:34 -0800)
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>
builtin/merge.c
t/t6439-merge-co-error-msgs.sh

index 74de2ebd2b33f967ff85988779bbf85819fe9a3c..32733e551d4c1a99c4a6302276b4038d6b6ba44f 100644 (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)
                ;
index 52cf0c87690be83e758b97c12b0ab00fa77613a2..0cbec57cdabc48b7cd952a6fbf5458391b44a645 100755 (executable)
@@ -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