summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2018-02-23 07:00:54 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-02-23 22:20:22 (GMT)
commit79f0ba1547a34b3a9791491f9bbfe4048026f373 (patch)
tree8b4e50bfcbf061ad5d5ac7726f54974b5e6a974f
parentffa952497288d29d94b16675c6789ef83850def3 (diff)
downloadgit-79f0ba1547a34b3a9791491f9bbfe4048026f373.zip
git-79f0ba1547a34b3a9791491f9bbfe4048026f373.tar.gz
git-79f0ba1547a34b3a9791491f9bbfe4048026f373.tar.bz2
strbuf_read_file(): preserve errno across close() call
If we encounter a read error, the user may want to report it by looking at errno. However, our close() call may clobber errno, leading to confusing results. Let's save and restore it in the error case. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--strbuf.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/strbuf.c b/strbuf.c
index 1df674e..5f138ed 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -612,14 +612,18 @@ ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
{
int fd;
ssize_t len;
+ int saved_errno;
fd = open(path, O_RDONLY);
if (fd < 0)
return -1;
len = strbuf_read(sb, fd, hint);
+ saved_errno = errno;
close(fd);
- if (len < 0)
+ if (len < 0) {
+ errno = saved_errno;
return -1;
+ }
return len;
}