Re: "Uninitialized array" warnings by c++ with -O2

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

 



On 07/06/2017 16:29, Jakub Jelinek wrote:
> On Wed, Jun 07, 2017 at 05:16:44PM +0300, K wrote:
>>>           ui16 *ptr = (ui16*)buf;
>>>
>>> There's no need for any of this messing about with pointer casts, as has
>>> been explained.
>>
>> Sorry, but I still can't get the idea. Cast from udp_pseudo to uint8_t
>> doesn't have an aliasing problem (std 8.8) and a cast from uint8_t to ui16
>> still doesn't have an aliasing problem (std  8.6), or may be I missed
>> something?
> 
> This is not the right list to learn C or C++, so this should be moved
> to gcc-help.  The casts themselves are not the points of UB (unless the
> pointer is misaligned for the pointee type you cast to), the problem is that
> you have an object (tmp and its fields) that doesn't have unsigned short as
> its dynamic type, nor cv-qualified version thereof, nor similar type, etc.
> (see [basic.lval]/8 for details) and you are accessing that object
> using a glvalue with unsigned short type.  And that invokes the UB:
> "If a program attempts to access the stored value of an object through a glvalue
> of other than one of the following types the behavior is undefined:"
> ... long list of what is allowed.
> 
> Accessing it e.g. using glvalue of char, unsigned char or std::byte would be
> fine, or say using ui16 __attribute__((may_alias)) type (by having
>   typedef ui16 ui16a __attribute__((may_alias));
>   ui16a *ptr = (ui16a*)buf;
>   ... *ptr ...
> or similar, or union that includes the type of tmp and ui16 as type of its
> members), etc.

Anyone messing around with pointer casts (other than pointer to
first field of struct foo cast to/from pointer to struct foo)
should be using -fno-strict-aliasing (as is done in Linux)

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fstrict-aliasing

If that makes any difference, then likely UB has been tickled.

Regards.



[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