Merge branch 'tr/maint-strbuf-grow-nul-termination'
authorJunio C Hamano <gitster@pobox.com>
Fri, 2 Sep 2011 20:18:29 +0000 (13:18 -0700)
committerJunio 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

1  2 
strbuf.c
diff --combined strbuf.c
index 1a7df12e8f233863cd931a960d453d54050f5584,cd576dc290a476490da1ae0478d1c931d055a78d..9ff1b597c995780026a32a92fab78a780d60329a
+++ b/strbuf.c
@@@ -30,10 -30,8 +30,8 @@@ void strbuf_init(struct strbuf *sb, siz
  {
        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 +63,15 @@@ void strbuf_attach(struct strbuf *sb, v
  
  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)
@@@ -103,27 -104,24 +104,27 @@@ void strbuf_ltrim(struct strbuf *sb
        sb->buf[sb->len] = '\0';
  }
  
 -struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
 +struct strbuf **strbuf_split_buf(const char *str, size_t slen, int delim, int max)
  {
        int alloc = 2, pos = 0;
 -      char *n, *p;
 +      const char *n, *p;
        struct strbuf **ret;
        struct strbuf *t;
  
        ret = xcalloc(alloc, sizeof(struct strbuf *));
 -      p = n = sb->buf;
 -      while (n < sb->buf + sb->len) {
 +      p = n = str;
 +      while (n < str + slen) {
                int len;
 -              n = memchr(n, delim, sb->len - (n - sb->buf));
 +              if (max <= 0 || pos + 1 < max)
 +                      n = memchr(n, delim, slen - (n - str));
 +              else
 +                      n = NULL;
                if (pos + 1 >= alloc) {
                        alloc = alloc * 2;
                        ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
                }
                if (!n)
 -                      n = sb->buf + sb->len - 1;
 +                      n = str + slen - 1;
                len = n - p + 1;
                t = xmalloc(sizeof(struct strbuf));
                strbuf_init(t, len);