From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=202.12.124.159 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796527128; b=dHLKOXjNBBDXVaowI2PMHzFzsiWepSE23CXmQPt3fHon1QMbCpwBFr/raXed+zLBTFgT VbYIzPHkGDATvKsRL14Lnh8iN4B5PpJ9pIUBVINda6rI8vXp6T6czg5tsABNJ7MIc8ZMY ei5KFUGtt0urkk6mwF9TW8UBx8iZhrkuMHYgpbC25uXCKIYvGd9n736bBqWVenS7xyD+1 pbpdbbuhLAVxtttFvefo96dhaO4pSCKyNvCk7dQUzJYeBslMtIZTMjWyi2lA475t+swIj lJCkVAVqW+BxOKTJdyl7fd8ZHIZz0xwbdRHTwd7dd+AYsBLLkE0sAeC67jYhFTev7pg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796527128; h=DMARC-Filter:Received:Received:DKIM-Signature:DKIM-Signature: X-DKIM2-Info:DKIM2-Signature:X-DKIM2-Info:Message-Instance: X-ME-Sender:X-ME-Received:X-ME-Proxy-Cause:X-ME-Proxy:Feedback-ID: Received:From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: User-Agent:MIME-Version:Content-Type; bh=oGLoD/h2DQAiskVpsYG8ExjZL7WvbU8nZ3Ai8lNpK7Q=; b=e86JTSQJw9KCER1pfIc/bEmxwf13vJhTZa+tFEJw43d9b+PeO4AzxHj7a7bJQ4fDgHUA +vlnGgKy7nLS7DEp/OHoC9VCMgsKlupg/EbaKw+AqHZRpHsZFC/SB2/M6yS1kdKY6PYYd ylMxXANDjjjUITgMuLvv0Xjn3uHw6h4C7mBU90CyXcjgrubPTPg4eqsT2bhKsQtF7bITY mhEh1L2LOMqbsEROh17BURx+RUZ7lGwBw1ttO4DH/3xUm/pjsRY0XezqCwoYQccobcAGw 8Ut05WggTMvJ3s6bByZGiURUsa+dZzArb18P9ECZ1ilHed/th+L36plJMPcoVDDbObg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=pobox.com; spf=pass smtp.mailfrom=pobox.com; dkim=pass header.d=pobox.com header.i=@pobox.com header.a=rsa-sha256 header.s=fm3 header.b=OtoVuTbr; dkim=pass header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=YIKamXNu; arc=none smtp.remote-ip=202.12.124.159 Received: from fhigh-b8-smtp.messagingengine.com (fhigh-b8-smtp.messagingengine.com [202.12.124.159]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6742606A31F for ; Sun, 6 Dec 2026 03:18:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A6742606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pobox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=pobox.com header.i=@pobox.com header.a=rsa-sha256 header.s=fm3 header.b=OtoVuTbr; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=YIKamXNu Received: from phl-compute-03.internal (phl-compute-03.internal [10.202.2.43]) by mailfhigh.stl.internal (Postfix) with ESMTP id EF6A07A0116 for ; Sat, 5 Dec 2026 22:18:46 -0500 (EST) Received: from phl-frontend-02 ([10.202.2.161]) by phl-compute-03.internal (MEProxy); Sat, 05 Dec 2026 22:18:47 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pobox.com; h=cc :cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1796527126; x=1796613526; bh=oGLoD/h2DQ AiskVpsYG8ExjZL7WvbU8nZ3Ai8lNpK7Q=; b=OtoVuTbrMn0tk7QGvv/nRvhWDQ H7m6siaxD7ohikG/v/HLYtRoTbKmNKBb7uaXQtZgrwycznCJkGaNYc4pG+A+vw87 uh09y1w/nsfJd+ksgZR30F2FaH/sNvPwtxA/RQG7ImWRonnUWWz7g+p4ZAWGctrf xGEaDrC6gntOxDeZyazLJ+v0fspZPYLwGW+bfr8yWfwwiiOmASNImJhwTAhgaeOw IusQ31D6lzL//SC6kAbL8YCrzv/EStCvtwJz+d9LxrymFOniidyoEYl9UGf5EW+5 fzEIOKlbajI3KNcyEIiCOmCpmKAmJeBHKiebx5QO5wQ+2Ijvz86XNBzO13TQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1796527126; x=1796613526; bh=oGLoD/h2DQAiskVpsYG8ExjZL7WvbU8nZ3A i8lNpK7Q=; b=YIKamXNuem+7Jmm67kgD4i0RaLq6S+OLfYiNRubCOzgpLt7Xjy5 dyiSamkOmAsFcfxrs/6KUsq1lYdg5xapIYgIacSDKotkQGveMjEQeor28LqiSgwJ PXniq7bEJGT2nZQFvM2IL5QlOr1BsoqyUIbozJ+XZpYv3tZUfMEPUgoZ+6x0lmZo DpHQSsD/cMFz/ubRMOxbhddmz4rCsbtYTSYAgETvWR+4eUnVcymmwg8eT27LQN89 /h1NVP6O9i8w6ixQIIejBTwYDd8SI9sic+3kfGxvVBGEV6NboHMmDaLtnGlisfXn 0UqhghZZYo6u6mjrvwQ/mrTHEizSYTkSd/Q== X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=sign d=pobox.com a=rsa-sha256; DKIM2-Signature: i=1; m=1; t=1796527126; d=pobox.com; mf=PGdpdHN0ZXJAcG9ib3guY29tPg==; rt=PGdpdEB2Z2VyLmtlcm5lbC5vcmc+; s=fm3:rsa-sha256:eyXfTtJftFA9ctDJ2BATzqjUetbGKBwmJRtvuceolPtgVz7 H5cPt2YUyzhOJLlfj1uEcPSxtrDYhtCMma+YjDfKYYUxi8cPFSIZF7e8+UcBFCMY vDDWMflIwMa5QJ3wm3Xw4TGMy3gLrw2LlHAMSehzBQUhCc2LUijNuYlACZ2Z8Eb/ uX9tjEzPZRrxAdMoiw6Op2XXFvIDPXJrwp82Dp38GT9hprx0b1gT7/ZtQ6e/CV7U YwUs7ZhYTpDSd3hpoU+81KiVcO35Ef3FEE6K/uj41rFx7R6aOBT0Pvaz8VMytdP5 geoDM5j4uUdz/vcrMw3n7+8HJbEAuZACDT88oeg==; X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=mi-m=1; hc=12; hn=cc,content-type,date,feedback-id,from,in-reply-to,message-id, mime-version,references,subject,to,user-agent; Message-Instance: m=1; h=sha256:KrCnZaRE6Ax4r/+PuNj6bdW3Yo6h4iDh02RenaABAX0=:mfP+pkfd/7GmMYvWC4zdus+ll3XSNLKucR42UAMJX+4=; X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTEbd71n75PKec61Jfnvzh25drPSuOcIVTpwocG8nLrhAykqlHrkoiToWBcUFqncHs VUUgN34MbFU6rVpB5+ij+oCIdvIY/ueDWs0fFdOepViusIML4M1Ogtw+EqIConIHZ+u80X D7tRea32sF+2nqaKpdR7fmjqcdsvHL78P8fApi8gXlz0v9rZvYahGYNq2vqzotUzg5oHAN B9ADKAERCYvxAis/taeil8kRIjqHgsigxvKJB95lj+r4/W15qFxO2p0FfDYea6F4ZcmQoO q+Uhr9jRLEO4F20EzW7zQrNRyeTESd+gz311At1A+KKElJwBKnR+AF3B3PKiRv8UpNius5 UB9ksMpaz/KVoDU3dqtz5vRJLePFW4j1EYDuNIL52SmgyHWssBzoSZC0Y55qBK+c1eXCbK LzM0grn2/CdLcvHJr+K8IpEhz+ZhxbF/QUGEDgW1ouF0HJNmWIdi5AElDoLOS1gyTHHBiy 90CypwDdQ32Ddgvcqv5qxpJ9EeOSS/RgT0KfuEtO+dpTFhHVWJALtNtmAGyU7z7JXjz6lE zgwRzYeFoN5f8APXSxtYVI5ikWYWLNKFjb7HUhHJNC//a791JBuytPWa7uuqmxnPpf6Zax 42wfkhV0a27rOhFhM+JdQOzk0pUcQAzVBQ8UeioR46MJ+WV4Yk/RAwHXCV8Q X-ME-Proxy: Feedback-ID: if26b431b:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 5 Dec 2026 22:18:45 -0500 (EST) From: Junio C Hamano To: "Johannes Schindelin via GitGitGadget" Cc: git@vger.kernel.org, Phillip Wood , Patrick Steinhardt , Harald Nordgren , Simon Richter , Aniket Gupta , Johannes Schindelin Subject: Re: [PATCH v5 5/5] date: avoid overflow in approxidate relative-time multiplication In-Reply-To: <9025fec315309a1b3e19799ecc58ef716ee114c0.1796483214.git.gitgitgadget@gmail.com> (Johannes Schindelin via GitGitGadget's message of "Sat, 05 Dec 2026 15:06:54 +0000") References: <9025fec315309a1b3e19799ecc58ef716ee114c0.1796483214.git.gitgitgadget@gmail.com> Date: Sun, 06 Dec 2026 12:18:44 +0900 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) 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 Content-Type: text/plain "Johannes Schindelin via GitGitGadget" writes: > 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. I wonder if feeding the patch (or the resulting code) back again to a tool, either the same one or a different one, would have caught further issues. It might lead to a virtuous cycle, where you let Coverity make the initial discovery, have an LLM tool or two fix it, feed the corrected code back to Coverity, and also have an LLM tool or two review the patch text, while you also look at the code to see if there is anything missed. And lather, rinse, and repeat until everybody in the toolchain and you are happy ;-). > 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; Because the earlier check in approxidate_alpha() already returns when !*num and approxidate_digit() rejects values above INT_MAX, *num is in [1, INT_MAX] here. Performing the multiplication in uintmax_t and checking unsigned_mult_overflows() plus date_overflows() avoids signed int overflow while still allowing large offsets such as "4000 weeks ago" on 64-bit time_t systems. > @@ -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; > } This fixes the issues in v4 cleanly. Because '.tm_wday' is initialized by localtime_r() in approxidate_str() (and preserved across update_tm() and date_never()), diff stays in [-6, 6]. On 32-bit int, max_days is 24855, so max_days - diff is always positive without overflow, and any 'n' passing the check keeps diff + 7 * n in [1, max_days], guaranteeing diff * 24 * 60 * 60 fits in int. This would limit weekday offsets to below ~68 years even on 64-bit time_t (unlike "4000 weeks ago" above), though that is harmless in practice. > @@ -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; > } Rejecting numbers above INT_MAX before narrowing to int also prevents inputs like "2147483648" from wrapping to INT_MIN and leaking negative values into pending_number() or approxidate_alpha(). This round looks good to me. Thanks, will replace.