Commit 145fd04
committed
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | | - | |
| 171 | + | |
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
| |||
0 commit comments