[Boost-bugs] [Boost C++ Libraries] #11726: boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error: left shift of negative value -1

Subject: [Boost-bugs] [Boost C++ Libraries] #11726: boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error: left shift of negative value -1
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2015-10-14 22:06:26


#11726: boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error:
left shift of negative value -1
------------------------------+---------------------
 Reporter: davidlt | Owner: djowel
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: spirit
  Version: Boost 1.57.0 | Severity: Problem
 Keywords: spirit |
------------------------------+---------------------
 Boost Spirit is triggered undefined behaviour in C++.

 {{{
 #include <iostream>

 #include <boost/spirit/home/support/detail/endian.hpp>

 int main(void) {
   const unsigned char raw_bytes[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 0xff, 0xff};
   long long result = boost::spirit::detail::load_little_endian<long long,
 8ul>(raw_bytes);
   std::cout << result << std::endl;
   return 0;
 }
 }}}

 or

 {{{
 #include <iostream>
 #include <memory>
 #include <tuple>

 #include <boost/spirit/home/support/detail/endian.hpp>

 int main(void) {
   long long* p;
   std::size_t sz;
   std::tie(p, sz) = std::get_temporary_buffer<long long>(1);
   assert(sz != 0);
   boost::spirit::detail::store_little_endian<long long, 8ul>(p, -1);

   long long result2 = boost::spirit::detail::load_little_endian<long long,
 8ul>(p);
   std::cout << result2 << std::endl;
   return 0;
 }
 }}}

 Compile:
 {{{
 g++ -fsanitize=undefined -fno-omit-frame-pointer -g -std=c++14 -O2
 test.cpp
 }}}

 Used GCC 4.9.3 in my case.

 Result:
 {{{
 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57:
 runtime error: left shift of negative value -1
 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57:
 runtime error: left shift of negative value -1
 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57:
 runtime error: left shift of negative value -1
 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57:
 runtime error: left shift of negative value -1
 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57:
 runtime error: left shift of negative value -1
 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57:
 runtime error: left shift of negative value -1
 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57:
 runtime error: left shift of negative value -1
 -1
 }}}

 Tested with Boost 1.57, but code has not been changed in master.

 I guess, this will trigger undefined behaviour every time a singed type is
 used. Reference: C++ standard 5.8 "Shift operators" section.

 From Boost:
 {{{
      75 static T load_little(const unsigned char* bytes)
      76 { return *bytes | (next::load_little(bytes + 1) << 8); }
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/11726>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:19 UTC