git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: Jonathan Nieder <jrnieder@gmail.com>, Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v2 4/7] avoid looking at errno for short read_in_full() returns
Date: Wed, 27 Sep 2017 02:01:07 -0400	[thread overview]
Message-ID: <20170927060107.duocwl3mf62zvgjp@sigill.intra.peff.net> (raw)
In-Reply-To: <20170927055424.22ati3g24xaqtqrk@sigill.intra.peff.net>

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>
---
 pack-write.c |  7 ++++++-
 sha1_file.c  | 11 ++++++++---
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/pack-write.c b/pack-write.c
index a333ec6754..fea6284192 100644
--- a/pack-write.c
+++ b/pack-write.c
@@ -213,14 +213,19 @@ void fixup_pack_header_footer(int pack_fd,
 	git_SHA_CTX old_sha1_ctx, new_sha1_ctx;
 	struct pack_header hdr;
 	char *buf;
+	ssize_t read_result;
 
 	git_SHA1_Init(&old_sha1_ctx);
 	git_SHA1_Init(&new_sha1_ctx);
 
 	if (lseek(pack_fd, 0, SEEK_SET) != 0)
 		die_errno("Failed seeking to start of '%s'", pack_name);
-	if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
+	read_result = read_in_full(pack_fd, &hdr, sizeof(hdr));
+	if (read_result < 0)
 		die_errno("Unable to reread header of '%s'", pack_name);
+	else if (read_result != sizeof(hdr))
+		die_errno("Unexpected short read for header of '%s'",
+			  pack_name);
 	if (lseek(pack_fd, 0, SEEK_SET) != 0)
 		die_errno("Failed seeking to start of '%s'", pack_name);
 	git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr));
diff --git a/sha1_file.c b/sha1_file.c
index 5a2014811f..09ad64ce55 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1748,10 +1748,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);
-- 
2.14.2.988.g01c8b37dde


  parent reply	other threads:[~2017-09-27  6:01 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
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   ` Jeff King [this message]
2017-09-27  6:59     ` [PATCH v2 4/7] avoid looking at errno for short read_in_full() returns 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=20170927060107.duocwl3mf62zvgjp@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jrnieder@gmail.com \
    /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).