From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Phillip Wood <phillip.wood123@gmail.com>
Cc: Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>,
git@vger.kernel.org
Subject: Re: [PATCH 5/5] date: avoid overflow in approxidate relative-time multiplication
Date: Sun, 15 Nov 2026 20:35:12 +0100 (CET) [thread overview]
Message-ID: <fb0ee14d-e133-86af-012b-b6befd51f826@gmx.de> (raw)
In-Reply-To: <163d14a8-711c-4d4f-b441-8268db819541@gmail.com>
Hi Phillip,
On Thu, 15 Oct 2026, Phillip Wood wrote:
> On 14/10/2026 07:09, Johannes Schindelin via GitGitGadget wrote:
> > From: Johannes Schindelin <johannes.schindelin@gmx.de>
> >
> > The `approxidate_alpha()` function multiplies a time unit length (e.g.,
> > 604800 for weeks) by a user-supplied number to compute a time offset in
> > seconds. Both operands are `int`, and the product is passed to
> > `update_tm()` as `time_t`. For inputs like "99999 weeks ago", the int
> > multiplication overflows (604800 * 99999 is roughly 60 billion, far
> > beyond `INT_MAX`), producing a wrapped value that results in a wrong
> > date.
>
> Signed integer overflow is undefined so we don't necessarily get a wrapped
> value - the whole program is invalid.
>
> It looks like "length" is always positive, can the user supplied number ever
> be negative?
Indeed, length cannot be negative. My entire approach was overzealous.
I've simply replaced the logic with much easier-to-review manual overflow
checks.
> > Use the checked `time_t` multiplication so oversized products do not
> > overflow or wrap around.
>
> This is only true when compiling with gcc or clang though because
> mult_overflows() always returns 0 on other compilers, so we're still exposed
> to undefined behavior when compiling with a different compiler on platforms
> where time_t is signed. I wonder if it would be better to perform the
> multiplication on a known type and then check that the result can be
> losslessly converted to time_t
>
> intmax_t length tl->length, num_t = *num, result;
>
> if (signed_mult_overflows(length, num_t))
> // error
> result = length * num_t;
> if ((intmax_t)((time_t)result) != result ||
> (result < 0 && (time_t)result > 0))
> // error
>
> that would avoid overflow on all platforms, regardless of the compiler and
> regardless of the type of time_t. Having mult_overflows() only implemented for
> gcc seems like a footgun waiting to happen when we're typically checking for
> overflow to avoid some form of bad behavior.
Yes, this GCC-only logic wasn't very smart of me.
> > On platforms where `time_t` is unsigned (and where the pre-processor
> > allows us to determine this fact), reject counts that are too large and
> > report a parse error. Narrowing them first could otherwise produce
> > negative or truncated values that become bogus offsets. Leave signed
> > `time_t` count parsing unchanged.
>
> I'm a bit confused what this is trying to do - why can't we check this when
> time_t is signed?
>
> if ((timestamp_t)((int)number) != number)
> return NULL
>
> return *val = value;
>
> Ensures "value" fits in "val" without truncation, we could also add a check
> that value is positive when time_t is unsigned if we wanted.
I simply did `if (number > INT_MAX) return NULL`.
Thank you for your thorough review!
Johannes
>
> Thanks
>
> Phillip
> > Pointed out by Coverity.
> >
> > Assisted-by: Claude Opus 4.6 and GPT-6.1 Sol
> > Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> > ---
> > date.c | 26 ++++++++++++++++++++++++--
> > 1 file changed, 24 insertions(+), 2 deletions(-)
> >
> > diff --git a/date.c b/date.c
> > index 014065b419..0d65804d2f 100644
> > --- a/date.c
> > +++ b/date.c
> > @@ -1298,7 +1298,18 @@ 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);
> > + time_t length = tl->length, num_t = *num;
> > + time_t seconds;
> > +#ifndef is_unsigned_type
> > + seconds = length * num_t;
> > +#else
> > + time_t max_val = is_unsigned_type(length)
> > + ? maximum_unsigned_value_of_type(length)
> > + : maximum_signed_value_of_type(length);
> > + seconds = mult_overflows(length, num_t)
> > + ? max_val : length * num_t;
> > +#endif
> > + update_tm(tm, now, seconds);
> > *num = 0;
> > *touched = 1;
> > return end;
> > @@ -1368,8 +1379,15 @@ 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) {
> > +#ifdef is_unsigned_type
> > + if (is_unsigned_type((time_t)0) &&
> > + (number > INT_MAX ||
> > + number > maximum_unsigned_value_of_type((time_t)0)))
> > + return NULL;
> > +#endif
> > *num = number;
> > + }
> > return end;
> > }
> >
> > @@ -1398,6 +1416,10 @@ static timestamp_t approxidate_str(const char *date,
> > if (isdigit(c)) {
> > pending_number(&tm, &number);
> > date = approxidate_digit(date-1, &tm, &number, time_sec);
> > + if (!date) {
> > + *error_ret = 1;
> > + return 0;
> > + }
> > touched = 1;
> > continue;
> > }
>
>
>
next prev parent reply other threads:[~2026-11-15 19:35 UTC|newest]
Thread overview: 20+ 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 [this message]
2026-10-16 8:43 ` [PATCH 0/5] Address overflows pointed out by Coverity Harald Nordgren
2026-11-07 22:36 ` Junio C Hamano
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=fb0ee14d-e133-86af-012b-b6befd51f826@gmx.de \
--to=johannes.schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=phillip.wood123@gmail.com \
/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