From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15448 invoked by alias); 28 Oct 2012 16:04:03 -0000 Received: (qmail 15352 invoked by uid 48); 28 Oct 2012 16:03:48 -0000 From: "acn1 at cam dot ac.uk" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/55108] New: bad compile-time evaluation of members of initialized union Date: Sun, 28 Oct 2012 16:04:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: acn1 at cam dot ac.uk X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-10/txt/msg02640.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D55108 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=3D28548 internal file "bad.i" obtained using -save-temp, as requested. gcc -v Using built-in specs. COLLECT_GCC=3Dgcc COLLECT_LTO_WRAPPER=3D/usr/lib/gcc/arm-linux-gnueabihf/4.6/lto-wrapper Target: arm-linux-gnueabihf Configured with: ../src/configure -v --with-pkgversion=3D'Debian 4.6.3-8+rp= i1' --with-bugurl=3Dfile:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=3Dc,c++,fortran,objc,obj-c++ --prefix=3D/usr --program-suffix=3D-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=3D/usr/lib --without-included-gettext --enable-threads=3Dposix --with-gxx-include-dir=3D/usr/include/c++/4.6 --libdir=3D/usr/lib --enable-nls --with-sysroot=3D/ --enable-clocale=3Dgnu --enable-libstdcxx-debug --enable-libstdcxx-time=3Dyes --enable-gnu-unique-= object --enable-plugin --enable-objc-gc --disable-sjlj-exceptions --with-arch=3Dar= mv6 --with-fpu=3Dvfp --with-float=3Dhard --enable-checking=3Drelease --build=3Darm-linux-gnueabihf --host=3Darm-linux-gnueabihf --target=3Darm-linux-gnueabihf Thread model: posix gcc version 4.6.3 (Debian 4.6.3-8+rpi1) File bad.c contains #include int main(int argc, char *argv[]) { union { double d; unsigned char c[8]; } u =3D {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 c= hars 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 =E2=80=98main=E2=80=99: bad.c:3:14: warning: unused parameter =E2=80=98argc=E2=80=99 [-Wunused-para= meter] bad.c:3:26: warning: unused parameter =E2=80=98argv=E2=80=99 [-Wunused-para= meter] 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".