From: Johannes Schindelin <joha…@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 <phil…@dunelm.org.uk> Assisted-by: GPT-6.1 Signed-off-by: Johannes Schindelin <joha…@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