[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260120094258.41313-1-david.desobry@formalgen.com>
Date: Tue, 20 Jan 2026 10:42:58 +0100
From: David Desobry <david.desobry@...malgen.com>
To: tglx@...nel.org,
mingo@...hat.com,
bp@...en8.de,
dave.hansen@...ux.intel.com
Cc: x86@...nel.org,
hpa@...or.com,
linux-kernel@...r.kernel.org,
David Desobry <david.desobry@...malgen.com>
Subject: [PATCH] x86/lib: Fix num_digits() signed overflow for INT_MIN
In num_digits(), the negation of the input value "val = -val"
causes undefined behavior when val is INT_MIN, as its absolute
value cannot be represented as a signed 32-bit integer.
This leads to incorrect results (returning 2 instead of 11).
By promoting the value to long long before negation, we ensure
the absolute value is correctly handled.
Signed-off-by: David Desobry <david.desobry@...malgen.com>
---
arch/x86/lib/misc.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/arch/x86/lib/misc.c b/arch/x86/lib/misc.c
index 40b81c338ae5..c975db6ccb9f 100644
--- a/arch/x86/lib/misc.c
+++ b/arch/x86/lib/misc.c
@@ -8,15 +8,16 @@
*/
int num_digits(int val)
{
+ long long v = val;
long long m = 10;
int d = 1;
- if (val < 0) {
+ if (v < 0) {
d++;
- val = -val;
+ v = -v;
}
- while (val >= m) {
+ while (v >= m) {
m *= 10;
d++;
}
--
2.43.0
Powered by blists - more mailing lists