Skip to content

Commit 8fbe2df

Browse files
vaidas-shopifygitster
authored andcommitted
strbuf: replace strbuf_grow() in strbuf_attach() with BUG() check
strbuf_attach() documents that alloc must be larger than len, as the buffer must have room for the NUL terminator. Replace the strbuf_grow(sb, 0) call, which was silently reallocating when alloc <= len, with an explicit BUG() to enforce this contract and write the NUL terminator directly. Signed-off-by: Vaidas Pilkauskas <vaidas.pilkauskas@shopify.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a41c247 commit 8fbe2df

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

strbuf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ char *strbuf_detach(struct strbuf *sb, size_t *sz)
9595

9696
void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
9797
{
98+
if (alloc <= len)
99+
BUG("alloc must be larger than len");
98100
strbuf_release(sb);
99101
sb->buf = buf;
100102
sb->len = len;
101103
sb->alloc = alloc;
102-
strbuf_grow(sb, 0);
103104
sb->buf[sb->len] = '\0';
104105
}
105106

0 commit comments

Comments
 (0)