From: Johannes Schindelin <joha…@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. 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. Helped-by: Phillip Wood <phil…@dunelm.org.uk> Helped-by: Junio C Hamano <gits…@pobox.com> Assisted-by: Claude Opus 4.6 and GPT-6.1 Signed-off-by: Johannes Schindelin <joha…@gmx.de> --- date.c | 44 +++++++++++++++++++++++++++++++++++--------- t/t0006-date.sh | 13 +++++++++++++ 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/date.c b/date.c index c50f45d310..ff9387d08f 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; @@ -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)); *touched = 1; return end; } @@ -1377,8 +1396,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 +1426,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