From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.zytor.com (terminus.zytor.com [198.137.202.136]) (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 A257644A71A for ; Tue, 20 Jan 2026 16:23:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.136 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768926240; cv=none; b=vCgCI7BCUworq0Mk9k4lZEBPRjo079A22R4ptGypCaOH7KiLx7Fn1jmYv60b2yau5oX5+GxGvl1SjTzr0uUz63nO1BjNleugvugaxpNbmwvUHQFMVKvkXiLMrqCvCH5iYXTx/iFjH1RCU2XB7M7z0jS/uAGxLEEW1fK7+0N7JcU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768926240; c=relaxed/simple; bh=6d7rwSuCPDfFK4/vd7y3m0HmtPFppq46GNnbwroK9Eo=; h=Date:From:To:CC:Subject:In-Reply-To:References:Message-ID: MIME-Version:Content-Type; b=lCP/9fTgCTHzkC1NOJ+e/aYo7MW6nIpJFhmFLPCF1mKMsmOkPz+IG2xGwvF0YBR+wr/6/BUhBYw8sFcIHZJUdap46PdQqJJ+ctXDNH8X2V9E0gtsTarUmGYUK4GvkalUC36ZkeZkmy+RSpaI9sWXqERnaaqoJYS3iUbAnd0dWCk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com; spf=pass smtp.mailfrom=zytor.com; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b=J2DpvkeZ; arc=none smtp.client-ip=198.137.202.136 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zytor.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b="J2DpvkeZ" Received: from ehlo.thunderbird.net (c-76-133-66-138.hsd1.ca.comcast.net [76.133.66.138]) (authenticated bits=0) by mail.zytor.com (8.18.1/8.17.1) with ESMTPSA id 60KGNMaH3769000 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NO); Tue, 20 Jan 2026 08:23:23 -0800 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 60KGNMaH3769000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2025122301; t=1768926204; bh=Whq3LnVNJvtVeAVvp/KuCK7rYK8cYMbqEzNJiw8Dl14=; h=Date:From:To:CC:Subject:In-Reply-To:References:From; b=J2DpvkeZrw5lX6AeK01FvIN9+psxTsUrmmYCT0Vwi1E00mafio4KxmoevKWQ0RCx6 Az7lUripxUkT3w1Zlg4Pbdz+n00kmk9Wc4EXmVTcCOf30EFbpogfSO8r9bsUsbaYev ep8J5ynaL5fMJKBp6wLMLinDhvXLzCwEF4hsfMYjO0fMB3C2Ja6npj0/B/YcjLAQtn Elo/UG7H/Q504Ybsq9yx6iJ0GwlvCaYszlHvV9JoDvV3U16kpl2QGxqmHkEYGu9ZLw Nathcv9jvzMaOE+O/M4DtDbL9nori2D/jMhq0v3UTl5FAviWY1ry47D0HmOlcsHRbO CzJUUMfXP3R/w== Date: Tue, 20 Jan 2026 08:23:16 -0800 From: "H. Peter Anvin" To: David Desobry , tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com CC: x86@kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] x86/lib: Fix num_digits() signed overflow for INT_MIN User-Agent: K-9 Mail for Android In-Reply-To: <20260120094258.41313-1-david.desobry@formalgen.com> References: <20260120094258.41313-1-david.desobry@formalgen.com> Message-ID: <53DB5B87-BA96-44B1-AE1B-9055CB7B9350@zytor.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On January 20, 2026 1:42:58 AM PST, David Desobry wrote: >In num_digits(), the negation of the input value "val =3D -val" >causes undefined behavior when val is INT_MIN, as its absolute >value cannot be represented as a signed 32-bit integer=2E > >This leads to incorrect results (returning 2 instead of 11)=2E >By promoting the value to long long before negation, we ensure >the absolute value is correctly handled=2E > >Signed-off-by: David Desobry >--- > arch/x86/lib/misc=2Ec | 7 ++++--- > 1 file changed, 4 insertions(+), 3 deletions(-) > >diff --git a/arch/x86/lib/misc=2Ec b/arch/x86/lib/misc=2Ec >index 40b81c338ae5=2E=2Ec975db6ccb9f 100644 >--- a/arch/x86/lib/misc=2Ec >+++ b/arch/x86/lib/misc=2Ec >@@ -8,15 +8,16 @@ > */ > int num_digits(int val) > { >+ long long v =3D val; > long long m =3D 10; > int d =3D 1; >=20 >- if (val < 0) { >+ if (v < 0) { > d++; >- val =3D -val; >+ v =3D -v; > } >=20 >- while (val >=3D m) { >+ while (v >=3D m) { > m *=3D 10; > d++; > } That has got to be the dumbest possible implementation of that task, bug o= r no bug=2E A switch statement would be simpler and faster=2E