Code

strbuf utils: Fixed resizing in append.
authorSebastian Harl <sh@tokkee.org>
Fri, 16 May 2014 06:48:40 +0000 (08:48 +0200)
committerSebastian Harl <sh@tokkee.org>
Fri, 16 May 2014 06:48:40 +0000 (08:48 +0200)
Previously, resizing happened "too early" which may possibly generate
avoidable memory churn.

src/utils/strbuf.c

index 6c881be18e4975517f4c5080747e5d6f83a183f1..0474677296c31f9c1fcce3ded0a0207fde621c8a 100644 (file)
@@ -154,8 +154,8 @@ sdb_strbuf_vappend(sdb_strbuf_t *strbuf, const char *fmt, va_list ap)
        }
 
        /* 'status' does not include nul-byte */
-       if ((size_t)status >= strbuf->size - strbuf->pos - 1) {
-               if (strbuf_resize(strbuf, strbuf->size + (size_t)status + 1)) {
+       if ((size_t)status >= strbuf->size - strbuf->pos) {
+               if (strbuf_resize(strbuf, strbuf->pos + (size_t)status + 1)) {
                        va_end(aq);
                        return -1;
                }