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>,
	Johannes Schindelin <johannes.schindelin@gmx.de>,
	Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH v3 5/5] date: avoid overflow in approxidate relative-time multiplication
Date: Wed, 25 Nov 2026 20:55:49 +0000	[thread overview]
Message-ID: <23ec6cf8f08ec63efe7b868d78745940e8475ed6.1795640149.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2253.v3.git.1795640149.gitgitgadget@gmail.com>

From: Johannes Schindelin <johannes.schindelin@gmx.de>

Parsing relative dates can overflow `int` when multiplying a value by a
unit length, before converting the result to `time_t`. Signed overflow
is undefined behavior, therefore checking the converted value is too
late.

Reject quantities above `INT_MAX` before narrowing to `int` on all
platforms. The inputs are non-negative, meaning that checking unsigned
arithmetic is sufficient. The product must also fit the native `time_t`
range. Report arithmetic or conversion failures as parse errors rather
than saturating the offset.

Pointed out by Coverity.

Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Assisted-by: Claude Opus 4.6 and GPT-6.1
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 date.c          | 26 ++++++++++++++++++++------
 t/t0006-date.sh | 13 +++++++++++++
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/date.c b/date.c
index c50f45d310..3cbfe20b37 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, num_t = *num, seconds;
+
+			if (unsigned_mult_overflows(length, num_t))
+				return NULL;
+			seconds = length * num_t;
+			if (date_overflows(seconds))
+				return NULL;
+			update_tm(tm, now, (time_t)seconds);
 			*num = 0;
 			*touched = 1;
 			return end;
@@ -1377,8 +1384,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;
 }
 
@@ -1404,14 +1414,18 @@ static timestamp_t approxidate_str(const char *date,
 		if (!c)
 			break;
 		date++;
-		if (isdigit(c)) {
+		if (isalpha(c))
+			date = approxidate_alpha(date-1, &tm, &now,
+						 &number, &touched);
+		else if (isdigit(c)) {
 			pending_number(&tm, &number);
 			date = approxidate_digit(date-1, &tm, &number, time_sec);
 			touched = 1;
-			continue;
 		}
-		if (isalpha(c))
-			date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
+		if (!date) {
+			*error_ret = 1;
+			return 0;
+		}
 	}
 	pending_number(&tm, &number);
 	if (!touched)
diff --git a/t/t0006-date.sh b/t/t0006-date.sh
index 6e63130ea2..2a33aaa327 100755
--- a/t/t0006-date.sh
+++ b/t/t0006-date.sh
@@ -211,6 +211,19 @@ check_approxidate 3.weeks.ago '2009-08-09 19:20:00'
 check_approxidate 3.months.ago '2009-05-30 19:20:00'
 check_approxidate 2.years.3.months.ago '2007-05-30 19:20:00'
 
+test_expect_success HAVE_64BIT_TIME 'parse a large number of weeks' '
+	echo "Jan 1 2099 4000 weeks ago -> 1651778400" >expect &&
+	test-tool date timestamp "Jan 1 2099 4000 weeks ago" >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success TIME_IS_64BIT 'reject a quantity that does not fit in int' '
+	git reflog expire --dry-run --all --expire="1 day 1 second ago" &&
+	test_must_fail git reflog expire --dry-run --all \
+		--expire="1 day 18446744073709551615 seconds ago" 2>err &&
+	test_grep "invalid timestamp" err
+'
+
 check_approxidate '6am yesterday' '2009-08-29 06:00:00'
 check_approxidate '6pm yesterday' '2009-08-29 18:00:00'
 check_approxidate '3:00' '2009-08-30 03:00:00'
-- 
gitgitgadget

  parent reply	other threads:[~2026-11-25 20:56 UTC|newest]

Thread overview: 45+ 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-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-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-11-25 20:55   ` Johannes Schindelin via GitGitGadget [this message]
2026-11-25 22:08     ` [PATCH v3 5/5] date: avoid overflow in approxidate relative-time multiplication Junio C Hamano
2026-11-26 14:08     ` Aniket Gupta

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=23ec6cf8f08ec63efe7b868d78745940e8475ed6.1795640149.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=Simon.Richter@hogyros.de \
    --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