diff options
author | Junio C Hamano <gitster@pobox.com> | 2011-09-02 13:18:29 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2011-09-02 13:18:29 -0700 |
commit | 5e2b3d7c6741fdd65e195fbe3af98b6d8d2fa551 (patch) | |
tree | 013425b27ecd20866abafa457b698f62c9a41fd1 | |
parent | Merge branch 'tr/maint-ident-to-git-memmove' (diff) | |
parent | strbuf_grow(): maintain nul-termination even for new buffer (diff) | |
download | tgif-5e2b3d7c6741fdd65e195fbe3af98b6d8d2fa551.tar.xz |
Merge branch 'tr/maint-strbuf-grow-nul-termination'
* tr/maint-strbuf-grow-nul-termination:
strbuf_grow(): maintain nul-termination even for new buffer
-rw-r--r-- | strbuf.c | 9 |
1 files changed, 5 insertions, 4 deletions
@@ -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) |