diff --git a/Makefile b/Makefile index 4c84eea6e2ea3e..0687eb82d32e46 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/archive-tar.c b/archive-tar.c index a1c66024d4dff4..2da6e554842b8b 100644 --- a/archive-tar.c +++ b/archive-tar.c @@ -210,6 +210,8 @@ static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen) i--; if (i > maxlen) i = maxlen; + if (!i) + return 0; do { i--; } while (i > 0 && path[i] != '/'); diff --git a/builtin/blame.c b/builtin/blame.c index 1e753a0b605227..ddc49d8d30255f 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 717d5376888f8e..49202c6581d528 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/config.c b/config.c index 5976a418d55f53..778fa247a8c379 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/date.c b/date.c index c50f45d3108a10..ff9387d08f6dbf 100644 --- a/date.c +++ b/date.c @@ -1307,7 +1307,14 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm while (tl->type) { size_t len = strlen(tl->type); if (match_string(date, tl->type) >= len-1) { - update_tm(tm, now, tl->length * *num); + uintmax_t length = tl->length, n = *num, seconds; + + if (unsigned_mult_overflows(length, n)) + return NULL; + seconds = length * n; + if (date_overflows(seconds)) + return NULL; + update_tm(tm, now, (time_t)seconds); *num = 0; *touched = 1; return end; @@ -1318,15 +1325,27 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm for (i = 0; i < 7; i++) { int match = match_string(date, weekday_names[i]); if (match >= 3) { - int diff, n = *num -1; + int diff; + uintmax_t n = *num -1; *num = 0; diff = tm->tm_wday - i; if (diff <= 0) n++; - diff += 7*n; - update_tm(tm, now, diff * 24 * 60 * 60); + if (unsigned_mult_overflows(7, n)) + return NULL; + n *= 7; + + if (n > INT_MAX - diff) + return NULL; + diff += n; + + if (diff > INT_MAX / (24 * 60 * 60) || + diff < INT_MIN / (24 * 60 * 60)) + return NULL; + + update_tm(tm, now, (time_t)(diff * 24 * 60 * 60)); *touched = 1; return end; } @@ -1377,8 +1396,11 @@ static const char *approxidate_digit(const char *date, struct tm *tm, int *num, } /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */ - if (date[0] != '0' || end - date <= 2) + if (date[0] != '0' || end - date <= 2) { + if (number > INT_MAX) + return NULL; *num = number; + } return end; } @@ -1404,14 +1426,18 @@ static timestamp_t approxidate_str(const char *date, if (!c) break; date++; - if (isdigit(c)) { + if (isalpha(c)) + date = approxidate_alpha(date-1, &tm, &now, + &number, &touched); + else if (isdigit(c)) { pending_number(&tm, &number); date = approxidate_digit(date-1, &tm, &number, time_sec); touched = 1; - continue; } - if (isalpha(c)) - date = approxidate_alpha(date-1, &tm, &now, &number, &touched); + if (!date) { + *error_ret = 1; + return 0; + } } pending_number(&tm, &number); if (!touched) diff --git a/dir.c b/dir.c index ac246ced1dea6f..1c9599c2bb8274 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/entry.c b/entry.c index 1c4f0f44070ea3..03959a6c2d83b2 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/grep.c b/grep.c index 1d75d314211aa8..c1c33fc140134d 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/http.c b/http.c index d79678d01f0524..2879eb3874a882 100644 --- a/http.c +++ b/http.c @@ -2848,7 +2848,7 @@ static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb, { unsigned char expn[4096]; size_t size = eltsize * nmemb; - int posn = 0; + size_t posn = 0; struct http_object_request *freq = data; struct active_request_slot *slot = freq->slot; diff --git a/imap-send.c b/imap-send.c index bf1d2cf74d6d42..dccc2264deeccf 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 d361e70a4830c3..991490a1875b0e 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 5ac172beb65f04..2228f31b5743e1 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 0bf4bd6d4abd8c..a0dddfd0bfd1b4 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/refs/packed-backend.c b/refs/packed-backend.c index 53511f2d91900a..ff7ef8410936f3 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 c3baa47b3f71e7..55825d20e8acee 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/meson.build b/t/meson.build index 1cf1d95f70bdd6..8938d4eb47d5b6 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/t0006-date.sh b/t/t0006-date.sh index 6e63130ea29ff1..2a33aaa32739b7 100755 --- a/t/t0006-date.sh +++ b/t/t0006-date.sh @@ -211,6 +211,19 @@ check_approxidate 3.weeks.ago '2009-08-09 19:20:00' check_approxidate 3.months.ago '2009-05-30 19:20:00' check_approxidate 2.years.3.months.ago '2007-05-30 19:20:00' +test_expect_success HAVE_64BIT_TIME 'parse a large number of weeks' ' + echo "Jan 1 2099 4000 weeks ago -> 1651778400" >expect && + test-tool date timestamp "Jan 1 2099 4000 weeks ago" >actual && + test_cmp expect actual +' + +test_expect_success TIME_IS_64BIT 'reject a quantity that does not fit in int' ' + git reflog expire --dry-run --all --expire="1 day 1 second ago" && + test_must_fail git reflog expire --dry-run --all \ + --expire="1 day 18446744073709551615 seconds ago" 2>err && + test_grep "invalid timestamp" err +' + check_approxidate '6am yesterday' '2009-08-29 06:00:00' check_approxidate '6pm yesterday' '2009-08-29 18:00:00' check_approxidate '3:00' '2009-08-30 03:00:00' diff --git a/t/unit-tests/u-reftable-stack.c b/t/unit-tests/u-reftable-stack.c index b6f1c6cc523e89..3e19f0466a2e1b 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 00000000000000..28adfbdf8e49bf --- /dev/null +++ b/t/unit-tests/u-wrapper.c @@ -0,0 +1,99 @@ +#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); +} + +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/worktree.c b/worktree.c index 8cb8637b189ab8..62b866c1a12183 100644 --- a/worktree.c +++ b/worktree.c @@ -949,7 +949,7 @@ int should_prune_worktree(struct repository *repo, int rc = 0; int fd; size_t len; - ssize_t read_result; + int read_result; *wtpath = NULL; @@ -982,19 +982,13 @@ int should_prune_worktree(struct repository *repo, len = xsize_t(st.st_size); path = xmallocz(len); - read_result = read_in_full(fd, path, len); + read_result = read_in_full_or_error(fd, path, len); close(fd); if (read_result < 0) { strbuf_addf(reason, _("unable to read gitdir file (%s)"), strerror(errno)); rc = 1; goto done; - } else if (read_result != len) { - strbuf_addf(reason, - _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"), - (uintmax_t)len, (uintmax_t)read_result); - rc = 1; - goto done; } while (len && (path[len - 1] == '\n' || path[len - 1] == '\r')) len--; diff --git a/wrapper.c b/wrapper.c index 05a9cd369c5971..5ed58f7df898a3 100644 --- a/wrapper.c +++ b/wrapper.c @@ -283,29 +283,57 @@ 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; + *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 write_in_full(int fd, const void *buf, size_t count) +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) { const char *p = buf; - ssize_t total = 0; while (count > 0) { ssize_t written = xwrite(fd, p, count); @@ -317,10 +345,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 a6287d7f4d11be..47d441e6c9a2ac 100644 --- a/wrapper.h +++ b/wrapper.h @@ -44,8 +44,18 @@ 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); -ssize_t write_in_full(int fd, const 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); + ssize_t pread_in_full(int fd, void *buf, size_t count, off_t offset); /* @@ -57,7 +67,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)); }