Skip to content

Commit a41c247

Browse files
vaidas-shopifygitster
authored andcommitted
strbuf_attach: fix all 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. Prepare for changing that by fixing call sites to pass the correct alloc. - mailinfo, am, refs/files-backend, fast-import, trailer: pass len+1 when the buffer is a NUL-terminated string (or from strbuf_detach). - rerere, apply: ll_merge returns a buffer with exactly result.size bytes (no extra NUL). Use strbuf_add() to copy and NUL-terminate into the strbuf, then free the merge result, so alloc is correct. Signed-off-by: Vaidas Pilkauskas <vaidas.pilkauskas@shopify.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 4535082 commit a41c247

7 files changed

Lines changed: 9 additions & 7 deletions

File tree

apply.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3589,7 +3589,8 @@ static int three_way_merge(struct apply_state *state,
35893589
return -1;
35903590
}
35913591
image_clear(image);
3592-
strbuf_attach(&image->buf, result.ptr, result.size, result.size);
3592+
strbuf_add(&image->buf, result.ptr, result.size);
3593+
free(result.ptr);
35933594

35943595
return status;
35953596
}

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
@@ -3246,7 +3246,7 @@ static void cat_blob(struct object_entry *oe, struct object_id *oid)
32463246
cat_blob_write("\n", 1);
32473247
if (oe && oe->pack_id == pack_id) {
32483248
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);
32503250
last_blob.depth = oe->depth;
32513251
} else
32523252
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
@@ -1806,7 +1806,7 @@ static int commit_ref(struct ref_lock *lock)
18061806
size_t len = strlen(path);
18071807
struct strbuf sb_path = STRBUF_INIT;
18081808

1809-
strbuf_attach(&sb_path, path, len, len);
1809+
strbuf_attach(&sb_path, path, len, len + 1);
18101810

18111811
/*
18121812
* If this fails, commit_lock_file() will also fail

rerere.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,8 @@ static int handle_cache(struct index_state *istate,
10311031
else
10321032
io.io.output = NULL;
10331033
strbuf_init(&io.input, 0);
1034-
strbuf_attach(&io.input, result.ptr, result.size, result.size);
1034+
strbuf_add(&io.input, result.ptr, result.size);
1035+
free(result.ptr);
10351036

10361037
/*
10371038
* Grab the conflict ID and optionally write the original

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)