This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: -fstrict-aliasing, -Wstrict-aliasing=2, and indirect conversions
Harald van DÄk <truedfx@gentoo.org> writes:
> Thank you for your quick reply. Your patch has been very helpful. It
> doesn't seem to catch these similar cases, though:
>
> int main() {
> int a = 1;
>
> void *p1 = (void *) &a;
> * (short *) p1 = 2;
>
> char *p2 = (char *) &a;
> * (short *) p2 = 3;
>
> return a;
> }
>
> This still returns 1 without a warning. (I am now using
> 4.1.0-20050723, so that I could try your patch.)
That is true. I'm frankly not too sure what should be done. If we
enable the warnings for these cases, the effect will be to warn about
almost every type cast which is not to or from void* or char*. That
doesn't seem very useful. On the other hand strict aliasing is a
serious problem.
Perhaps the problem is that the warning currently triggers on the
cast, but should perhaps somehow trigger on the dereference. I think
the bug occurs if you have these statements:
P2 = (TYPE *) P1;
*P1;
*P2;
in some order in the same function. However, I don't know whether the
compiler has the data structures we would need to efficiently
determine whether this happens.
Could you please open a bugzilla PR for this at
http://gcc.gnu.org/bugzilla/ ? That way at least the issue will get
recorded. Thanks.
Ian