x86/lib: Fix num_digits() signed overflow for INT_MIN
PATCH 1/1 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.
--- arch/x86/lib/misc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/lib/misc.c +4 -3
@@ -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++; }
Signature · 1 line
2.43.0
H. Peter Anvin
reviewerQuoted 27 lines · David Desobry
In
num_digits(), the negation of the input value "val = -val" causes undefined behavior when val isINT_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 <[email protected]>---
arch/x86/lib/misc.c| 7 ++++---1 file changed, 4 insertions(+), 3 deletions(-)arch/x86/lib/misc.c +2
-1@@ -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 ) {
arch/x86/lib/misc.c line 23
Quoted 10 lines
arch/x86/lib/misc.c +2
-2d++; - val = -val; + v = -v; } -while (val >= m) { +while (v >= m) { m *=10 ; d++; }
That has got to be the dumbest possible implementation of that task, bug or no bug.
A switch statement would be simpler and faster.
Borislav Petkov
maintainerQuoted 1 line · H. Peter Anvin
That has got to be the dumbest possible implementation of that task, bug or no bug.
LOL, did you read the comment above that function?
ROTFL.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteH. Peter Anvin
reviewerQuoted 7 lines · Borislav Petkov
Quoted 1 line · H. Peter Anvin
That has got to be the dumbest possible implementation of that task, bug or no bug.
LOL, did you read the comment above that function?
ROTFL.
I did. No wonder it sucked! 🤣
H. Peter Anvin
reviewerQuoted 10 lines · H. Peter Anvin
Quoted 7 lines · Borislav Petkov
Quoted 1 line · H. Peter Anvin
That has got to be the dumbest possible implementation of that task, bug or no bug.
LOL, did you read the comment above that function?
ROTFL.
I did. No wonder it sucked! 🤣
Seriously, though. As Linus likes to point out, there is a huge difference between "this is stupid" and "you are stupid."
I have said "this is stupid" about my own code more times than I can count. For good reason.
To be honest, I didn't even need to see the comment to know that that was probably my code in the first place. I recognized my own style at a glance; in particular the kind of code I tend to write specifically for the purpose of small as opposed to fast code. I remember writing this code while waiting for a table in a restaurant :) but I don't remember the context.
I was in a hurry but I almost put in a snark saying "this must be my code or something." :)
Anyway, I do not mind anyone calling my code stupid, especially if it actually is. It may or may not have been stupid in the first place, or the context might have changed, but it doesn't really matter — replacing stupid code is how we improve Linux :)
-hpa (proud author of plenty of stupid code)
Borislav Petkov
maintainerQuoted 2 lines · H. Peter Anvin
Seriously, though. As Linus likes to point out, there is a huge difference between "this is stupid" and "you are stupid."
Yap.
Quoted 2 lines
I have said "this is stupid" about my own code more times than I can count. For good reason.
Same here.
Quoted 5 lines
To be honest, I didn't even need to see the comment to know that that was probably my code in the first place. I recognized my own style at a glance; in particular the kind of code I tend to write specifically for the purpose of small as opposed to fast code. I remember writing this code while waiting for a table in a restaurant :) but I don't remember the context.
You sent it and I productized it into a patch by saying:
Change num_digits() to hpa's division-avoiding, cell-phone-typed version which he went at great lengths and pains to submit on a Saturday evening.
Quoted 4 lines
Anyway, I do not mind anyone calling my code stupid, especially if it actually is. It may or may not have been stupid in the first place, or the context might have changed, but it doesn't really matter — replacing stupid code is how we improve Linux :)
Absolutely. We do that all the time and it used to work back then. Hell, it works fine for CPU numbers but if someone wants to give it INT_MIN... we didn't care about that. We needed it for this gunk:
a17bce4d1dce ("x86/boot: Further compress CPUs bootup message")
Which reminds me:
@David, are you staring at the code or are you using this function somewhere and hitting this corner case?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteH. Peter Anvin
reviewerQuoted 7 lines · Borislav Petkov
Absolutely. We do that all the time and it used to work back then. Hell, it works fine for CPU numbers but if someone wants to give it
INT_MIN... we didn't care about that. We needed it for this gunk:
a17bce4d1dce("x86/boot: Further compress CPUs bootup message")
For negative numbers? Dave Hansen pointed out that we currently don't ever pass negative numbers, in which case it would make more sense to have the function simply take an unsigned int.
-hpa
Borislav Petkov
maintainerQuoted 3 lines · H. Peter Anvin
For negative numbers? Dave Hansen pointed out that we currently don't ever pass negative numbers, in which case it would make more sense to have the function simply take an unsigned int.
Yes, that's why I'm asking what we're even fixing here...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 6 lines · On Tue, 20 Jan 2026 21:13:46 +0100 Borislav Petkov <[email protected]> wrote:
Quoted 3 lines · H. Peter Anvin
For negative numbers? Dave Hansen pointed out that we currently don't ever pass negative numbers, in which case it would make more sense to have the function simply take an unsigned int.
Yes, that's why I'm asking what we're even fixing here...
You can certainly just document it doesn't work for INT_MIN.
David
A message that is not in the archive (sent off-list or not yet received): [email protected]
H. Peter Anvin
reviewerQuoted 3 lines · David Desobry
I haven't hit this corner case in practice; I was just auditing the code and noticed the corner case overflow leading to wrong logic.
Then in a v2 version I brought some design improvement (switch case).
The switch is a good improvement. I would say, though, that if we don't need signed input then we should just take out the minus handling code and have the function take an unsigned int as input.
-hpa
Borislav Petkov
maintainerQuoted 3 lines · H. Peter Anvin
The switch is a good improvement. I would say, though, that if we don't need signed input then we should just take out the minus handling code and have the function take an unsigned int as input.
Yah.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerQuoted 2 lines · David Desobry
I haven't hit this corner case in practice; I was just auditing the code and noticed the corner case overflow leading to wrong logic.
Ok, good.
Quoted 1 line
Then in a v2 version I brought some design improvement (switch case).
I saw it, thanks.
Btw, please do not send HTML mail and do not top-post on a public mailing list.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 33 lines · On Tue, 20 Jan 2026 08:23:16 -0800 "H. Peter Anvin" <[email protected]> wrote:
Quoted 32 lines · David Desobry
In
num_digits(), the negation of the input value "val = -val" causes undefined behavior when val isINT_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 <[email protected]>---
arch/x86/lib/misc.c| 7 ++++---1 file changed, 4 insertions(+), 3 deletions(-)arch/x86/lib/misc.c +3
-2@@ -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; }
Quoted 7 lines
arch/x86/lib/misc.c line 23
Quoted 5 lines
arch/x86/lib/misc.c +1
-1-while (val >= m) { +while (v >= m) { m *=10 ; d++; }That has got to be the dumbest possible implementation of that task, bug or no bug.
And you really don't want to be doing 64bit maths on a 32bit system.
Quoted 1 line
A switch statement would be simpler and faster.
I think you mean a chain of if statement - you'd need a lot of them. But you could have:
if (val < 0) {
if (val < -999999999)
return 11;
val = -val;
d++;
} else {
if (val > 999999999)
return 10;
}then use whatever scheme looks best.
David
Fair point! I've sent a v2 that replaces the loop with a switch statement (using GCC ranges). It's faster, handles INT_MIN properly via an unsigned cast, and I've cleaned up that mobile-submission comment while I was at it.
Le 20/01/2026 à 17:23, H. Peter Anvin a écrit :
Quoted 41 lines · David Desobry
Quoted 27 lines
In
num_digits(), the negation of the input value "val = -val" causes undefined behavior when val isINT_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 <[email protected]>---
arch/x86/lib/misc.c| 7 ++++---1 file changed, 4 insertions(+), 3 deletions(-)arch/x86/lib/misc.c +2
-1@@ -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 ) {arch/x86/lib/misc.c line 23
Quoted 10 lines
arch/x86/lib/misc.c +2
-2d++; - val = -val; + v = -v; } -while (val >= m) { +while (v >= m) { m *=10 ; d++; }That has got to be the dumbest possible implementation of that task, bug or no bug.
A switch statement would be simpler and faster.
Quoted 2 lines · On Tue, 20 Jan 2026 18:48:54 +0100 David Desobry <[email protected]>…
Fair point! I've sent a v2 that replaces the loop with a switch statement (using GCC ranges). It's faster,
Look at the 'crap' that clang generates.
And remember that mispredicted branches are expensive and the default is either 'random', 'not taken' or 'backwards branches taken'. It is likely better to have a lot of not-taken branches than a binary tree.
David