diff options
author | Jeff King <peff@peff.net> | 2017-09-27 02:01:07 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-09-27 15:45:24 +0900 |
commit | 90dca6710e6e5aad5d78d0cd006c3adadb65524d (patch) | |
tree | 60d3225445c7321ca07f4a154db46e4e02fda125 /sha1_file.c | |
parent | 61d36330b422237b6be9581cdbade07782ab61a8 (diff) | |
download | git-90dca6710e6e5aad5d78d0cd006c3adadb65524d.tar.gz |
avoid looking at errno for short read_in_full() returns
When a caller tries to read a particular set of bytes via
read_in_full(), there are three possible outcomes:
1. An error, in which case -1 is returned and errno is
set.
2. A short read, in which fewer bytes are returned and
errno is unspecified (we never saw a read error, so we
may have some random value from whatever syscall failed
last).
3. The full read completed successfully.
Many callers handle cases 1 and 2 together by just checking
the result against the requested size. If their combined
error path looks at errno (e.g., by calling die_errno), they
may report a nonsense value.
Let's fix these sites by having them distinguish between the
two error cases. That avoids the random errno confusion, and
lets us give more detailed error messages.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'sha1_file.c')
-rw-r--r-- | sha1_file.c | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/sha1_file.c b/sha1_file.c index dd7cbe52ef..11346cf6d8 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -1757,10 +1757,15 @@ static int index_core(unsigned char *sha1, int fd, size_t size, ret = index_mem(sha1, "", size, type, path, flags); } else if (size <= SMALL_FILE_SIZE) { char *buf = xmalloc(size); - if (size == read_in_full(fd, buf, size)) - ret = index_mem(sha1, buf, size, type, path, flags); + ssize_t read_result = read_in_full(fd, buf, size); + if (read_result < 0) + ret = error_errno("read error while indexing %s", + path ? path : "<unknown>"); + else if (read_result != size) + ret = error("short read while indexing %s", + path ? path : "<unknown>"); else - ret = error_errno("short read"); + ret = index_mem(sha1, buf, size, type, path, flags); free(buf); } else { void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |