https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77617

            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 gnu.org
          Reporter: obilaniu at yahoo dot com
  Target Milestone: ---

Created attachment 39633
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=39633&action=edit
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
0.

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
arms.


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.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/4.8/lto-wrapper
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++,objc,fortran,obj-c++,java,ada
--enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.8
--enable-ssp --disable-libssp --disable-plugin
--with-bugurl=http://bugs.opensuse.org/ --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
--host=x86_64-suse-linux
Thread model: posix
gcc version 4.8.5 (SUSE Linux)

> gcc-5 -v
Using built-in specs.
COLLECT_GCC=gcc-5
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/5/lto-wrapper
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=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux'
--disable-libgcj --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit
--enable-libstdcxx-allocator=new --disable-libstdcxx-pch
--with-default-libstdcxx-abi=gcc4-compatible
--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.
COLLECT_GCC=./gcc
COLLECT_LTO_WRAPPER=/tmp/ramdisk/build/lib/gcc/x86_64-pc-linux-gnu/7.0.0/lto-wrapper
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:
https://ideone.com/djOB6g

Reply via email to