public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/55108] New: bad compile-time evaluation of members of initialized union
@ 2012-10-28 16:04 acn1 at cam dot ac.uk
  2012-10-29 14:44 ` [Bug target/55108] " rguenth at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: acn1 at cam dot ac.uk @ 2012-10-28 16:04 UTC (permalink / raw)
  To: gcc-bugs


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: unassigned@gcc.gnu.org
        ReportedBy: acn1@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".


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-02-04  0:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-28 16:04 [Bug c/55108] New: bad compile-time evaluation of members of initialized union acn1 at cam dot ac.uk
2012-10-29 14:44 ` [Bug target/55108] " rguenth at gcc dot gnu.org
2012-10-30 14:13 ` rearnsha at gcc dot gnu.org
2012-10-30 14:16 ` rearnsha at gcc dot gnu.org
2013-02-03 23:25 ` mikpe at it dot uu.se
2013-02-04  0:06 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).