From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=103.168.172.146 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796448823; b=XJz2okrLURObGCrdPkPg/kh00htGSsGuyVxiQCKEprjXC+HIrmEZwlXtMQKQwTzZFyM3 +ebB5CRMBaxivu0frYkdRHfRPzXJQKFEOI/hH0f6EXSoyI4pYKf3s5dxbHl0zBejSzHOv Fd8sxoBCe5YCnuyIRbmZkJnPj4V4XVqUB+gwnl0z9LbWvaFGHvI9Ms11qH6J+QCckZs+6 Qk9qt1a7THYt5eLDdPG5JQbcU8A3cFWPdiY8JfGR0BgwvHU/VWGbfgfrt8WlGhlJz+mpU ysSH249yFF1D8DgqsfjShl5yp+9HLvd06teQ27Z+ArcjsENIVNQ4V+VQIr6wyzRldaQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796448823; 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=pv8XHC8Bac6YDHyJpa5/djNeziUD5KDQe8OjhC7uW5E=; b=m6XobyPYxY01+NtzevmS1uG6IMt3En4CXvMqg32F7coDfz1tb8CTTgv4FdNcA9A1wvDL 9Si89wPfZVOv3Dm10ThahTSLdGKIvMgxRDElxcrlTd8+hz8F9KqfumMzV9NZfoh/vmL2J viQjYRdRa9V5AJX8X9xizkOSv0PREMrLPF+Dwn+dWk/DFpxHXGaQir5jajqH61kkOpctQ JKG2aAHIsC43IA/3hT6g056Pgdiyy8xud4XEg2yOYt2s0LJDTPfKxlTh7upcap+DRYBLU T5F0C8Qmxpjl8aISovCrmfsEjOHJTARtt48hfKooXEycYp/uSOHmPGZKMOB6Z71RFLA== 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=Nhu29wMV; dkim=pass header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=3KECIMnU; arc=none smtp.remote-ip=103.168.172.146 Received: from fout-a3-smtp.messagingengine.com (fout-a3-smtp.messagingengine.com [103.168.172.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39102606A322 for ; Sat, 5 Dec 2026 05:33:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 39102606A322 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=Nhu29wMV; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=3KECIMnU Received: from phl-compute-07.internal (phl-compute-07.internal [10.202.2.47]) by mailfout.phl.internal (Postfix) with ESMTP id 65977EC01AF for ; Sat, 5 Dec 2026 00:33:42 -0500 (EST) Received: from phl-frontend-01 ([10.202.2.160]) by phl-compute-07.internal (MEProxy); Sat, 05 Dec 2026 00:33:42 -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=1796448822; x=1796535222; bh=pv8XHC8Bac 6YDHyJpa5/djNeziUD5KDQe8OjhC7uW5E=; b=Nhu29wMVCe1iXN932IkY4RUIcn zcEx50hqOzV3QiwGP1PupZDqNdIGvw8EJ9tDKzk0j2FW6pTxZch9D4WikGkLbUeu O9xH+T460SxAQmp3iFOBsxCd8sJ+/2fPgZT05NM+VzHwnHl1vj7R9n67usPQLi76 Ml9rRvgTPAAn42/mM1q7L540bdn8qOp/Ekp3XUYq9g6VZjoCtQlt3QnIzXtxILBi ay3YcIrfENPxXKLJcDQSbfXhDGtYEs2Lrk9uaFs/8YW+tnAgZ4S+WzZwP42tQaOe mEjaMdMETGpb2BHRlvpdbrdeIvYtBj5AYAYTIMvnLA6eNfeUKHHW7Swuf0iw== 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= 1796448822; x=1796535222; bh=pv8XHC8Bac6YDHyJpa5/djNeziUD5KDQe8O jhC7uW5E=; b=3KECIMnU8VJAbcudp1q5dnpPGhTZLVtrsDnkX7GPj0uiOyeCIet 4nlWshhSw6yGqwnHQu1DcFmTQ46NgQNO5DhlwCiixIhwh6p5d6lrh0AkxoNk1eln MG0SZOlPhQxn56UPgjSlqlLcp7zJqb0MH5SivJj9ibym5T9XdF/giD014cK2hFfR NuLvx4h7+CuTeKFrnaIw63ztCGm5oAGCFiMJTeX+olFi4xdCylyc/Yo7ahnHuCvA rzPwbDWwsFN4OnIo6UTu76Q/FZlNfJy2e6JlNV1X/Sqntyn37Ej5ZyVixCfL0CjV a+jkQmSB5GM3tWYns3qNs4Gg4FJA+HU+gQw== 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=1796448822; d=pobox.com; mf=PGdpdHN0ZXJAcG9ib3guY29tPg==; rt=PGdpdEB2Z2VyLmtlcm5lbC5vcmc+; s=fm3:rsa-sha256:exyj2YKH+c/yqj7wp8DFetBy5vIK5V+gUF7Qx55aQB+SfR2 S41Y/GEOQ5Qu5Lm8WFSV/4PEfkvHtFUe7AJ60ErnVHRNvPUmOaSU/uY4anavaAAg vWm4PUW8kxnrNRKz3smvF3maeWnQj83oz8OyfDVc2uDmO5pnF8mjw/YL6HEpwu3t SdYAwfUPCp9LCeHpvovlKhoBjtBhTdrEBJyJm9H1+KZLRNpc0tY7WNjtm7ObsieX L7mVysTLUj2Yozw1mbwk2nhIwJ5SgQllNEF6PAXuSpEpQOWHHf2qPO/VrZ0ox15W +K96TjOs7pPwTTBAAfgUIBqEf/sst681mZaKMKQ==; 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:rY/HStdLn3k3gxCFID0AiHQOAKovN6KKDga0UNDpKzo=:9f2KxHn/nwlDlzR9yr4FlpfL0UT14FYYmG6z49IDvAU=; X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTEuDCpnkVHuebXZkxiRmDv7i2bQ0CbyKyhoriZWk9PpGTrGupO8o1Y9JgI+yMSU8C AaXdsLz1AqxfPrbqAU86U90L8t9SY82teHZaMaFs9lSZlrZWQKPEqxm39N/6/RgY3oVce+ FPXHH03o8NnChxPJ8sn65KNBUH6snVmhD05nihWsOeLX0cvVonn5nDC2dStuU004JgjdwR fn6EMEHbNqZGytLVzTXcSDx2MfPRL3h0TMeVK3srJGcgmiD9ZBfbHrH4xOZWFWGTGIieOb HoGZ1HdaynIYVcAfdVhxxJxNLySjCS1bDSzvf4Xo6oajgZXD+Bh2XYzZeXpEvHEeNn5Mkj 3aovYtOKVNkuRJT+3ecgjgl/hzeG7ZtnRnhuoONM+0Nf3uCDbMA3yhSlSxiHnrSZhgXQgb /l5yZf6pZAbTHNBbmeKx0EA6sO3eT3pwxRcE2ULuNRgj89p4siwTR1yiJ9FnPW4oEiPufG OaMXQ4A8Ipw4bBIvrBv0LO0bvsLiaNwttWO1GaxOQfsmN6KPoQkPS83Y1tPbuOi7d3gJdQ 8hVZ3WbqXWhHZnjuppBQsvULnifB6eAjlVpiv3kDTrcCaJLyA9wp/xPBMXP/+xInwrVmB2 xHC1OSf5K+lhSh7kPLMUcH3da1bHgkb0qIhHENRWGzk8WRWWBexxYK6Blxig X-ME-Proxy: Feedback-ID: if26b431b:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 5 Dec 2026 00:33:41 -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 v4 5/5] date: avoid overflow in approxidate relative-time multiplication In-Reply-To: (Johannes Schindelin via GitGitGadget's message of "Fri, 04 Dec 2026 14:35:42 +0000") References: Date: Sat, 05 Dec 2026 14:33:40 +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: > @@ -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.