Re: [PATCH 2/4] tools/nolibc: stdlib: avoid signed overflow in abs() and friends
From: Thomas Weißschuh
Date: Tue Jul 28 2026 - 13:52:40 EST
On 2026-07-28 09:46:25+0100, David Laight wrote:
> On Sun, 26 Jul 2026 17:13:03 +0700
> Ammar Faizi <ammarfaizi2@xxxxxxxxxxxxx> wrote:
>
> > Negating the smallest negative value of a signed type overflows, which
> > is undefined behavior. The selftests are built with:
> >
> > -fsanitize=undefined -fsanitize-trap=all
> >
> > so a caller passing INT_MIN does not merely get an unspecified answer,
> > it dies (on both x86-64 and i386):
>
> Thinking more, that is probably the correct thing to do.
> 'Undefined behaviour' means exactly that.
> It tends to be used for things that might cause traps on some architectures.
> But I believe 'undefined behaviour' is exactly that, erasing your hard disk
> (or starting a nuclear war) are both valid.
That's a good point. Calling these functions with those inputs is UB as
per the standard(s). By trapping it under -fsanitize=undefined the users
get what they ask for.
So I think we should keep the functions as they are.
If any users require different semantics, they can use a local function
easily.
Thomas