Opened 4 years ago

Last modified 4 years ago

#13619 new Bugs

boost.crc fails to compile with gcc due to undefined behaviour

Reported by: Pavel Fevralev <dllmain@…> Owned by: Daryle Walker
Milestone: To Be Determined Component: crc
Version: Boost 1.67.0 Severity: Problem
Keywords: Cc:

Description

Boost revision: 62faaa4584e16cd60cd62d5cbb0833954686e89c
Following code is minimal example to reproduce error:

 #include <boost/crc.hpp>
 template class boost::crc_optimal<6,6,6,6,0,0>;
 int main() {}
 $ g++ -std=c++14 -pedantic-errors -I../boost/install-root/include/ -o/dev/null crc-test.cxx
 In file included from ../boost/install-root/include/boost/config.hpp:61:0,
                  from ../boost/install-root/include/boost/crc.hpp:12,
                  from crc-test.cxx:1:
 ../boost/install-root/include/boost/crc.hpp: In instantiation of 'const least boost::detail::mask_uint_t<6ul>::sig_bits':
 ../boost/install-root/include/boost/crc.hpp:356:9:   required from 'const fast boost::detail::mask_uint_t<6ul>::sig_bits_fast'
 ../boost/install-root/include/boost/crc.hpp:915:41:   required from 'boost::crc_optimal<Bits, TruncPoly, InitRem, FinalXor, ReflectIn, ReflectRem>::value_type boost::crc_optimal<Bits, TruncPoly, InitRem, FinalXor, ReflectIn, ReflectRem>::get_interim_remainder() const [with long unsigned int Bits = 6ul; typename boost::uint_t<Bits>::fast TruncPoly = 6u; typename boost::uint_t<Bits>::fast InitRem = 6u; typename boost::uint_t<Bits>::fast FinalXor = 6u; bool ReflectIn = false; bool ReflectRem = false; boost::crc_optimal<Bits, TruncPoly, InitRem, FinalXor, ReflectIn, ReflectRem>::value_type = unsigned char]'
 crc-test.cxx:3:23:   required from here
 ../boost/install-root/include/boost/crc.hpp:350:69: error: left operand of shift expression '(-1 << 6ul)' is negative [-fpermissive]
          BOOST_STATIC_CONSTANT( least, sig_bits = (~( ~(least( 0u )) << Bits )) );
                                                     ~~~~~~~~~~~~~~~~~^~~~~~~~~

GCC produce incorrect message: there is no -Werror options or similar, as you see. Reason is initialization of static constant in declaration in class with expression that is _not_ constant because consists UB(left shift of negative value). '0u' literal is unsigned, but promouted to 'int' when 'least' is 'unsigned char'. Error is reproductable on release 1.67.0. Solution, for example, is make explicit cast of left operand to unsigned integral type. GCC information:

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 6.3.0-18+deb9u1' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)

Change History (1)

comment:1 by Pavel Fevralev <dllmain@…>, 4 years ago

Component: Nonecrc
Owner: set to Daryle Walker
Note: See TracTickets for help on using tickets.