Skip to content

Commit 145fd04

Browse files
strbuf: fix incorrect alloc size in strbuf_reencode()
The strbuf_reencode() function incorrectly passes the string length as the allocation size to strbuf_attach(), when it should pass length + 1 to account for the null terminator. The reencode_string_len() function allocates len + 1 bytes (including the null terminator) and returns the string length (excluding the null terminator) via the len parameter. However, strbuf_reencode() then calls strbuf_attach() with this length value as both the len and alloc parameters: strbuf_attach(sb, out, len, len); This is incorrect because strbuf_attach()'s alloc parameter should reflect the actual allocated buffer size, which includes space for the null terminator. This could lead to incorrect memory management in code that relies on sb->alloc being accurate. Fix by passing len + 1 as the alloc parameter: strbuf_attach(sb, out, len, len + 1); Signed-off-by: Vaidas Pilkauskas <vaidas.pilkauskas@shopify.com>
1 parent 453e7b7 commit 145fd04

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)