Code

strbuf_grow(): maintain nul-termination even for new buffer
[git.git] / strbuf.c
index 77444a94df3d4a0cda6403957fd13ea262d3ab24..cd576dc290a476490da1ae0478d1c931d055a78d 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -63,12 +63,15 @@ void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
 
 void strbuf_grow(struct strbuf *sb, size_t extra)
 {
+       int new_buf = !sb->alloc;
        if (unsigned_add_overflows(extra, 1) ||
            unsigned_add_overflows(sb->len, extra + 1))
                die("you want to use way too much memory");
-       if (!sb->alloc)
+       if (new_buf)
                sb->buf = NULL;
        ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
+       if (new_buf)
+               sb->buf[0] = '\0';
 }
 
 void strbuf_trim(struct strbuf *sb)