Re: [PATCH v2] x86/lib: Optimize num_digits() and fix INT_MIN overflow

From: David Laight

Date: Wed Jan 21 2026 - 05:05:57 EST


On Wed, 21 Jan 2026 01:04:39 +0100
David Desobry <david.desobry@xxxxxxxxxxxxx> wrote:

> On 1/21/26 00:49, H. Peter Anvin wrote:
..
> Actually, the V3 change:
> if (val < 0) {
> - d++;
> - val = -val;
> + d = 1;
> + v = -val;
> + } else {
> + d = 0;
> + v = val;
> }
> reintroduced the undefined behavior for val == INT_MIN.
> So this V3 version is incorrect.

Change to:
v = -(val + 1); v++;
The compiler notices the two '+ 1' cancel each other out.

David

> I'm not familiar enough with the rest of the codebase to know if
> changing the function signature to unsigned int is correct here.

In theory you'd need to change the name and all the callers.

David