Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

read_in_full: always report errors

The read_in_full function repeatedly calls read() to fill a
buffer. If the first read() returns an error, we notify the
caller by returning the error. However, if we read some data
and then get an error on a subsequent read, we simply return
the amount of data that we did read, and the caller is
unaware of the error.

This makes the tradeoff that seeing the partial data is more
important than the fact that an error occurred. In practice,
this is generally not the case; we care more if an error
occurred, and should throw away any partial data.

I audited the current callers. In most cases, this will make
no difference at all, as they do:

  if (read_in_full(fd, buf, size) != size)
	  error("short read");

However, it will help in a few cases:

  1. In sha1_file.c:index_stream, we would fail to notice
     errors in the incoming stream.

  2. When reading symbolic refs in resolve_ref, we would
     fail to notice errors and potentially use a truncated
     ref name.

  3. In various places, we will get much better error
     messages. For example, callers of safe_read would
     erroneously print "the remote end hung up unexpectedly"
     instead of showing the read error.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information...
commit 56d7c27af1f8aa7519f165f6c022732e64db3716 1 parent 09ffc70
Jeff King peff authored gitster committed

Showing 1 changed file with 4 additions and 2 deletions. Show diff stats Hide diff stats

  1. +4 2 wrapper.c
6 wrapper.c
@@ -148,8 +148,10 @@ ssize_t read_in_full(int fd, void *buf, size_t count)
148 148
149 149 while (count > 0) {
150 150 ssize_t loaded = xread(fd, p, count);
151   - if (loaded <= 0)
152   - return total ? total : loaded;
  151 + if (loaded < 0)
  152 + return -1;
  153 + if (loaded == 0)
  154 + return total;
153 155 count -= loaded;
154 156 p += loaded;
155 157 total += loaded;

0 comments on commit 56d7c27

Please sign in to comment.
Something went wrong with that request. Please try again.