Problem report: Struct aliasing problem causes Thread_Ready_Chain corruption in 4.6.99.3

Sergei Organov osv at javad.com
Fri Dec 8 14:36:23 UTC 2006


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




More information about the users mailing list