From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796483230; b=U2api8ezdT6TVFQZWKsmi5w9Q3ufuG+lNgFtUXxNflO5RPGi0ilHAKzADhfiaQWZ1+Fn 9qumU1l7a3IJ0DbpHdelaXrx07HGnyeOgpKCLpY0rFThX91JrK4tlxK76wMLiQQqcI1P4 X7OVw0ctnkXEmCxXBdNqneq/qyW+bdAfCj14R5vWGCoXhJVLeu84HzxKG/bk56+C1YCbu WxUZGJJ/ekK6/Q68ofmLm55xvNxFB2v0w2O8vgxx09Yq+j1CCewNXwyYbOfo2PZSxM2Tb OStX7Wq1yBQhF1Om+CNL+LX3hKRZVMYxBirerD63lvIVBePq0l9kKKel10RKGxKd2Aw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796483230; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Message-Id:In-Reply-To: References:From:Date:Subject:Fcc:Content-Type: Content-Transfer-Encoding:MIME-Version:To:Cc; bh=0aB+Dg3nrWyK7oaA0kcdbp7AVdQI35UdgIEB8vaM08A=; b=VhwUzAA4hIB5dDHrgVj+22MSEaZpdwmowY2kyiM2HSWt0H1QEPEN2XJeBKkTAzf0vTt5 eCxpxKf2h1jqzYVMxcpcO6VwLDeVXpLEWhxVv4gxcOnOYhjcVW9j+qVfJfzYtTH06f3K7 4fmUCcyIRUDbAAMltT2mSGi7KKU5MxXS5GwyGC4GQFcWIpyU1klvG4UsHmX5VAGWk1Bhd 3/LhaAKRgVKFUzabBajUDHbSGZRDbH7F2Mjk7Vsy/35RupgWr+z3d06aGSbRmEHDVLT35 QOb69gZHHcDPUyMBIeq5xagVtikEGAfuRDs8Pd+RkG686GjI9Yenwpbm3eoHGte1plA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fXkbVIYH; arc=none smtp.remote-ip=209.85.210.49 Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2D7B606A322 for ; Sat, 5 Dec 2026 15:07:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B2D7B606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fXkbVIYH Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-844a8bf7e25so1173949a34.1 for ; Sat, 05 Dec 2026 07:07:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796483229; x=1797088029; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:from:to:cc :subject:date:message-id:reply-to:content-type; bh=0aB+Dg3nrWyK7oaA0kcdbp7AVdQI35UdgIEB8vaM08A=; b=fXkbVIYHrYEA04LujYeSqVBbSRJuRL7sQkxebQCTGTT9VCzez9vsr0WH1ykeuyfS17 Ii9bD+MMVqfPUVp6xyuPDv52uP117CsnXpjG4QLKUW2kW2L8y9z7jPZIdzr3/Q+cGoFl 1qzDn9xoFrIclB4VMx5LfbTQnRJLT0HwZHcr7tdooIGgWuX5933HBmEXqDVILTpgEKVB wv4anMCNPxXvfGa9+qoY8w4TWg6mU0aJIV3yQJ2Ib/tBZ5D/+mNTPrf1iyXkmV/BH+Pd TkMI3E0q2K8mq3Rlt3uATniGkn2hTuQbM31pH/di3F8Uw5nsUlf2CWTXZwPxKnnXOzOh 6ihA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796483229; x=1797088029; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=0aB+Dg3nrWyK7oaA0kcdbp7AVdQI35UdgIEB8vaM08A=; b=Bjv8SrAcXT9AMfggPwImDQj+t5odD0WJW8Gan6gkfYcztMW6DzYjCDvPOWSfZDIBFo 5/yhEl/XiWyfO42NE9i0kga/fdYEfG4Asqd2cCFVPFIXhiw4bl+i9/toAuduLymsFaNA UrAYanfM9GGGavKppwVTArH6olT5+5F016mnWwev5+elE73iqiQuug1uyhihEH09k+nV GbtbwZppnoxHCgJVjzZv/8Rhzk8dai6pIzQs1sg4pBH2cFznOw1i/3m874BEV1YXpFLr LgYtf4qJphgzDc0PTI4YkvDEhkzhwHEAW25b/lhy3RRTZYSgQVIa8+Xb2DKvfYen2QD/ N/Pw== X-Gm-Message-State: APuU4Z+rq2fdEkaWIrZQC/D3mDHLiEFxxbE9BFwQUWgGF6yXQK9MI9U5 t5Qion7FJdcl1eXlfNS6W2BW/DAMrUpWhl2PF+KFjydxtWvlfo440bl6Vta4EooI19M= X-Gm-Gg: AZFdTZdlhoK8Et4nzrWGQu5rlS4GowSvH6akzoh2D+L3BNcaqoj6gop3gf233G5MZmL Mlig2BAb0DHcKOqafhEwom/7Rlc+GIMMtbxCCakiEbGrkLINdQ5vEZLS9YtTKtCWfJIDQfIg0/b nKKmH0HGTmDE3mVK8mot1dpRj8Fm+b42VAdpXPSJvOJGLIQvAYiGaDhZCkEoV+fp/lgpTYZX5Lb tqN0mF02oTbDnMI3M7rAQ2MIlh2OoTN45hjld5zLjih4lxRSX+nyzBh+bj2YzX8s0KCV1LAXukY D3APH4xb9eRmw6t2bW1ZyqXiUSZY1ZugBj5u6/N37g3+FfpaBzpk8rzPW29iacwzqW3eHpZ0ZS8 ruj7rAffzJYcV5hgedV4sGfODKvCINGDE1GwIivRoqicafcGokcO7eW63YMAaIMld37wkmNqjYC W5WJJZYLPRTmzC6bXyJ2fFX/Ecu6jFlJVztscRXD49lzMQxJi+Gv2BSDu9NZEGJc8bPTYdZRVNh SPWTAWcXVooHDkqv2/incxUiMEMTq30hajS21KKWPwj2+6Jn2G70ef8h6qQ+XaS37BdCM1+OV4n dExvD1ezLcQhkOLD7uGsEqvFDgCac5PqCg== X-Received: by 2002:a05:6830:924:b0:862:af57:78e with SMTP id 46e09a7af769-863f89ab67amr7418862a34.6.1796483228684; Sat, 05 Dec 2026 07:07:08 -0800 (PST) Received: from [127.0.0.1] ([52.165.103.180]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f7dd42a8sm5336939a34.0.2026.12.05.07.07.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 07:07:07 -0800 (PST) Message-Id: <9025fec315309a1b3e19799ecc58ef716ee114c0.1796483214.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Johannes Schindelin via GitGitGadget" Date: Sat, 05 Dec 2026 15:06:54 +0000 Subject: [PATCH v5 5/5] date: avoid overflow in approxidate relative-time multiplication Fcc: Sent Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 To: git@vger.kernel.org Cc: Phillip Wood , Patrick Steinhardt , Harald Nordgren , Simon Richter , Aniket Gupta , Johannes Schindelin , Johannes Schindelin From: Johannes Schindelin 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 in that logic, too. Helped-by: Phillip Wood Helped-by: Junio C Hamano Assisted-by: Claude Opus 4.6 and GPT-6.1 Signed-off-by: Johannes Schindelin --- date.c | 33 ++++++++++++++++++++++++++------- t/t0006-date.sh | 13 +++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) 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; @@ -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; } @@ -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; } @@ -1404,14 +1419,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