strbuf: release memory on read error in strbuf_read_once()
[gitweb.git] / strbuf.c
index ace58e7367300975e118660ed2004c7d0885481c..0809bca2562a4c970e0405b69de3ed095f3c6e26 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -393,12 +393,15 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 
 ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
 {
+       size_t oldalloc = sb->alloc;
        ssize_t cnt;
 
        strbuf_grow(sb, hint ? hint : 8192);
        cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
        if (cnt > 0)
                strbuf_setlen(sb, sb->len + cnt);
+       else if (oldalloc == 0)
+               strbuf_release(sb);
        return cnt;
 }