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

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

 



* Alexander Monakov via Gcc-help:

> 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.

I interpreted Adhemerval as indicating this here:

| 		   (int) ((unsigned int) opt << GROUP_BITS) >> GROUP_BITS,

| Yeah, but -fsanitize=undefinied still triggers this as UB.  I can add an
| option to suppress this kind of shift, but this will add a bit more
| complexity on the handler handling.

<https://inbox.sourceware.org/libc-alpha/7c8ad506-bc42-469b-86a7-bccc5d013067@xxxxxxxxxx/>

I really dislike the very verbose alternative.

Thanks,
Florian




[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