Problem report: Struct aliasing problem causes Thread_Ready_Chain corruption in 4.6.99.3

Till Straumann strauman at slac.stanford.edu
Fri Dec 8 17:49:53 UTC 2006


Sergei Organov wrote:
> Till Straumann
> <strauman at slac.stanford.edu> writes:
>
> [...]
>
>   
>> Plus, it seems that there is a gcc (extension?) so that
>>     
>
> I think this is pretty standard-conforming, see below.
>
>   
>> extern char *some_allocator();
>>
>> int tst()
>> {
>> char *rval = some_allocator();
>>     rval[0]      = 0;
>>     *(int*)rval = 0xffffffff;
>>     return rval[0];
>> }
>>
>> returns indeed 0xff (i.e., gcc assumes *(int*)rval may alias
>> *rval) whereas
>>
>> extern short *some_other_allocator();
>>
>> int tst()
>> {
>> short *rval = some_other_allocator();
>>     rval[0]  = 0;
>>     *(int*)rval = 0xffffffff;
>>     return rval[0];
>> }
>>
>> returns 0. So gcc seems to not only allow char to alias type anything but
>> also conversely anything to alias char. This is more relaxed than the
>> standard which says
>>     
>
> As far as I understand, "alias" is symmetric with respect to read/write.
> So, provided the standard says int* and char* may alias,
It doesn't. It says that an object may only be accessed by
an lvalue of compatible type or character type.

-> char[4] is a char array
-> *(int*)char is an int

The standard doesn't allow a char array to be accessed as an int.

OTOH

int i; is an int

*(char*)&i is an lvalue of type char which is a legal way to
access an int.

>  you are allowed
> to store to char* and then read by int*, as well as to store to int*
> and then read by char*. The latter is what you are doing in the first
> example,
not at all. The allocator returns an array of chars which are
then written-to as an 'int'. This is not allowed by the standard.

-- T.
>  so it's pretty OK from the pow of C99, I think.
>
> The second example does violate the aliasing rules indeed as int* and
> short* aren't allowed to alias.
>
> -- Sergei.
>
> _______________________________________________
> rtems-users mailing list
> rtems-users at rtems.com
> http://rtems.rtems.org/mailman/listinfo/rtems-users
>   




More information about the users mailing list