]> git.ipfire.org Git - thirdparty/git.git/commitdiff
strbuf_attach: fix call sites to pass correct alloc
authorVaidas Pilkauskas <vaidas.pilkauskas@shopify.com>
Tue, 17 Mar 2026 13:00:34 +0000 (13:00 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Mar 2026 16:14:19 +0000 (09:14 -0700)
strbuf_attach(sb, buf, len, alloc) requires alloc > len (the buffer
must have at least len+1 bytes to hold the NUL). Several call sites
passed alloc == len, relying on strbuf_grow(sb, 0) inside strbuf_attach
to reallocate. Fix these in mailinfo, am, refs/files-backend,
fast-import, and trailer by passing len+1 when the buffer is a
NUL-terminated string (or from strbuf_detach).

Signed-off-by: Vaidas Pilkauskas <vaidas.pilkauskas@shopify.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/am.c
builtin/fast-import.c
mailinfo.c
refs/files-backend.c
trailer.c

index b66a33d8a8899c6d5e0f9c05ea6f04873df420ae..66be33ab420ff6bcef0697cf780a08e037efd63e 100644 (file)
@@ -1188,7 +1188,7 @@ static void am_append_signoff(struct am_state *state)
 {
        struct strbuf sb = STRBUF_INIT;
 
-       strbuf_attach(&sb, state->msg, state->msg_len, state->msg_len);
+       strbuf_attach(&sb, state->msg, state->msg_len, state->msg_len + 1);
        append_signoff(&sb, 0, 0);
        state->msg = strbuf_detach(&sb, &state->msg_len);
 }
index b8a7757cfda943484bf42169850a32a616709d14..164d8a6198abc8da88ce7d35a176e5250d9ae8c0 100644 (file)
@@ -3246,7 +3246,7 @@ static void cat_blob(struct object_entry *oe, struct object_id *oid)
        cat_blob_write("\n", 1);
        if (oe && oe->pack_id == pack_id) {
                last_blob.offset = oe->idx.offset;
-               strbuf_attach(&last_blob.data, buf, size, size);
+               strbuf_attach(&last_blob.data, buf, size, size + 1);
                last_blob.depth = oe->depth;
        } else
                free(buf);
index 99ac596e096e70e11926eed2e930b8f94ddeb032..e52a35fde01023852360e3462f7a4a295799f046 100644 (file)
@@ -470,7 +470,7 @@ static int convert_to_utf8(struct mailinfo *mi,
                return error("cannot convert from %s to %s",
                             charset, mi->metainfo_charset);
        }
-       strbuf_attach(line, out, out_len, out_len);
+       strbuf_attach(line, out, out_len, out_len + 1);
        return 0;
 }
 
index 240d3c3b26e0b551d1fe61d102fbfd534173802e..bddc04099d0a4d027151102edc3bb0f49a47c073 100644 (file)
@@ -1806,7 +1806,7 @@ static int commit_ref(struct ref_lock *lock)
                size_t len = strlen(path);
                struct strbuf sb_path = STRBUF_INIT;
 
-               strbuf_attach(&sb_path, path, len, len);
+               strbuf_attach(&sb_path, path, len, len + 1);
 
                /*
                 * If this fails, commit_lock_file() will also fail
index 911a81ed993ec6aa802bcd0ecbb5a86e3e497d43..3afe368db03f9a864dba72f1b6defc545d29e56e 100644 (file)
--- a/trailer.c
+++ b/trailer.c
@@ -1009,7 +1009,7 @@ static struct trailer_block *trailer_block_get(const struct process_trailer_opti
        for (ptr = trailer_lines; *ptr; ptr++) {
                if (last && isspace((*ptr)->buf[0])) {
                        struct strbuf sb = STRBUF_INIT;
-                       strbuf_attach(&sb, *last, strlen(*last), strlen(*last));
+                       strbuf_attach(&sb, *last, strlen(*last), strlen(*last) + 1);
                        strbuf_addbuf(&sb, *ptr);
                        *last = strbuf_detach(&sb, NULL);
                        continue;