]> git.ipfire.org Git - thirdparty/git.git/commitdiff
strbuf: pass correct alloc to strbuf_attach() in strbuf_reencode()
authorVaidas Pilkauskas <vaidas.pilkauskas@shopify.com>
Tue, 17 Mar 2026 13:00:33 +0000 (13:00 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Mar 2026 16:14:19 +0000 (09:14 -0700)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
strbuf.c

index 59678bf5b03e0b2c9884283771e23c36224d91c7..bad76ea64dbe1cfc502f31fd91350c25f461730b 100644 (file)
--- 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;
 }