Skip to content

Commit 6e76be1

Browse files
strbuf: pass correct alloc to strbuf_attach() in strbuf_reencode()
reencode_string_len() allocates len+1 bytes (including the NUL) and returns the string length in len. strbuf_reencode() was calling strbuf_attach(sb, out, len, len), so alloc was one byte too small. strbuf_attach() then calls strbuf_grow(sb, 0). With alloc < len+1, ALLOC_GROW always reallocates, so we reallocated immediately after attach even when the strbuf was not extended further. Pass len+1 as the alloc argument so the existing buffer is reused and the reallocation is avoided. Signed-off-by: Vaidas Pilkauskas <vaidas.pilkauskas@shopify.com>
1 parent ca1db8a commit 6e76be1

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

strbuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ int strbuf_reencode(struct strbuf *sb, const char *from, const char *to)
168168
if (!out)
169169
return -1;
170170

171-
strbuf_attach(sb, out, len, len);
171+
strbuf_attach(sb, out, len, len + 1);
172172
return 0;
173173
}
174174

0 commit comments

Comments
 (0)