This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/14750] New: type-punned pointer causes bad code


A program which causes the warning:

 warning: dereferencing type-punned pointer will break strict-aliasing rules

leads to incorrect code. Put this code in typepun.c:
---------------------------------------
typedef unsigned short ushort;
typedef unsigned long ulong;
 
typedef struct {
    ushort field1;
    ushort field2;
} twoshorts;
 
void bug (ulong *p, int pcount)
{
    twoshorts two;
    if (pcount == 1) {
         two.field2 = 0x400;
         two.field1 = 0x8300;
         *p = *(ulong *)&two;
    }
}
------------------<end of typepun.c>---------------

and compile it with this script:

mips64-elf-gcc -S -nostdinc -mips3 -mabi=32 -O2 -Wall typepun.c

Examining the code, we see that two ushorts are written to a local struct, and
then the whole struct is copied elsewhere as a ulong. (Yeah, it's gross, but in
its defence, the original program that I cut down contains the keyword
"volatile".) Examining the resulting .s file, we see (as 4 consecutive lines):

   sh      $2,2($sp)
   lw      $2,0($sp)
   li      $3,-32000
   sh      $3,0($sp)

-- 
           Summary: type-punned pointer causes bad code
           Product: gcc
           Version: 3.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: lindsayd at cisco dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: mips64-unknown-elf


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14750


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]