Skip to content

Commit c3ff8f6

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9752ad0 commit c3ff8f6

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

strbuf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,15 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
393393

394394
ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
395395
{
396+
size_t oldalloc = sb->alloc;
396397
ssize_t cnt;
397398

398399
strbuf_grow(sb, hint ? hint : 8192);
399400
cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
400401
if (cnt > 0)
401402
strbuf_setlen(sb, sb->len + cnt);
403+
else if (oldalloc == 0)
404+
strbuf_release(sb);
402405
return cnt;
403406
}
404407

0 commit comments

Comments
 (0)