strbuf_attach: fix call sites to pass correct alloc

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>

Vaidas Pilkauskas committed Mar 17, 2026 at 13:00 UTC a4fddb01c5bd0ecbd5e297ee571ad29ca62bf940
5 files changed +5 -5
builtin/am.c
+1 -1
@@ -1188,7 +1188,7 @@ static void am_append_signoff(struct am_state *state)
1188 {
1189 struct strbuf sb = STRBUF_INIT;
1190
1191 - strbuf_attach(&sb, state->msg, state->msg_len, state->msg_len);
1191 + strbuf_attach(&sb, state->msg, state->msg_len, state->msg_len + 1);
1192 append_signoff(&sb, 0, 0);
1193 state->msg = strbuf_detach(&sb, &state->msg_len);
1194 }
builtin/fast-import.c
+1 -1
@@ -3246,7 +3246,7 @@ static void cat_blob(struct object_entry *oe, struct object_id *oid)
3246 cat_blob_write("\n", 1);
3247 if (oe && oe->pack_id == pack_id) {
3248 last_blob.offset = oe->idx.offset;
3249 - strbuf_attach(&last_blob.data, buf, size, size);
3249 + strbuf_attach(&last_blob.data, buf, size, size + 1);
3250 last_blob.depth = oe->depth;
3251 } else
3252 free(buf);
mailinfo.c
+1 -1
@@ -470,7 +470,7 @@ static int convert_to_utf8(struct mailinfo *mi,
470 return error("cannot convert from %s to %s",
471 charset, mi->metainfo_charset);
472 }
473 - strbuf_attach(line, out, out_len, out_len);
473 + strbuf_attach(line, out, out_len, out_len + 1);
474 return 0;
475 }
476
refs/files-backend.c
+1 -1
@@ -1806,7 +1806,7 @@ static int commit_ref(struct ref_lock *lock)
1806 size_t len = strlen(path);
1807 struct strbuf sb_path = STRBUF_INIT;
1808
1809 - strbuf_attach(&sb_path, path, len, len);
1809 + strbuf_attach(&sb_path, path, len, len + 1);
1810
1811 /*
1812 * If this fails, commit_lock_file() will also fail
trailer.c
+1 -1
@@ -1009,7 +1009,7 @@ static struct trailer_block *trailer_block_get(const struct process_trailer_opti
1009 for (ptr = trailer_lines; *ptr; ptr++) {
1010 if (last && isspace((*ptr)->buf[0])) {
1011 struct strbuf sb = STRBUF_INIT;
1012 - strbuf_attach(&sb, *last, strlen(*last), strlen(*last));
1012 + strbuf_attach(&sb, *last, strlen(*last), strlen(*last) + 1);
1013 strbuf_addbuf(&sb, *ptr);
1014 *last = strbuf_detach(&sb, NULL);
1015 continue;