All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,  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: Re: [PATCH v5 5/5] date: avoid overflow in approxidate relative-time multiplication
Date: Sun, 06 Dec 2026 12:18:44 +0900	[thread overview]
Message-ID: <xmqq7bhnt7u3.fsf@gitster.g> (raw)
In-Reply-To: <9025fec315309a1b3e19799ecc58ef716ee114c0.1796483214.git.gitgitgadget@gmail.com> (Johannes Schindelin via GitGitGadget's message of "Sat, 05 Dec 2026 15:06:54 +0000")

"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:

> Even though Coverity did not point it out (but Junio Hamano did), the
> weekday calculation suffers equally unguarded arithmetic. Let's guard
> against overflows in that logic, too.

I wonder if feeding the patch (or the resulting code) back again to a
tool, either the same one or a different one, would have caught further
issues.  It might lead to a virtuous cycle, where you let Coverity make
the initial discovery, have an LLM tool or two fix it, feed the
corrected code back to Coverity, and also have an LLM tool or two review
the patch text, while you also look at the code to see if there is
anything missed.  And lather, rinse, and repeat until everybody in the
toolchain and you are happy ;-).

> diff --git a/date.c b/date.c
> index c50f45d310..7d7675059b 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;

Because the earlier check in approxidate_alpha() already returns when
!*num and approxidate_digit() rejects values above INT_MAX, *num is in
[1, INT_MAX] here.  Performing the multiplication in uintmax_t and
checking unsigned_mult_overflows() plus date_overflows() avoids signed
int overflow while still allowing large offsets such as "4000 weeks ago"
on 64-bit time_t systems.

> @@ -1318,15 +1325,20 @@ 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) {
> +			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++;
> +
> +			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;
>  		}

This fixes the issues in v4 cleanly.  Because '.tm_wday' is initialized
by localtime_r() in approxidate_str() (and preserved across update_tm()
and date_never()), diff stays in [-6, 6].  On 32-bit int, max_days is
24855, so max_days - diff is always positive without overflow, and any
'n' passing the check keeps diff + 7 * n in [1, max_days], guaranteeing
diff * 24 * 60 * 60 fits in int.

This would limit weekday offsets to below ~68 years even on 64-bit
time_t (unlike "4000 weeks ago" above), though that is harmless in
practice.

> @@ -1377,8 +1389,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;
>  }

Rejecting numbers above INT_MAX before narrowing to int also prevents
inputs like "2147483648" from wrapping to INT_MIN and leaking negative
values into pending_number() or approxidate_alpha().

This round looks good to me.  Thanks, will replace.

      reply	other threads:[~2026-12-06  3:18 UTC|newest]

Thread overview: 66+ 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 ` [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
2026-12-06  3:18     ` Junio C Hamano [this message]

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=xmqq7bhnt7u3.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=Simon.Richter@hogyros.de \
    --cc=aniketgupta3001@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.