author | Junio C Hamano <gitster@pobox.com> | |
Fri, 2 Sep 2011 20:18:29 +0000 (13:18 -0700) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Fri, 2 Sep 2011 20:18:29 +0000 (13:18 -0700) |
* tr/maint-strbuf-grow-nul-termination:
strbuf_grow(): maintain nul-termination even for new buffer
strbuf_grow(): maintain nul-termination even for new buffer
strbuf.c | patch | blob | history |
diff --git a/strbuf.c b/strbuf.c
index 1a7df12e8f233863cd931a960d453d54050f5584..9ff1b597c995780026a32a92fab78a780d60329a 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
{
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)
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)