Skip to content

Commit 0dc214d

Browse files
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>
1 parent 6e76be1 commit 0dc214d

5 files changed

Lines changed: 5 additions & 5 deletions

File tree

builtin/am.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ static void am_append_signoff(struct am_state *state)
11881188
{
11891189
struct strbuf sb = STRBUF_INIT;
11901190

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);
11921192
append_signoff(&sb, 0, 0);
11931193
state->msg = strbuf_detach(&sb, &state->msg_len);
11941194
}

builtin/fast-import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3250,7 +3250,7 @@ static void cat_blob(struct object_entry *oe, struct object_id *oid)
32503250
cat_blob_write("\n", 1);
32513251
if (oe && oe->pack_id == pack_id) {
32523252
last_blob.offset = oe->idx.offset;
3253-
strbuf_attach(&last_blob.data, buf, size, size);
3253+
strbuf_attach(&last_blob.data, buf, size, size + 1);
32543254
last_blob.depth = oe->depth;
32553255
} else
32563256
free(buf);

mailinfo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ static int convert_to_utf8(struct mailinfo *mi,
470470
return error("cannot convert from %s to %s",
471471
charset, mi->metainfo_charset);
472472
}
473-
strbuf_attach(line, out, out_len, out_len);
473+
strbuf_attach(line, out, out_len, out_len + 1);
474474
return 0;
475475
}
476476

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,7 @@ static int commit_ref(struct ref_lock *lock)
18131813
size_t len = strlen(path);
18141814
struct strbuf sb_path = STRBUF_INIT;
18151815

1816-
strbuf_attach(&sb_path, path, len, len);
1816+
strbuf_attach(&sb_path, path, len, len + 1);
18171817

18181818
/*
18191819
* If this fails, commit_lock_file() will also fail

trailer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ static struct trailer_block *trailer_block_get(const struct process_trailer_opti
10091009
for (ptr = trailer_lines; *ptr; ptr++) {
10101010
if (last && isspace((*ptr)->buf[0])) {
10111011
struct strbuf sb = STRBUF_INIT;
1012-
strbuf_attach(&sb, *last, strlen(*last), strlen(*last));
1012+
strbuf_attach(&sb, *last, strlen(*last), strlen(*last) + 1);
10131013
strbuf_addbuf(&sb, *ptr);
10141014
*last = strbuf_detach(&sb, NULL);
10151015
continue;

0 commit comments

Comments
 (0)