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 3/5] wrapper: avoid byte-count overflow in write_in_full()
Date: Wed, 25 Nov 2026 20:55:47 +0000 [thread overview]
Message-ID: <f38c7e71337632f2e86ef5716e90c776c491b762.1795640149.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2253.v3.git.1795640149.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>
Accumulating the output count in `write_in_full()` can overflow because
it counts via a `ssize_t` variable which can overflow for valid `size_t`
sizes passed as input.
Since this function either completes the requested count or fails,
callers already know how many bytes were written on success. Let's just
return status instead, so the total transfer size need not fit in
`ssize_t`.
Pointed out by Coverity.
Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Assisted-by: GPT-6.1
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
Makefile | 1 +
config.c | 14 +++++------
entry.c | 2 +-
imap-send.c | 2 +-
notes-merge.c | 13 ++--------
odb/streaming.c | 6 ++---
parallel-checkout.c | 2 +-
t/meson.build | 1 +
t/unit-tests/u-reftable-stack.c | 2 +-
t/unit-tests/u-wrapper.c | 42 +++++++++++++++++++++++++++++++++
wrapper.c | 6 ++---
wrapper.h | 7 ++++--
12 files changed, 66 insertions(+), 32 deletions(-)
create mode 100644 t/unit-tests/u-wrapper.c
diff --git a/Makefile b/Makefile
index 4c84eea6e2..0687eb82d3 100644
--- a/Makefile
+++ b/Makefile
@@ -1564,6 +1564,7 @@ CLAR_TEST_SUITES += u-strvec
CLAR_TEST_SUITES += u-trailer
CLAR_TEST_SUITES += u-urlmatch-normalization
CLAR_TEST_SUITES += u-utf8-width
+CLAR_TEST_SUITES += u-wrapper
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
diff --git a/config.c b/config.c
index 5976a418d5..778fa247a8 100644
--- a/config.c
+++ b/config.c
@@ -2768,11 +2768,11 @@ static struct strbuf store_create_section(const char *key,
return sb;
}
-static ssize_t write_section(int fd, const char *key,
- const struct config_store_data *store)
+static int write_section(int fd, const char *key,
+ const struct config_store_data *store)
{
struct strbuf sb = store_create_section(key, store);
- ssize_t ret;
+ int ret;
ret = write_in_full(fd, sb.buf, sb.len);
strbuf_release(&sb);
@@ -2780,12 +2780,12 @@ static ssize_t write_section(int fd, const char *key,
return ret;
}
-static ssize_t write_pair(int fd, const char *key, const char *value,
- const char *comment,
- const struct config_store_data *store)
+static int write_pair(int fd, const char *key, const char *value,
+ const char *comment,
+ const struct config_store_data *store)
{
int i;
- ssize_t ret;
+ int ret;
const char *quote = "";
struct strbuf sb = STRBUF_INIT;
diff --git a/entry.c b/entry.c
index 1c4f0f4407..03959a6c2d 100644
--- a/entry.c
+++ b/entry.c
@@ -290,7 +290,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
char *new_blob;
struct strbuf buf = STRBUF_INIT;
size_t size;
- ssize_t wrote;
+ int wrote;
size_t newsize = 0;
struct stat st;
const struct submodule *sub;
diff --git a/imap-send.c b/imap-send.c
index bf1d2cf74d..dccc2264de 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -392,7 +392,7 @@ static int socket_write(struct imap_socket *sock, const char *buf, int len)
n = SSL_write(sock->ssl, buf, len);
else
#endif
- n = write_in_full(sock->fd[1], buf, len);
+ n = write_in_full(sock->fd[1], buf, len) < 0 ? -1 : len;
if (n != len) {
socket_perror("write", sock, n);
close(sock->fd[0]);
diff --git a/notes-merge.c b/notes-merge.c
index d361e70a48..991490a187 100644
--- a/notes-merge.c
+++ b/notes-merge.c
@@ -319,17 +319,8 @@ static void write_buf_to_worktree(const struct object_id *obj,
fd = xopen(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
- while (size > 0) {
- ssize_t ret = write_in_full(fd, buf, size);
- if (ret < 0) {
- /* Ignore epipe */
- if (errno == EPIPE)
- break;
- die_errno("notes-merge");
- }
- size -= ret;
- buf += ret;
- }
+ if (write_in_full(fd, buf, size) < 0 && errno != EPIPE)
+ die_errno("notes-merge");
close(fd);
free(path);
diff --git a/odb/streaming.c b/odb/streaming.c
index 5ac172beb6..2228f31b57 100644
--- a/odb/streaming.c
+++ b/odb/streaming.c
@@ -247,7 +247,7 @@ int odb_stream_blob_to_fd(struct object_database *odb,
goto close_and_exit;
for (;;) {
char buf[1024 * 16];
- ssize_t wrote, holeto;
+ ssize_t holeto;
ssize_t readlen = odb_stream_read(st, buf, sizeof(buf));
if (readlen < 0)
@@ -268,9 +268,7 @@ int odb_stream_blob_to_fd(struct object_database *odb,
goto close_and_exit;
else
kept = 0;
- wrote = write_in_full(fd, buf, readlen);
-
- if (wrote < 0)
+ if (write_in_full(fd, buf, readlen) < 0)
goto close_and_exit;
}
if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 ||
diff --git a/parallel-checkout.c b/parallel-checkout.c
index 0bf4bd6d4a..a0dddfd0bf 100644
--- a/parallel-checkout.c
+++ b/parallel-checkout.c
@@ -274,7 +274,7 @@ static int write_pc_item_to_fd(struct parallel_checkout_item *pc_item, int fd,
struct strbuf buf = STRBUF_INIT;
char *blob;
size_t size;
- ssize_t wrote;
+ int wrote;
/* Sanity check */
ASSERT(is_eligible_for_parallel_checkout(pc_item->ce, &pc_item->ca));
diff --git a/t/meson.build b/t/meson.build
index 1cf1d95f70..8938d4eb47 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -27,6 +27,7 @@ clar_test_suites = [
'unit-tests/u-trailer.c',
'unit-tests/u-urlmatch-normalization.c',
'unit-tests/u-utf8-width.c',
+ 'unit-tests/u-wrapper.c',
]
clar_sources = [
diff --git a/t/unit-tests/u-reftable-stack.c b/t/unit-tests/u-reftable-stack.c
index b6f1c6cc52..3e19f0466a 100644
--- a/t/unit-tests/u-reftable-stack.c
+++ b/t/unit-tests/u-reftable-stack.c
@@ -86,7 +86,7 @@ void test_reftable_stack__read_file(void)
cl_assert(fd > 0);
n = write_in_full(fd, out, strlen(out));
- cl_assert_equal_i(n, strlen(out));
+ cl_assert_equal_i(n, 0);
err = close(fd);
cl_assert(err >= 0);
diff --git a/t/unit-tests/u-wrapper.c b/t/unit-tests/u-wrapper.c
new file mode 100644
index 0000000000..713ac0df92
--- /dev/null
+++ b/t/unit-tests/u-wrapper.c
@@ -0,0 +1,42 @@
+#include "unit-test.h"
+#include "tempfile.h"
+
+static struct tempfile *file;
+static char data[128 * 1024], actual[sizeof(data)];
+
+void test_wrapper__initialize(void)
+{
+ file = mks_tempfile("wrapper-test-XXXXXX");
+ cl_assert(file != NULL);
+ for (size_t i = 0; i < sizeof(data); i++)
+ data[i] = i % 127;
+ memset(actual, 0, sizeof(actual));
+}
+
+void test_wrapper__cleanup(void)
+{
+ delete_tempfile(&file);
+}
+
+void test_wrapper__write_in_full(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(read_in_full(fd, actual, sizeof(actual)) ==
+ (ssize_t)sizeof(actual));
+ cl_assert(!memcmp(data, actual, sizeof(data)));
+}
+
+void test_wrapper__write_in_full_empty(void)
+{
+ cl_assert_equal_i(write_in_full(-1, NULL, 0), 0);
+}
+
+void test_wrapper__write_in_full_error(void)
+{
+ errno = 0;
+ cl_assert_equal_i(write_in_full(-1, data, 1), -1);
+ cl_assert_equal_i(errno, EBADF);
+}
diff --git a/wrapper.c b/wrapper.c
index 05a9cd369c..87e1b4bfd2 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -302,10 +302,9 @@ ssize_t read_in_full(int fd, void *buf, size_t count)
return total;
}
-ssize_t write_in_full(int fd, const void *buf, size_t count)
+int write_in_full(int fd, const void *buf, size_t count)
{
const char *p = buf;
- ssize_t total = 0;
while (count > 0) {
ssize_t written = xwrite(fd, p, count);
@@ -317,10 +316,9 @@ ssize_t write_in_full(int fd, const void *buf, size_t count)
}
count -= written;
p += written;
- total += written;
}
- return total;
+ return 0;
}
ssize_t xwritev(int fd, struct iovec *iov, int iovcnt)
diff --git a/wrapper.h b/wrapper.h
index a6287d7f4d..726c5c2f6b 100644
--- a/wrapper.h
+++ b/wrapper.h
@@ -45,7 +45,10 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
int git_mkstemp_mode(char *pattern, int mode);
ssize_t read_in_full(int fd, void *buf, size_t count);
-ssize_t write_in_full(int fd, const 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);
+
ssize_t pread_in_full(int fd, void *buf, size_t count, off_t offset);
/*
@@ -57,7 +60,7 @@ ssize_t pread_in_full(int fd, void *buf, size_t count, off_t offset);
*/
ssize_t writev_in_full(int fd, struct iovec *iov, int iovcnt);
-static inline ssize_t write_str_in_full(int fd, const char *str)
+static inline int write_str_in_full(int fd, const char *str)
{
return write_in_full(fd, str, strlen(str));
}
--
gitgitgadget
next prev 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 ` Johannes Schindelin via GitGitGadget [this message]
2026-11-25 23:47 ` [PATCH v3 3/5] wrapper: avoid byte-count overflow in write_in_full() Junio C Hamano
2026-11-26 15:14 ` Phillip Wood
2026-11-25 20:55 ` [PATCH v3 4/5] wrapper: add read_in_full_or_error() Johannes Schindelin via GitGitGadget
2026-11-25 23:57 ` 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=f38c7e71337632f2e86ef5716e90c776c491b762.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