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 v5 0/5] Address overflows pointed out by Coverity
Date: Sat, 05 Dec 2026 15:06:49 +0000	[thread overview]
Message-ID: <pull.2253.v5.git.1796483214.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 v4:

 * Fixed the commit message to no longer claim a Boolean return value of
   read_in_full_or_error().
 * Fixed the incomplete weekday arithmetic guard that I introduced in v4,
   simplifying it in the process.

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                          | 33 ++++++++---
 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, 225 insertions(+), 72 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-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2253/dscho/coverity-fixes-overflow-v5
Pull-Request: https://github.com/gitgitgadget/git/pull/2253

Range-diff vs v4:

 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:  ef314d773f ! 4:  8d85d97fe1 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 failure.
     +    that returns a negative value to indicate failure and 0 for success.
      
          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
     @@ wrapper.c: 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.
     ++ * read in out. 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)
       {
 5:  cb693161ca ! 5:  9025fec315 date: avoid overflow in approxidate relative-time multiplication
     @@ Commit message
      
          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.
     +    against overflows in that logic, too.
      
          Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
          Helped-by: Junio C Hamano <gitster@pobox.com>
     @@ date.c: static const char *approxidate_alpha(const char *date, struct tm *tm, st
       	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;
     ++			const int max_days = INT_MAX / (24 * 60 * 60);
     ++			/* `*num` is guaranteed to be positive here */
     + 			int diff, 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))
     ++			if (n > (max_days - diff) / 7)
      +				return NULL;
     -+
     + 			diff += 7*n;
     + 
     +-			update_tm(tm, now, diff * 24 * 60 * 60);
      +			update_tm(tm, now, (time_t)(diff * 24 * 60 * 60));
       			*touched = 1;
       			return end;

-- 
gitgitgadget

  parent reply	other threads:[~2026-12-05 15:06 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 ` [PATCH v4 0/5] Address overflows pointed out by Coverity Johannes Schindelin via GitGitGadget
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 ` Johannes Schindelin via GitGitGadget [this message]
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.v5.git.1796483214.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