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

             Bug #: 55108
           Summary: bad compile-time evaluation of members of initialized
                    union
    Classification: Unclassified
           Product: gcc
           Version: 4.6.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: a...@cam.ac.uk


Created attachment 28548
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=28548
internal file "bad.i" obtained using -save-temp, as requested.

gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.6/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.3-8+rpi1'
--with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.6 --enable-shared --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object
--enable-plugin --enable-objc-gc --disable-sjlj-exceptions --with-arch=armv6
--with-fpu=vfp --with-float=hard --enable-checking=release
--build=arm-linux-gnueabihf --host=arm-linux-gnueabihf
--target=arm-linux-gnueabihf
Thread model: posix
gcc version 4.6.3 (Debian 4.6.3-8+rpi1)

File bad.c contains

#include <stdio.h>

int main(int argc, char *argv[])
{
    union
    {   double d;
        unsigned char c[8];
    } u = {1.0/7.0};
    printf("Bytes from float are %x %x %x %x\n",
           u.c[0] & 0xff, u.c[1] & 0xff, u.c[2] & 0xff, u.c[3] & 0xff);
    return 0;
}

My hope is that the aliasing is OK both because the float is aliased with chars
and because I believed that GCC was kind about unions.

The output is

acn1@raspberrypi ~ $ gcc -O0 bad.c -o bad0
acn1@raspberrypi ~ $ gcc -O1 bad.c -o bad1 -Wall -Wextra
bad.c: In function ‘main’:
bad.c:3:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
bad.c:3:26: warning: unused parameter ‘argv’ [-Wunused-parameter]
acn1@raspberrypi ~ $ ./bad0
Bytes from float are 92 24 49 92         <<< expected output
acn1@raspberrypi ~ $ ./bad1
Bytes from float are 92 924924 9249 92   <<< output > 0xff unexpected
acn1@raspberrypi ~ $

To my mind whatever else might be going on the "& 0xff" that I have should not
let me get such large numbers displayed!

The ".i" file is attached. gcc is evaluating the components of the union at
compile time and not respecting the "unsigned char" width or the subseqent "&
0xff".

Reply via email to