Re: UBSAN-safe way to convert from unsigned int to int

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 20 May 2025 at 10:59, Alexander Monakov via Gcc-help
<gcc-help@xxxxxxxxxxx> wrote:
>
>
> On Tue, 20 May 2025, Florian Weimer via Gcc-help wrote:
>
> > Is there a concise way to convert X to a value Y of type int, so that
> >
> >   X == (unsigned int) Y
> >
> > (assuming that X is of type unsigned int to start with)?  GCC supports
> > this as an extension, using Y = (int) X, but this extension is
> > apparently unavailable if UBSAN is active.
>
> Can you give a specific compilable example? Neither GCC nor Clang instruments
>
> int f(unsigned x)
> {
>         return x;
> }
>
> under -fsanitize=undefined.

Clang has -fsanitize=integer though:

int.cc:3:10: runtime error: implicit conversion from type 'unsigned
int' of value 2147483648 (32-bit, unsigned) to type 'int
' changed the value to -2147483648 (32-bit, signed)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior int.cc:3:10

GCC does not (and will not) implement that.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux