Git development
 help / color / mirror / Atom feed
From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Phillip Wood <phillip.wood123@gmail.com>,
	Patrick Steinhardt <ps@pks.im>,
	Harald Nordgren <haraldnordgren@gmail.com>,
	Simon Richter <Simon.Richter@hogyros.de>,
	Johannes Schindelin <johannes.schindelin@gmx.de>,
	Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH v3 4/5] wrapper: add read_in_full_or_error()
Date: Wed, 25 Nov 2026 20:55:48 +0000	[thread overview]
Message-ID: <d45982f1ccfcb1d945b0be32f138627fc3552b55.1795640149.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2253.v3.git.1795640149.gitgitgadget@gmail.com>

From: Johannes Schindelin <johannes.schindelin@gmx.de>

Callers of the `read_in_full()` function want to read all requested
bytes, or get an error. However, limiting the return value type to
`ssize_t` is insufficient, as file sizes can easily exceed that type's
range.

For those callers that do not require the return value to report the
number of read bytes, add a new function, `read_in_full_or_error()`,
that returns a Boolean to indicate success or faiure.

Callers that do need to know that byte count can still call
`read_in_full()`, but will now abort to signal a programming error when
the requested byte count exceeds what `ssize_t` can represent.

Pointed out by Coverity.

Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Assisted-by: GPT-6.1 and GPT-6 Luna
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/blame.c          |  2 +-
 combine-diff.c           |  6 +----
 dir.c                    |  2 +-
 grep.c                   |  2 +-
 refs/packed-backend.c    |  4 +--
 strbuf.c                 | 10 ++++---
 t/unit-tests/u-wrapper.c | 57 ++++++++++++++++++++++++++++++++++++++++
 wrapper.c                | 50 ++++++++++++++++++++++++++++-------
 wrapper.h                |  7 +++++
 9 files changed, 116 insertions(+), 24 deletions(-)

diff --git a/builtin/blame.c b/builtin/blame.c
index 1e753a0b60..ddc49d8d30 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -1006,7 +1006,7 @@ static void parse_default_ignore_revs_file(struct blame_scoreboard *sb,
 	}
 	size = xsize_t(st.st_size);
 	buf = xmalloc(size);
-	if (read_in_full(fd, buf, size) == (ssize_t)size)
+	if (!read_in_full_or_error(fd, buf, size))
 		oidset_parse_buffer_carefully(&sb->ignore_list, buf, size,
 					      sb->repo->hash_algo,
 					      peel_to_commit_oid, sb);
diff --git a/combine-diff.c b/combine-diff.c
index 717d537688..49202c6581 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -1073,7 +1073,6 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 			free_filespec(df);
 		} else if (0 <= (fd = open(elem->path, O_RDONLY))) {
 			size_t len = xsize_t(st.st_size);
-			ssize_t done;
 			int is_file, i;
 
 			elem->mode = canon_mode(st.st_mode);
@@ -1089,11 +1088,8 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 			result_size = len;
 			result = xmallocz(len);
 
-			done = read_in_full(fd, result, len);
-			if (done < 0)
+			if (read_in_full_or_error(fd, result, len) < 0)
 				die_errno("read error '%s'", elem->path);
-			else if (done < len)
-				die("early EOF '%s'", elem->path);
 
 			/* If not a fake symlink, apply filters, e.g. autocrlf */
 			if (is_file) {
diff --git a/dir.c b/dir.c
index ac246ced1d..1c9599c2bb 100644
--- a/dir.c
+++ b/dir.c
@@ -1190,7 +1190,7 @@ static int add_patterns(const char *fname, const char *base, int baselen,
 			return 0;
 		}
 		buf = xmallocz(size);
-		if (read_in_full(fd, buf, size) != size) {
+		if (read_in_full_or_error(fd, buf, size) < 0) {
 			free(buf);
 			close(fd);
 			return -1;
diff --git a/grep.c b/grep.c
index 1d75d31421..c1c33fc140 100644
--- a/grep.c
+++ b/grep.c
@@ -1964,7 +1964,7 @@ static int grep_source_load_file(struct grep_source *gs)
 	if (i < 0)
 		goto err_ret;
 	data = xmallocz(size);
-	if (st.st_size != read_in_full(i, data, size)) {
+	if (read_in_full_or_error(i, data, size) < 0) {
 		error_errno(_("'%s': short read"), filename);
 		close(i);
 		free(data);
diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index 53511f2d91..ff7ef84109 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -543,7 +543,6 @@ static int refname_contains_nul(struct strbuf *refname)
 
 static int allocate_snapshot_buffer(struct snapshot *snapshot, int fd, struct stat *st)
 {
-	ssize_t bytes_read;
 	size_t size;
 
 	size = xsize_t(st->st_size);
@@ -552,8 +551,7 @@ static int allocate_snapshot_buffer(struct snapshot *snapshot, int fd, struct st
 
 	if (mmap_strategy == MMAP_NONE || size <= SMALL_FILE_SIZE) {
 		snapshot->buf = xmalloc(size);
-		bytes_read = read_in_full(fd, snapshot->buf, size);
-		if (bytes_read < 0 || bytes_read != size)
+		if (read_in_full_or_error(fd, snapshot->buf, size) < 0)
 			die_errno("couldn't read %s", snapshot->refs->path);
 		snapshot->mmapped = 0;
 	} else {
diff --git a/strbuf.c b/strbuf.c
index c3baa47b3f..55825d20e8 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -537,8 +537,12 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 
 	strbuf_grow(sb, hint ? hint : 8192);
 	for (;;) {
-		ssize_t want = sb->alloc - sb->len - 1;
-		ssize_t got = read_in_full(fd, sb->buf + sb->len, want);
+		size_t want = sb->alloc - sb->len - 1;
+		ssize_t got;
+
+		if ((uintmax_t)want > maximum_signed_value_of_type(ssize_t))
+			want = maximum_signed_value_of_type(ssize_t);
+		got = read_in_full(fd, sb->buf + sb->len, want);
 
 		if (got < 0) {
 			if (oldalloc == 0)
@@ -548,7 +552,7 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 			return -1;
 		}
 		sb->len += got;
-		if (got < want)
+		if ((size_t)got < want)
 			break;
 		strbuf_grow(sb, 8192);
 	}
diff --git a/t/unit-tests/u-wrapper.c b/t/unit-tests/u-wrapper.c
index 713ac0df92..28adfbdf8e 100644
--- a/t/unit-tests/u-wrapper.c
+++ b/t/unit-tests/u-wrapper.c
@@ -40,3 +40,60 @@ void test_wrapper__write_in_full_error(void)
 	cl_assert_equal_i(write_in_full(-1, data, 1), -1);
 	cl_assert_equal_i(errno, EBADF);
 }
+
+void test_wrapper__read_in_full_or_error(void)
+{
+	int fd = get_tempfile_fd(file);
+
+	cl_assert_equal_i(write_in_full(fd, data, sizeof(data)), 0);
+	cl_assert(lseek(fd, 0, SEEK_SET) == 0);
+	cl_assert_equal_i(read_in_full_or_error(fd, actual, sizeof(actual)), 0);
+	cl_assert(!memcmp(data, actual, sizeof(data)));
+}
+
+void test_wrapper__read_in_full_or_error_empty(void)
+{
+	cl_assert_equal_i(read_in_full_or_error(-1, NULL, 0), 0);
+}
+
+void test_wrapper__read_in_full_or_error_short(void)
+{
+	int fd = get_tempfile_fd(file);
+
+	cl_assert_equal_i(write_in_full(fd, "abc", 3), 0);
+	cl_assert(lseek(fd, 0, SEEK_SET) == 0);
+	errno = 0;
+	cl_assert_equal_i(read_in_full_or_error(fd, actual, sizeof(actual)),
+			  -1);
+	cl_assert_equal_i(errno, EIO);
+	cl_assert(!memcmp(actual, "abc", 3));
+}
+
+void test_wrapper__read_in_full_or_error_bad_fd(void)
+{
+	errno = 0;
+	cl_assert_equal_i(read_in_full_or_error(-1, actual, 1), -1);
+	cl_assert_equal_i(errno, EBADF);
+}
+
+void test_wrapper__read_in_full_short(void)
+{
+	int fd = get_tempfile_fd(file);
+
+	cl_assert_equal_i(write_in_full(fd, "abc", 3), 0);
+	cl_assert(lseek(fd, 0, SEEK_SET) == 0);
+	cl_assert_equal_i(read_in_full(fd, actual, sizeof(actual)), 3);
+	cl_assert(!memcmp(actual, "abc", 3));
+}
+
+void test_wrapper__read_in_full_empty(void)
+{
+	cl_assert_equal_i(read_in_full(-1, NULL, 0), 0);
+}
+
+void test_wrapper__read_in_full_error(void)
+{
+	errno = 0;
+	cl_assert_equal_i(read_in_full(-1, actual, 1), -1);
+	cl_assert_equal_i(errno, EBADF);
+}
diff --git a/wrapper.c b/wrapper.c
index 87e1b4bfd2..dba8b90dcc 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -283,23 +283,53 @@ ssize_t xpread(int fd, void *buf, size_t len, off_t offset)
 	}
 }
 
-ssize_t read_in_full(int fd, void *buf, size_t count)
+/*
+ * Read until count bytes have been read or EOF. On success, store the number
+ * read in out if non-NULL. Return false on an I/O error, with errno set.
+ */
+static bool read_in_full_1(int fd, void *buf, size_t count, size_t *out)
 {
 	char *p = buf;
-	ssize_t total = 0;
+	size_t remaining = count;
 
-	while (count > 0) {
-		ssize_t loaded = xread(fd, p, count);
+	while (remaining) {
+		ssize_t loaded = xread(fd, p, remaining);
 		if (loaded < 0)
-			return -1;
-		if (loaded == 0)
-			return total;
-		count -= loaded;
+			return false;
+		if (!loaded)
+			break;
+		remaining -= loaded;
 		p += loaded;
-		total += loaded;
 	}
 
-	return total;
+	if (out)
+		*out = count - remaining;
+	return true;
+}
+
+int read_in_full_or_error(int fd, void *buf, size_t count)
+{
+	size_t total;
+
+	if (!read_in_full_1(fd, buf, count, &total))
+		return -1;
+	if (total != count) {
+		errno = EIO;
+		return -1;
+	}
+	return 0;
+}
+
+ssize_t read_in_full(int fd, void *buf, size_t count)
+{
+	size_t total;
+
+	if ((uintmax_t)count > maximum_signed_value_of_type(ssize_t))
+		BUG("read_in_full() cannot return %"PRIuMAX" bytes",
+		    (uintmax_t)count);
+	if (!read_in_full_1(fd, buf, count, &total))
+		return -1;
+	return (ssize_t)total;
 }
 
 int write_in_full(int fd, const void *buf, size_t count)
diff --git a/wrapper.h b/wrapper.h
index 726c5c2f6b..47d441e6c9 100644
--- a/wrapper.h
+++ b/wrapper.h
@@ -44,8 +44,15 @@ char *git_mkdtemp(char *pattern);
 int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
 int git_mkstemp_mode(char *pattern, int mode);
 
+/*
+ * Return the number of bytes read, possibly short at EOF, or -1 on error.
+ * The requested count must be representable in ssize_t.
+ */
 ssize_t read_in_full(int fd, void *buf, size_t count);
 
+/* Return 0 on a complete read, or -1 with errno set (EIO on premature EOF). */
+int read_in_full_or_error(int fd, void *buf, size_t count);
+
 /* Return 0 on a complete write, or -1 with errno set on failure. */
 int write_in_full(int fd, const void *buf, size_t count);
 
-- 
gitgitgadget


  parent reply	other threads:[~2026-11-25 20:56 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14  6:09 [PATCH 0/5] Address overflows pointed out by Coverity Johannes Schindelin via GitGitGadget
2026-10-14  6:09 ` [PATCH 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-10-14  6:09 ` [PATCH 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-10-16  8:38   ` Patrick Steinhardt
2026-10-16 14:30     ` Junio C Hamano
2026-10-17  0:18       ` Simon Richter
2026-10-17  4:10         ` Junio C Hamano
2026-10-17  4:11         ` Junio C Hamano
2026-10-14  6:09 ` [PATCH 3/5] wrapper: reject insanely large byte counts when reading/writing Johannes Schindelin via GitGitGadget
2026-10-14 11:37   ` Phillip Wood
2026-11-15 19:24     ` Johannes Schindelin
2026-10-14  6:09 ` [PATCH 4/5] git-compat-util: add `signed_mult_overflows` and `mult_overflows` macros Johannes Schindelin via GitGitGadget
2026-10-15  9:27   ` Phillip Wood
2026-11-15 19:29     ` Johannes Schindelin
2026-10-14  6:09 ` [PATCH 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-10-14 21:44   ` Junio C Hamano
2026-10-15  9:28   ` Phillip Wood
2026-11-15 19:35     ` Johannes Schindelin
2026-10-16  8:43 ` [PATCH 0/5] Address overflows pointed out by Coverity Harald Nordgren
2026-11-07 22:36 ` Junio C Hamano
2026-11-23 22:13 ` [PATCH v2 " Johannes Schindelin via GitGitGadget
2026-11-23 22:13   ` [PATCH v2 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-11-24 17:34     ` Junio C Hamano
2026-11-23 22:13   ` [PATCH v2 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-11-24 17:54     ` Junio C Hamano
2026-11-25 14:04       ` Johannes Schindelin
2026-11-23 22:13   ` [PATCH v2 3/5] wrapper: avoid byte-count overflow in write_in_full() Johannes Schindelin via GitGitGadget
2026-11-24 14:37     ` Phillip Wood
2026-11-25 14:20       ` Johannes Schindelin
2026-11-23 22:13   ` [PATCH v2 4/5] wrapper: add read_in_full_or_error() Johannes Schindelin via GitGitGadget
2026-11-24 14:24     ` Junio C Hamano
2026-11-23 22:13   ` [PATCH v2 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-11-24 14:44     ` Phillip Wood
2026-11-24 18:04   ` [PATCH v2 0/5] Address overflows pointed out by Coverity Junio C Hamano
2026-11-25 20:55 ` [PATCH v3 " Johannes Schindelin via GitGitGadget
2026-11-25 20:55   ` [PATCH v3 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-11-25 20:55   ` [PATCH v3 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-11-25 20:55   ` [PATCH v3 3/5] wrapper: avoid byte-count overflow in write_in_full() Johannes Schindelin via GitGitGadget
2026-11-25 23:47     ` Junio C Hamano
2026-11-26 15:14       ` Phillip Wood
2026-11-25 20:55   ` Johannes Schindelin via GitGitGadget [this message]
2026-11-25 23:57     ` [PATCH v3 4/5] wrapper: add read_in_full_or_error() Junio C Hamano
2026-11-25 20:55   ` [PATCH v3 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-11-25 22:08     ` Junio C Hamano
2026-11-26 14:08     ` Aniket Gupta

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

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

  git send-email \
    --in-reply-to=d45982f1ccfcb1d945b0be32f138627fc3552b55.1795640149.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=Simon.Richter@hogyros.de \
    --cc=git@vger.kernel.org \
    --cc=haraldnordgren@gmail.com \
    --cc=johannes.schindelin@gmx.de \
    --cc=phillip.wood123@gmail.com \
    --cc=ps@pks.im \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox