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>,
	Aniket Gupta <aniketgupta3001@gmail.com>,
	Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH v4 0/5] Address overflows pointed out by Coverity
Date: Fri, 04 Dec 2026 14:35:37 +0000	[thread overview]
Message-ID: <pull.2253.v4.git.1796394942.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2253.git.1791958171.gitgitgadget@gmail.com>

This patch series is part of my effort to bring Git for Windows' Coverity
reports down to zero; Previous patch series have been merged in 347fe8a91b5
(Merge branch 'js/coverity-fixes', 2026-07-16), 0b75ef5168c (Merge branch
'js/coverity-fixes-null-safety', 2026-07-19), fcc8c931b53 (Merge branch
'js/coverity-unchecked-returns-fix', 2026-08-24) and 8bc9c7fcfd8 (Merge
branch 'js/coverity-fixes', 2026-10-03).

Changes since v3:

 * Fixed typo in 4/5's commit message.
 * Removed unnecessary conditional in read_in_full_1().
 * The should_prune_worktree() function now also uses
   read_in_full_or_error() (and is simpler for it).
 * The num_t variable was renamed (because the _t suffix suggested a type,
   which is misleading).
 * The weekday logic is now also guarded against over-/underflows.

Changes since v2:

 * Fixed a grammar error and an overstatement in the "Changes since v1"
   section :-)
 * Accepted Junio's rewritten explanation in 2/5's commit message.
 * Clarified the design decision to abort on programing errors in 4/5's
   commit message.
 * Adjusted a few straggler ssize_t variables that can now safely be int or
   even be dropped.

Changes since v1:

 * Five callers of read_in_full() and all callers of write_in_full() are now
   no longer restricted by the range of ssize_t.
 * The signed_mult_overflows() and mult_overflows() macros have been
   dropped.
 * Since the approxidate() function only deals with non-negative values, the
   complicated overflow check could have been simplified.
 * Rebased to master, to avoid merge conflicts.

Johannes Schindelin (5):
  archive-tar: guard get_path_prefix against zero-length input
  http: use `size_t` for byte position in fwrite_sha1_file
  wrapper: avoid byte-count overflow in write_in_full()
  wrapper: add read_in_full_or_error()
  date: avoid overflow in approxidate relative-time multiplication

 Makefile                        |  1 +
 archive-tar.c                   |  2 +
 builtin/blame.c                 |  2 +-
 combine-diff.c                  |  6 +-
 config.c                        | 14 ++---
 date.c                          | 44 ++++++++++++---
 dir.c                           |  2 +-
 entry.c                         |  2 +-
 grep.c                          |  2 +-
 http.c                          |  2 +-
 imap-send.c                     |  2 +-
 notes-merge.c                   | 13 +----
 odb/streaming.c                 |  6 +-
 parallel-checkout.c             |  2 +-
 refs/packed-backend.c           |  4 +-
 strbuf.c                        | 10 +++-
 t/meson.build                   |  1 +
 t/t0006-date.sh                 | 13 +++++
 t/unit-tests/u-reftable-stack.c |  2 +-
 t/unit-tests/u-wrapper.c        | 99 +++++++++++++++++++++++++++++++++
 worktree.c                      | 10 +---
 wrapper.c                       | 55 +++++++++++++-----
 wrapper.h                       | 14 ++++-
 23 files changed, 234 insertions(+), 74 deletions(-)
 create mode 100644 t/unit-tests/u-wrapper.c


base-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2253%2Fdscho%2Fcoverity-fixes-overflow-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2253/dscho/coverity-fixes-overflow-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/2253

Range-diff vs v3:

 1:  bb8225547f = 1:  bb8225547f archive-tar: guard get_path_prefix against zero-length input
 2:  aa2d3961b2 = 2:  aa2d3961b2 http: use `size_t` for byte position in fwrite_sha1_file
 3:  f38c7e7133 = 3:  f38c7e7133 wrapper: avoid byte-count overflow in write_in_full()
 4:  d45982f1cc ! 4:  ef314d773f wrapper: add read_in_full_or_error()
     @@ Commit message
      
          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.
     +    that returns a Boolean to indicate success or failure.
      
          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.
      
     +    Note that one of the callers that _did_ look at the byte count is
     +    `should_prune_worktree()`, but since all it did was to write an error
     +    message about a short read, I also converted it to call
     +    `read_in_full_or_error()`. The result is a bit simpler.
     +
          Pointed out by Coverity.
      
          Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
     +    Helped-by: Junio C Hamano <gitster@pobox.com>
          Assisted-by: GPT-6.1 and GPT-6 Luna
          Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
      
     @@ t/unit-tests/u-wrapper.c: void test_wrapper__write_in_full_error(void)
      +	cl_assert_equal_i(errno, EBADF);
      +}
      
     + ## worktree.c ##
     +@@ worktree.c: int should_prune_worktree(struct repository *repo,
     + 	int rc = 0;
     + 	int fd;
     + 	size_t len;
     +-	ssize_t read_result;
     ++	int read_result;
     + 
     + 	*wtpath = NULL;
     + 
     +@@ worktree.c: 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--;
     +
       ## wrapper.c ##
      @@ wrapper.c: ssize_t xpread(int fd, void *buf, size_t len, off_t offset)
       	}
     @@ wrapper.c: ssize_t xpread(int fd, void *buf, size_t len, off_t offset)
       	}
       
      -	return total;
     -+	if (out)
     -+		*out = count - remaining;
     ++	*out = count - remaining;
      +	return true;
      +}
      +
 5:  23ec6cf8f0 ! 5:  cb693161ca date: avoid overflow in approxidate relative-time multiplication
     @@ Commit message
      
          Pointed out by Coverity.
      
     +    Even though Coverity did not point it out (but Junio Hamano did), the
     +    weekday calculation suffers equally unguarded arithmetic. Let's guard
     +    against overflows/underflows in that logic, too.
     +
          Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
     +    Helped-by: Junio C Hamano <gitster@pobox.com>
          Assisted-by: Claude Opus 4.6 and GPT-6.1
          Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
      
     @@ date.c: static const char *approxidate_alpha(const char *date, struct tm *tm, st
       		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, num_t = *num, seconds;
     ++			uintmax_t length = tl->length, n = *num, seconds;
      +
     -+			if (unsigned_mult_overflows(length, num_t))
     ++			if (unsigned_mult_overflows(length, n))
      +				return NULL;
     -+			seconds = length * num_t;
     ++			seconds = length * n;
      +			if (date_overflows(seconds))
      +				return NULL;
      +			update_tm(tm, now, (time_t)seconds);
       			*num = 0;
       			*touched = 1;
       			return end;
     +@@ date.c: 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;
     + 		}
      @@ date.c: static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
       	}
       

-- 
gitgitgadget

  parent reply	other threads:[~2026-12-04 14:35 UTC|newest]

Thread overview: 65+ 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-12-04 14:09   ` Johannes Schindelin
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-12-04 13:23         ` Johannes Schindelin
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-12-04 13:30       ` Johannes Schindelin
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-12-04 14:08       ` Johannes Schindelin
2026-11-26 14:08     ` Aniket Gupta
2026-12-04 13:36       ` Johannes Schindelin
2026-12-04 14:35 ` Johannes Schindelin via GitGitGadget [this message]
2026-12-04 14:35   ` [PATCH v4 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-12-04 14:35   ` [PATCH v4 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-12-04 14:35   ` [PATCH v4 3/5] wrapper: avoid byte-count overflow in write_in_full() Johannes Schindelin via GitGitGadget
2026-12-04 14:35   ` [PATCH v4 4/5] wrapper: add read_in_full_or_error() Johannes Schindelin via GitGitGadget
2026-12-05  5:32     ` Junio C Hamano
2026-12-04 14:35   ` [PATCH v4 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-12-05  5:33     ` Junio C Hamano
2026-12-05 15:05       ` Johannes Schindelin
2026-12-05 15:06 ` [PATCH v5 0/5] Address overflows pointed out by Coverity Johannes Schindelin via GitGitGadget
2026-12-05 15:06   ` [PATCH v5 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-12-05 15:06   ` [PATCH v5 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-12-05 15:06   ` [PATCH v5 3/5] wrapper: avoid byte-count overflow in write_in_full() Johannes Schindelin via GitGitGadget
2026-12-05 15:06   ` [PATCH v5 4/5] wrapper: add read_in_full_or_error() Johannes Schindelin via GitGitGadget
2026-12-05 15:06   ` [PATCH v5 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget

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=pull.2253.v4.git.1796394942.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=Simon.Richter@hogyros.de \
    --cc=aniketgupta3001@gmail.com \
    --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