🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #478

[PATCH 5/5] date: avoid overflow in approxidate relative-time multiplication

#478
From: "Johannes Schindelin via GitGitG...
Date: Wed, 14 Oct 2026 06:09
82 lines
10659 bytes
From: Johannes Schindelin <johannes.schindelin@gmx.de>

The `approxidate_alpha()` function multiplies a time unit length (e.g.,
604800 for weeks) by a user-supplied number to compute a time offset in
seconds. Both operands are `int`, and the product is passed to
`update_tm()` as `time_t`. For inputs like "99999 weeks ago", the int
multiplication overflows (604800 * 99999 is roughly 60 billion, far
beyond `INT_MAX`), producing a wrapped value that results in a wrong
date.

Use the checked `time_t` multiplication so oversized products do not
overflow or wrap around.

On platforms where `time_t` is unsigned (and where the pre-processor
allows us to determine this fact), reject counts that are too large and
report a parse error. Narrowing them first could otherwise produce
negative or truncated values that become bogus offsets. Leave signed
`time_t` count parsing unchanged.

Pointed out by Coverity.

Assisted-by: Claude Opus 4.6 and GPT-6.1 Sol
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 date.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/date.c b/date.c
index 014065b419..0d65804d2f 100644
--- a/date.c
+++ b/date.c
@@ -1298,7 +1298,18 @@ 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);
+			time_t length = tl->length, num_t = *num;
+			time_t seconds;
+#ifndef is_unsigned_type
+			seconds = length * num_t;
+#else
+			time_t max_val = is_unsigned_type(length)
+				? maximum_unsigned_value_of_type(length)
+				: maximum_signed_value_of_type(length);
+			seconds = mult_overflows(length, num_t)
+				? max_val : length * num_t;
+#endif
+			update_tm(tm, now, seconds);
 			*num = 0;
 			*touched = 1;
 			return end;
@@ -1368,8 +1379,15 @@ 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) {
+#ifdef is_unsigned_type
+		if (is_unsigned_type((time_t)0) &&
+		    (number > INT_MAX ||
+		     number > maximum_unsigned_value_of_type((time_t)0)))
+			return NULL;
+#endif
 		*num = number;
+	}
 	return end;
 }
 
@@ -1398,6 +1416,10 @@ static timestamp_t approxidate_str(const char *date,
 		if (isdigit(c)) {
 			pending_number(&tm, &number);
 			date = approxidate_digit(date-1, &tm, &number, time_sec);
+			if (!date) {
+				*error_ret = 1;
+				return 0;
+			}
 			touched = 1;
 			continue;
 		}
-- 
gitgitgadget
Technical Details
Message-ID: <a4e602b9e726081aaaf73c627548b823a4d35477.1791958171.git.gitgitgadget@gmail.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-71937-linux-kernel-git=dewy.ru
References: <pull.2253.git.1791958171.gitgitgadget@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-71937-linux-kernel-git=dewy.ru
From: gitgitgadget@gmail.com ("Johannes Schindelin via GitGitGadget")
Newsgroups: linux.kernel.git
Subject: [PATCH 5/5] date: avoid overflow in approxidate relative-time
 multiplication
Date: Wed, 14 Oct 2026 06:09:31 +0000
Organization: Usenet Network
Message-ID: <a4e602b9e726081aaaf73c627548b823a4d35477.1791958171.git.gitgitgadget@gmail.com>
References: <pull.2253.git.1791958171.gitgitgadget@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2230606"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:RvD3Qly2HSgJ1LXyBbb0tu70qyI= sha256:KKNLphb9R1SCWbggsOzvpLJCZp4wbpFcY8XkbQ99FRY=
	sha1:KhvD+UeT1D0+ltAwUzUdiLpNEV4= sha256:rVTxG+BbfQFoQRcDwtHtOnYsXiCWauos2zIyxRIgN0Q=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 7577210FFF7; Wed, 14 Oct 2026 09:10:26 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.048
X-Spam-Status: No, score=-3.048 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id 5700y_cC21jT;
	Wed, 14 Oct 2026 09:10:23 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=git+bounces-71937-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id DEDBC10FFF6; Wed, 14 Oct 2026 09:10:14 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
	by tor.lore.kernel.org (Postfix) with ESMTP id CD83440B69; Wed, 14 Oct 2026 06:09:50 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 12F3B412266;
	Wed, 14 Oct 2026 06:09:42 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SPimDCv7"
X-Received: from mail-qv1-f48.google.com (mail-qv1-f48.google.com [209.85.219.48])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id BAB48417D86
	for <git@vger.kernel.org>; Wed, 14 Oct 2026 06:09:39 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.48
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1791958181; cv=none; b=owDP16X5cDQX4iXHmnhyzJaNMC4WLrjy1LSOrOKhVocmjvX5NRS/WTaQRmgRwqIl/8dJf4espj42ZkIt8DbRIAiKNV4QdqyCg5+BMKB13A9jDNzenfDj4s2IYpkbxUybrALveSEthAAMaZ1eXBalosqxras4IQj3FXIbIaHwiF8=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1791958181; c=relaxed/simple;
	bh=GMlD/eBWQxrQxY7TEc8HRIqLeLZ+w1njCUQYQXsEFOs=;
	h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type:
	 MIME-Version:To:Cc; b=ZgP1HSsbZO89X00OXpAXD1Ddzhqrd60rvSsmrq5d5W3JGAnHpI8tWkgy83teeap6m/QZNsHLj3zuETRXxNm1qwb1+0Xp0jh2eaw0HVNreKuC3WwE1mBPrZJmKqUM3PECe9cSoagrZ3iUis6prCK182EmBOLyV5G0/Kd79+D0txk=
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SPimDCv7; arc=none smtp.client-ip=209.85.219.48
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-qv1-f48.google.com with SMTP id 6a1803df08f44-9144e8021d3so223856d6.3
        for <git@vger.kernel.org>; Tue, 13 Oct 2026 23:09:39 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20251104; t=1791958178; x=1792562978; 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=xsfecZSkKeiQAqQP7AWZJhO9hHpfIXAXhhCEo3qqqqQ=;
        b=SPimDCv7z6x0uLt/0U0j4Aa2GnLxZxYAZQxjBcA+jlxTyRoFXR0nR7M565pPEQUhe7
         MIHw3pJMKdRhfqO9ouxRZPbaTnPJg8YkrCV9hCwcI5uPuxZX00i19gGwA71u+7NgaTak
         sEzeGkaEPg/xjPs4KMrqWrgVp56zDaqRUQaRSOdsjrj/Zx1tn2W3NEHjUtiJgTJzexc2
         SMgiD15eOR8QdCTWhM2TsFWG16CCOS8fTStY1isHT4nc/8anM0XSavnf9n01r1cN+p2e
         BRMZgTNTfvL1BUNyRF/MeCnrbHQWrEq+sxcN1NmlZKA9WPhQ73s2+PmLuWl5zR6pf/47
         2c3A==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1791958178; x=1792562978;
        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=xsfecZSkKeiQAqQP7AWZJhO9hHpfIXAXhhCEo3qqqqQ=;
        b=k4CbIynmvA+GZNkrKVpak/Jtnm7GMo+0ZhlwR2rAOFEzInbk2O1s3PNAE8B6hxPsKt
         B+/LsizsFc3wNPRPIOxZVmKsWs7vBwexOeTeBKaaZjS/7ShBf+atldSNAF4F+a/iM+vY
         GCvxA4QCKR1HOc1mNGiQnTMJtN9uQ8YvTUlC8FfbySemVzkjMkCL2RjPbT80EMzNV6fL
         SbQEq6luYmXHxI2u0nBzTtqT2rWGUPZvEMO0BooW+0n9vWAY7MbULrMOkNrpc/fp0v+S
         /1PbJnHBPAr1y0zoccIIqDPe5LckDyk7IVHHIOaIKCdAhIvL0pW2eI2ge5VZNmqhMIYM
         FNFg==
X-Gm-Message-State: AFq9FYJZYIhd4Y+rnJdd3XnUYzTgraTbrTellwYvZ5T94okqg28VciuF
	FbWfFMdMPtLF4y6L0AdSIz9y0A3QmKg/+n7cPHK9qtdOFculT6JB8y4L7fow3g==
X-Gm-Gg: AYBFou1Z+T67dtkeaUJz8xaxEl3PoG37TIr3Y52BMzmXuPNWKnrup7i1f4WCMGMhlQh
	zt0cGm8OVCTtB3Voe8VUW0ZmJlg+ZWiLVQDjIMo9RVDKEXUQCAg/DquXHktYcuyTrCWFU3aXf7t
	ymsqMOy4ACZNcfG9pF29zQp6/3x1HaRWszDmYGRJIyy91em856EldF9wcthDRRbfwNUDt2XzWQp
	zG0WtFiDEMDlfsJmtQMD3dtPc2a2xXL/zn/+a9nm5dR6S8iIT2t+oj7mUDXTiaDcG7vAU5sHdae
	VSHCzSFn4uixqBpAAQzAXmQ3/fZlf/68mwOOMy7Nz6+u+23iD/1gbPYpbXgXfYsQ+OKe08pOyLS
	JwK9f7/8D0bdLDr0SmGaDhnM/R7TPyDOLkkFhrXvjj4CHC2p2e31/JchLV8qlYW6UkC6zGYkpzQ
	/51OV0VOSUDLB4NwEnDBrqSiydxMe+cgz+T8Ym9FYVwcAL9N682MbVJjSbtD+4alHx+4TAS+vTc
	5uZNg/Orls=
X-Received: by 2002:a05:6214:5f0a:b0:919:867b:5215 with SMTP id 6a1803df08f44-91b55334746mr242100196d6.3.1791958178520;
        Tue, 13 Oct 2026 23:09:38 -0700 (PDT)
X-Received: from [127.0.0.1] ([9.234.149.85])
        by smtp.gmail.com with ESMTPSA id 6a1803df08f44-91b97b622bfsm44784116d6.24.2026.10.13.23.09.37
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Tue, 13 Oct 2026 23:09:38 -0700 (PDT)
X-In-Reply-To: <pull.2253.git.1791958171.gitgitgadget@gmail.com>
X-Fcc: Sent
X-Precedence: bulk
X-Mailing-List: git@vger.kernel.org
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-Cc: Johannes Schindelin <johannes.schindelin@gmx.de>,
    Johannes Schindelin <johannes.schindelin@gmx.de>