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 v4 5/5] date: avoid overflow in approxidate relative-time multiplication
Date: Sat, 05 Dec 2026 14:33:40 +0900 [thread overview]
Message-ID: <xmqq8q24nvez.fsf@gitster.g> (raw)
In-Reply-To: <cb693161ca138d0d50cbb2e4f816fb985af2409e.1796394942.git.gitgitgadget@gmail.com> (Johannes Schindelin via GitGitGadget's message of "Fri, 04 Dec 2026 14:35:42 +0000")
"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:
> @@ -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));
The typelen loop and approxidate_digit() changes above look solid.
the new overflow guards in the weekday branch introduce both undefined
behavior on ordinary inputs and a bypass on large counts, though.
First, unsigned_mult_overflows(a, b) requires matching unsigned types
for both arguments, because maximum_unsigned_value_of_type(a) derives
its bit width from 'a'. Passing signed int '7' as the first argument
makes the macro check against UINT_MAX (4294967295) instead of
UINTMAX_MAX.
Second, and worse, diff is tm->tm_wday - i, which is negative whenever
tm->tm_wday < i (for example, on Sunday with "last Friday", diff is -5).
Evaluating INT_MAX - diff in signed int arithmetic when diff is negative
overflows int, triggering undefined behavior even on existing tests such
as "last Friday at noon" in t0006-date.sh.
Under two's complement wraparound on 32-bit int, those two issues also
let an oversized weekday multiplier slip past every check. For
example, with GIT_TEST_DATE_NOW=1251660000 (Sunday 2009-08-30 UTC) and
"1 second 613566756 Friday" ("1 second " keeps parse_date_basic() from
treating the bare number as an epoch timestamp):
- approxidate_digit() accepts 613566756 <= INT_MAX.
- n becomes 613566755, diff = 0 - 5 = -5 <= 0 increments n to
613566756.
- unsigned_mult_overflows(7, n) checks 613566756 > UINT_MAX / 7
(613566756), which is false, and n becomes 4294967292ULL.
- INT_MAX - (-5) wraps to -2147483644, which converts to uintmax_t
18446744071562067972ULL, so n > INT_MAX - diff is false.
- diff += n yields -5 + 4294967292ULL = 4294967287ULL, narrowing to
int -9.
- -9 is within [INT_MIN / 86400, INT_MAX / 86400], so update_tm()
runs with -9 days instead of returning NULL.
Since diff is in [-6, 6] and diff <= 0 increments n (keeping the final
day offset positive whenever tm->tm_wday is in [0, 6]), we can avoid
mixing signed and unsigned overflow arithmetic altogether by checking the
bound before multiplying, something like:
int diff = tm->tm_wday - i;
int max_days = INT_MAX / (24 * 60 * 60);
uintmax_t n = *num - 1;
*num = 0;
if (diff <= 0)
n++;
if (n > (uintmax_t)(max_days - diff) / 7)
return NULL;
diff += 7 * n;
update_tm(tm, now, (time_t)(diff * 24 * 60 * 60));
Because max_days is 24855 and diff is in [-6, 6], max_days - diff is
always in [24849, 24861] with no signed overflow, and any n that passes
keeps diff + 7 * n within [-6, max_days] so diff * 24 * 60 * 60 cannot
overflow int. Alternatively, we can compute seconds in uintmax_t using
(uintmax_t)7 and check date_overflows(seconds), just like the typelen
loop.
Thanks.
next prev parent reply other threads:[~2026-12-05 5:33 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 [this message]
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=xmqq8q24nvez.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox