Bug ID: 77617
           Summary: Miscompilation n &= 0x1F ? x+n : x;
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot
          Reporter: obilaniu at yahoo dot com
  Target Milestone: ---

Created attachment 39633
Demonstration file minimal.c

GCC has been miscompiling on Linux, since at least 4.8.5, the following
function fail():

static       unsigned good(unsigned x, unsigned n){
        n &= 0x1F;
        return n ? x+n : x;
static       unsigned fail(unsigned x, unsigned n){
        return n &= 0x1F ? x+n : x;

int main(){
        return fail(0xDEADBEEFU, 16U) == 0xDEADBEFFU;

The program should return an exit status of 1. When main() calls good() it does
return an exit code of 1, but when it calls fail() it returns an exit code of

The program exhibits no UB; A sequence point occurs after the evaluation of the
condition of the ternary operator and before the evaluation of either of its

My system is OpenSuSE 42.1 Leap with Linux kernel 4.4.5 and the CPU is an Intel
Haswell Core i7. I am using the stock compilers and an SVN checkout.

The miscompilation occurs for the following GCC versions I have access to:
- GCC 4.8.5
- GCC 5.3.1
- GCC 7.0.0 20160916 (svn r240176)

They were configured as follows:

> gcc -v
Using built-in specs.
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
--enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.8
--enable-ssp --disable-libssp --disable-plugin
--with-bugurl= --with-pkgversion='SUSE Linux'
--disable-libgcj --disable-libmudflap --with-slibdir=/lib64 --with-system-zlib
--enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch
--enable-version-specific-runtime-libs --enable-linker-build-id
--enable-linux-futex --program-suffix=-4.8 --without-system-libunwind
--with-arch-32=i586 --with-tune=generic --build=x86_64-suse-linux
Thread model: posix
gcc version 4.8.5 (SUSE Linux)

> gcc-5 -v
Using built-in specs.
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
--enable-languages=c,c++,fortran,ada,go --enable-checking=release
--with-gxx-include-dir=/usr/include/c++/5 --enable-ssp --disable-libssp
--disable-libvtv --enable-libmpx --disable-plugin
--with-bugurl= --with-pkgversion='SUSE Linux'
--disable-libgcj --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit
--enable-libstdcxx-allocator=new --disable-libstdcxx-pch
--enable-version-specific-runtime-libs --enable-linker-build-id
--enable-linux-futex --program-suffix=-5 --without-system-libunwind
--enable-multilib --with-arch-32=x86-64 --with-tune=generic
--build=x86_64-suse-linux --host=x86_64-suse-linux
Thread model: posix
gcc version 5.3.1 20160301 [gcc-5-branch revision 233849] (SUSE Linux)

> ./gcc -v
Using built-in specs.
Target: x86_64-pc-linux-gnu
Configured with: ../gcc/configure --prefix=/tmp/ramdisk/build
--disable-bootstrap --disable-nls --enable-languages=c
Thread model: posix
gcc version 7.0.0 20160916 (experimental) (GCC)

The program minimal.c was compiled at both -O0 and -O3 and using both gcc and
g++ with identical effects:
  gcc       minimal.c -o minimal
  gcc   -O3 minimal.c -o minimal
  g++       minimal.c -o minimal
  g++   -O3 minimal.c -o minimal
  gcc-5     minimal.c -o minimal
  gcc-5 -O3 minimal.c -o minimal
  g++-5     minimal.c -o minimal
  g++-5 -O3 minimal.c -o minimal
  ./gcc     minimal.c -o minimal
  ./gcc -O3 minimal.c -o minimal
No compilations gave any warnings whatsoever during compilation. All
compilations above exhibit the bug when the fail() function is called and don't
when the good() function is called.

An ideone link to a related problem is here:

Reply via email to