strbuf: release memory on read error in strbuf_read_once()
If other strbuf add functions cause the first allocation and subsequently encounter an error then they release the memory, restoring the pristine state of the strbuf. That simplifies error handling for callers. Do the same in strbuf_read_once(), and do it also in case no bytes were read -- which may or may not be an error as well, depending on the caller. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Dec 7, 2017 at 21:51 UTC
c3ff8f6c145638afe996b51e91375fd94cd064d0
1 file changed
+3
strbuf.c
+3
@@ -393,12 +393,15 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
393
394
ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
395
{
396
+ size_t oldalloc = sb->alloc;
397
ssize_t cnt;
398
399
strbuf_grow(sb, hint ? hint : 8192);
400
cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
401
if (cnt > 0)
402
strbuf_setlen(sb, sb->len + cnt);
403
+ else if (oldalloc == 0)
404
+ strbuf_release(sb);
405
return cnt;
406
}
407