X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=strbuf.c;h=cd576dc290a476490da1ae0478d1c931d055a78d;hb=8c74ef1e972df2aebc403ee807992017e9cc6838;hp=09c43ae59a7d4715c26f13d72ca37bc759d1c76d;hpb=d6ad4ff1202bd79f6470e8699bbd16d6ceb6ae98;p=git.git diff --git a/strbuf.c b/strbuf.c index 09c43ae59..cd576dc29 100644 --- a/strbuf.c +++ b/strbuf.c @@ -30,10 +30,8 @@ void strbuf_init(struct strbuf *sb, size_t hint) { sb->alloc = sb->len = 0; sb->buf = strbuf_slopbuf; - if (hint) { + if (hint) strbuf_grow(sb, hint); - sb->buf[0] = '\0'; - } } void strbuf_release(struct strbuf *sb) @@ -65,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)