[PATCH v5 1/4] strbuf: pass correct alloc to strbuf_attach() in strbuf_reencode()
- From
Vaidas Pilkauskas via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Feb 23, 2026, 14:20 UTC
- Message-ID
- <7ec2d6644785827c047a55a99b213468583c6c1a.1771856405.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2008.v5.git.1771856405.gitgitgadget@gmail.com>
From: Vaidas Pilkauskas <vaidas.pilkauskas@shopify.com>
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> --- strbuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/strbuf.c b/strbuf.c index 3939863cf3..3e04addc22 100644 --- a/strbuf.c +++ b/strbuf.c @@ -168,7 +168,7 @@ int strbuf_reencode(struct strbuf *sb, const char *from, const char *to) if (!out) return -1; - strbuf_attach(sb, out, len, len); + strbuf_attach(sb, out, len, len + 1); return 0; }
-- gitgitgadget