Possible gcc bug in strict type aliasing

Vincent Lefevre vincent+gcc@vinc17.org
Tue Sep 27 12:49:00 GMT 2016

On 2016-09-26 10:32:14 +0100, Andrew Haley wrote:
> On 25/09/16 22:46, David Brown wrote:
> I think the bug is here:
> >        temp = *t2p;      // Read as T2
> >        t1p2 = (T1*)t2p;  // Visible T2 to T1 pointer conversion
> >        *t1p2 = temp;     // Write as T1
> Pointers
> 7 A pointer to an object type may be converted to a pointer to a
>   different object type. If the resulting pointer is not correctly
>   aligned for the referenced type, the behavior is undefined.
>   Otherwise, when converted back again, the result shall compare equal
>   to the original pointer.
> Note that you have permission only to convert the pointer back to the
> original type and compare it.  You don't have permission to
> dereference it as a different type.  IMO your program is undefined.

I disagree. The above paragraph says nothing about dereferencing the
pointer; in particular, it does not disallow one to dereference it
as a different type. The restrictions about dereferencing are given
by 6.5#7 (itself based on 6.5#6, which defines the "effective type").
Moreover 6.5#7 restrictions are about write-read combinations, not
about read-write like in the above 3 lines of code.

Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)

More information about the Gcc-help mailing list