summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2017-12-07 21:51:26 +0100
committerJunio C Hamano <gitster@pobox.com>2017-12-07 13:19:23 -0800
commitc3ff8f6c145638afe996b51e91375fd94cd064d0 (patch)
treebb9552a9296e9dbe1e81467e7e7511b574bdc4c1
parent9752ad0bb79f680bca48db7adc45338b298304b0 (diff)
downloadgit-rs/strbuf-read-once-reset-length.tar.gz
strbuf: release memory on read error in strbuf_read_once()rs/strbuf-read-once-reset-length
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>
-rw-r--r--strbuf.c3
1 files changed, 3 insertions, 0 deletions
diff --git a/strbuf.c b/strbuf.c
index ace58e7367..0809bca256 100644
--- 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;
}