From patchwork Thu Feb 1 00:10:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Szreder X-Patchwork-Id: 195105 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:106:209c:c626 with SMTP id mn5csp113182dyc; Wed, 31 Jan 2024 16:23:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IH/mku+OC3WNIVAhKUri4mggH+hSINYwPfTZYZdqnjsyZKZf8ZR5QVUWDDkXkcUe6kqzrsI X-Received: by 2002:a05:6214:486:b0:68c:385b:ac93 with SMTP id pt6-20020a056214048600b0068c385bac93mr1032391qvb.59.1706747002002; Wed, 31 Jan 2024 16:23:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706747001; cv=pass; d=google.com; s=arc-20160816; b=kH+DfAVE0C50H0E9UieLKDAbSlNYxAfOxU7vDUn368jAQL2LyjPRrcfsQTaGFpCArY /yfKxgHchdL9lNJ3LzChMBlrdePOcAdTLRvz98cW/WHCCFXqhZKXkHBAIjXKkSkZrTL0 lrQVzbupC3GSu77xuWEo88VGhAhZXgpsmJ6JuwfsUa6jfMAQoCUcHHEHVEVCy+PPaXBv JNPNNYFMM0OZkeErwC5M8Zs1joJqA00mnYREynBGPIzxYDXpiuuCgbdy1NaoZwm4jvUD EzBhf65+HK7DnSBORTQo+hgNxjzJDuQ0evjM//WmMdqijeT2IrFPBYVxLN0LPGexiG6R 7yew== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=4Lck4P3n60kDkUB+8cduNcdibbAkEnn4c9TsGapgrQk=; fh=8jJdW23CLVzTM4yCRpQuyXwVJLZAm9ns7ySfmnP7R38=; b=Qh+/Ize2Qi+kxPlTj+MTTpffvsmfWp1gyPPLwtxvdnLpJRlitguTkKBw7/ScdaxlXx /ifamGuWVDkORXT+yO9z56SpEke1OWwrfh+etliRYEe0kacBDY3X73+1xGOpJWr83iL1 20ptEXk4BLDnh8h7Pk4Mxe5vaUpHiU5ZPg5ykYAXmyXMxhP6eCgx+GskXVWfpBKrFZzP CRYzBuAepI0Cw2PveMHYfoDt/QQRQo6w+paPey8++c18MunUkBT/UJKQmXWxk0q7kqmw zGhkWeP1+MAG+dHXqlw5dsUhGDaYKfrcsdnpFs01z3sQG44MvvJdscZbC68GXRkASsC/ K4qw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@zgredder.pl header.s=default header.b=mlUTHJCp; arc=pass (i=1 spf=pass spfdomain=zgredder.pl dkim=pass dkdomain=zgredder.pl); spf=pass (google.com: domain of linux-kernel+bounces-47426-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47426-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCXBh/wJfjkPRwhjpwgwRAJCRkN3Q19IDinpNkpOFkKc9au0ZK8vpR8BdrNJO5GnXBV1g80S6yVIWhAZNyfGymnEL5J6Wg== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id iy13-20020a0562140f6d00b0068c4b5da3c4si8689913qvb.35.2024.01.31.16.23.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 16:23:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-47426-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@zgredder.pl header.s=default header.b=mlUTHJCp; arc=pass (i=1 spf=pass spfdomain=zgredder.pl dkim=pass dkdomain=zgredder.pl); spf=pass (google.com: domain of linux-kernel+bounces-47426-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47426-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C53131C25E8E for ; Thu, 1 Feb 2024 00:23:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8DBB61851; Thu, 1 Feb 2024 00:23:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=zgredder.pl header.i=@zgredder.pl header.b="mlUTHJCp" Received: from mail1.hosting.lp.pl (mail1.hosting.lp.pl [194.1.144.125]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF050374 for ; Thu, 1 Feb 2024 00:23:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.1.144.125 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706746989; cv=none; b=U63oflzy3JiNORdvLL9NTTGjywmh5/96tTqFN6cOH8JOnHGao6+uQzSFaPlOeHYhcvlMBDWE5coKWRJbOFfsS+n/jVG6GGJAM0PvX7uSeYg8c+z8Wnq5VAjfFOoZpriSeJjbKvg9xIGqHLtCn41GtayqigXaXJKEg9I6D8OEK0g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706746989; c=relaxed/simple; bh=8EQCJ0zRWIJAH4B7S7rBKKBSElX4FaQmCDWO7VGXYbY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Z6CoCDYqIe+bIgTG0atz6L746wXSzT6JR2bkQeC2/Cv1p6iI2ojEKcAcDF8t6B4NW5DpqrxKw2zQC251rz4swuFtEIWPI4+fMPeq6DJBkQocRj9yFKF2Q4D3s4KCBEgauGu6u0tXg0F1tWINqq/gANyVMnJ6b9w5qGyWoU8DHVg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zgredder.pl; spf=pass smtp.mailfrom=zgredder.pl; dkim=pass (2048-bit key) header.d=zgredder.pl header.i=@zgredder.pl header.b=mlUTHJCp; arc=none smtp.client-ip=194.1.144.125 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zgredder.pl Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zgredder.pl Received: from localhost (localhost.localdomain [127.0.0.1]) by mail1.hosting.lp.pl (Postfix) with ESMTP id EA55C88B69; Thu, 1 Feb 2024 01:15:12 +0100 (CET) Authentication-Results: mail1.hosting.lp.pl (amavisd-new); dkim=pass (2048-bit key) reason="pass (just generated, assumed good)" header.d=zgredder.pl DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=zgredder.pl; h= content-transfer-encoding:mime-version:x-mailer:message-id:date :date:subject:subject:from:from; s=default; t=1706746511; x= 1708560912; bh=8EQCJ0zRWIJAH4B7S7rBKKBSElX4FaQmCDWO7VGXYbY=; b=m lUTHJCpwmkpihcMCOMRM5IQmeGl0jDsHesvRAiesKDRIC9pH4HTv5mgDS2e/fYnj pM2tVoCHwtCZtzR84W47Qi4I4+rCNMJKUgp+IKaefSWi0upzZSilmUCYwlmyLjKI 5jtA7eDZHY4KFgPxay+twisDkOwOfvq2LN7UG08ckGjzn/WFljNLT8wKFibfOhQA LercHP6fDtfP3+qHaEB7D/4GQe+F71sbXZHj9iCXm2bU2jxu8z4sclbvC+aOXEKe t2Xq2FZaT6HD7IG5rU6R7UBQ4vgEk1DKZ50kP6Y4PRQKIEMk9ulDfj+e14ps4Ibn c8RdGfMSXGsseyTq3FCaQ== X-Virus-Scanned: Debian amavisd-new at mail1.hosting.lp.pl Received: from mail1.hosting.lp.pl ([127.0.0.1]) by localhost (mail1.hosting.lp.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id FBzgoY_-3Q_q; Thu, 1 Feb 2024 01:15:11 +0100 (CET) Received: from localhost.localdomain (31-178-92-228.dynamic.chello.pl [31.178.92.228]) (Authenticated sender: zgredder@zgredder.pl) by smtp.lmail.pl (Postfix) with ESMTPSA id B693788A5C; Thu, 1 Feb 2024 01:15:11 +0100 (CET) From: Bartosz Szreder To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, colin.i.king@gmail.com, linux-kernel@vger.kernel.org Cc: Bartosz Szreder Subject: [PATCH] x86/lib: Avoid undefined behavior on sign flip Date: Thu, 1 Feb 2024 01:10:33 +0100 Message-ID: <20240201001033.9483-1-zgredder@zgredder.pl> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789653944376071391 X-GMAIL-MSGID: 1789653944376071391 If argument to function num_digits() is equal to INT_MIN, the sign change operation results in undefined behavior due to signed integer overflow. Avoid that issue by converting to unsigned type. Signed-off-by: Bartosz Szreder --- arch/x86/lib/misc.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/x86/lib/misc.c b/arch/x86/lib/misc.c index 40b81c338ae5..778ab6d27372 100644 --- a/arch/x86/lib/misc.c +++ b/arch/x86/lib/misc.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include /* * Count the digits of @val including a possible sign. @@ -9,14 +10,17 @@ int num_digits(int val) { long long m = 10; + unsigned int tmp = val; int d = 1; if (val < 0) { d++; - val = -val; + + if (val != INT_MIN) + tmp = -val; } - while (val >= m) { + while (tmp >= m) { m *= 10; d++; }