git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jonathan Nieder <jrnieder@gmail.com>
To: Jeff King <peff@peff.net>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 3/7] read_in_full: reset errno before reading
Date: Mon, 25 Sep 2017 15:09:14 -0700	[thread overview]
Message-ID: <20170925220914.GB27425@aiede.mtv.corp.google.com> (raw)
In-Reply-To: <20170925202916.4tqo4gttrsoy7kai@sigill.intra.peff.net>

Jeff King wrote:

> In an ideal world, callers would always distinguish between
> these cases and give a useful message for each. But as an
> easy way to make our imperfect world better, let's reset
> errno to a known value. The best we can do is "0", which
> will yield something like:
>
>   unable to read: Success
>
> That's not great, but at least it's deterministic and makes
> it clear that we didn't see an error from read().

Yuck.  Can we set errno to something more specific instead?

read(2) also doesn't promise not to clobber errno on success.

How about something like the following?

-- >8 --
Subject: read_in_full: set errno for partial reads

Many callers of read_in_full() complain when we do not read their full
byte-count. But a check like:

  if (read_in_full(fd, buf, len) != len)
	return error_errno("unable to read");

conflates two problem conditions:

  1. A real error from read().
  2. There were fewer than "len" bytes available.

In the first case, showing the user strerror(errno) is useful. In the
second, we may see a random errno that was set by some previous system
call.

In an ideal world, callers would always distinguish between these
cases and give a useful message for each. But as an easy way to make
our imperfect world better, let's set errno in the second case to a
known value. There is no standard "Unexpected end of file" errno
value, so instead use EILSEQ, which yields a message like

  unable to read: Illegal byte sequence

That's not great, but at least it's deterministic and makes it
possible to reverse-engineer what went wrong.

Change-Id: I48138052f71b7c6cf35c2d00a10dc8febaca4f10
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 wrapper.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/wrapper.c b/wrapper.c
index 61aba0b5c1..1842a99b87 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -318,8 +318,10 @@ ssize_t read_in_full(int fd, void *buf, size_t count)
 		ssize_t loaded = xread(fd, p, count);
 		if (loaded < 0)
 			return -1;
-		if (loaded == 0)
+		if (loaded == 0) {
+			errno = EILSEQ;
 			return total;
+		}
 		count -= loaded;
 		p += loaded;
 		total += loaded;
-- 
2.14.1.821.g8fa685d3b7


  reply	other threads:[~2017-09-25 22:09 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-25 20:26 [PATCH 0/7] read/write_in_full leftovers Jeff King
2017-09-25 20:27 ` [PATCH 1/7] files-backend: prefer "0" for write_in_full() error check Jeff King
2017-09-25 21:59   ` Jonathan Nieder
2017-09-25 23:13     ` Jeff King
2017-09-25 20:27 ` [PATCH 2/7] notes-merge: drop dead zero-write code Jeff King
2017-09-25 21:59   ` Jonathan Nieder
2017-09-25 20:29 ` [PATCH 3/7] read_in_full: reset errno before reading Jeff King
2017-09-25 22:09   ` Jonathan Nieder [this message]
2017-09-25 23:23     ` Jeff King
2017-09-25 23:33       ` Jonathan Nieder
2017-09-25 23:37         ` Jeff King
2017-09-25 23:45           ` Jeff King
2017-09-25 23:55             ` Jonathan Nieder
2017-09-26  0:01               ` Jeff King
2017-09-26  0:06                 ` Stefan Beller
2017-09-26  0:09                   ` Jeff King
2017-09-26  0:16                     ` Jonathan Nieder
2017-09-26  0:17                       ` Jonathan Nieder
2017-09-26  0:19                         ` Jeff King
2017-09-26  0:22                           ` Jonathan Nieder
2017-09-26  4:11                           ` Junio C Hamano
2017-09-27  4:07                             ` Jeff King
2017-09-27  4:27                               ` Junio C Hamano
2017-09-26  0:13                 ` Jonathan Nieder
2017-09-26  0:17                   ` Jeff King
2017-09-26  0:20                     ` Jonathan Nieder
2017-09-26  0:25                       ` Jeff King
2017-09-26  0:28                         ` Jonathan Nieder
2017-09-25 23:52           ` Jonathan Nieder
2017-09-25 20:29 ` [PATCH 4/7] get-tar-commit-id: prefer "!=" for read_in_full() error check Jeff King
2017-09-25 22:10   ` Jonathan Nieder
2017-09-25 20:30 ` [PATCH 5/7] worktree: use xsize_t to access file size Jeff King
2017-09-25 22:11   ` Jonathan Nieder
2017-09-25 20:31 ` [PATCH 6/7] worktree: check the result of read_in_full() Jeff King
2017-09-25 22:14   ` Jonathan Nieder
2017-09-25 23:25     ` Jeff King
2017-09-25 20:33 ` [PATCH 7/7] add xread_in_full() helper Jeff King
2017-09-25 22:16   ` Jonathan Nieder
2017-09-25 23:27     ` Jeff King
2017-09-27  5:54 ` [PATCH v2 0/7] read/write_in_full leftovers Jeff King
2017-09-27  6:00   ` [PATCH v2 1/7] files-backend: prefer "0" for write_in_full() error check Jeff King
2017-09-27  6:00   ` [PATCH v2 2/7] notes-merge: drop dead zero-write code Jeff King
2017-09-27  6:00   ` [PATCH v2 3/7] prefer "!=" when checking read_in_full() result Jeff King
2017-09-27  6:59     ` Junio C Hamano
2017-09-27  7:09       ` Jeff King
2017-09-27  6:01   ` [PATCH v2 4/7] avoid looking at errno for short read_in_full() returns Jeff King
2017-09-27  6:59     ` Junio C Hamano
2017-09-27  6:02   ` [PATCH v2 5/7] distinguish error versus short read from read_in_full() Jeff King
2017-09-27  6:02   ` [PATCH v2 6/7] worktree: use xsize_t to access file size Jeff King
2017-09-27  6:02   ` [PATCH v2 7/7] worktree: check the result of read_in_full() Jeff King
2017-09-27  6:57   ` [PATCH v2 0/7] read/write_in_full leftovers Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170925220914.GB27425@aiede.mtv.corp.google.com \
    --to=jrnieder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).