Boost C++ Libraries: Ticket #11726: boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error: left shift of negative value -1 https://svn.boost.org/trac10/ticket/11726 <p> Boost Spirit is triggered undefined behaviour in C++. </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;boost/spirit/home/support/detail/endian.hpp&gt; 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&lt;long long, 8ul&gt;(raw_bytes); std::cout &lt;&lt; result &lt;&lt; std::endl; return 0; } </pre><p> or </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;memory&gt; #include &lt;tuple&gt; #include &lt;boost/spirit/home/support/detail/endian.hpp&gt; int main(void) { long long* p; std::size_t sz; std::tie(p, sz) = std::get_temporary_buffer&lt;long long&gt;(1); assert(sz != 0); boost::spirit::detail::store_little_endian&lt;long long, 8ul&gt;(p, -1); long long result2 = boost::spirit::detail::load_little_endian&lt;long long, 8ul&gt;(p); std::cout &lt;&lt; result2 &lt;&lt; std::endl; return 0; } </pre><p> Compile: </p> <pre class="wiki">g++ -fsanitize=undefined -fno-omit-frame-pointer -g -std=c++14 -O2 test.cpp </pre><p> Used GCC 4.9.3 in my case. </p> <p> Result: </p> <pre class="wiki">/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 </pre><p> Tested with Boost 1.57, but code has not been changed in master. </p> <p> I guess, this will trigger undefined behaviour every time a singed type is used. Reference: C++ standard 5.8 "Shift operators" section. </p> <p> From Boost: </p> <pre class="wiki"> 75 static T load_little(const unsigned char* bytes) 76 { return *bytes | (next::load_little(bytes + 1) &lt;&lt; 8); } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11726 Trac 1.4.3 davidlt Mon, 19 Oct 2015 08:04:04 GMT <link>https://svn.boost.org/trac10/ticket/11726#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11726#comment:1</guid> <description> <p> Patch proposed: <a class="ext-link" href="https://github.com/boostorg/spirit/pull/161"><span class="icon">​</span>https://github.com/boostorg/spirit/pull/161</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>davidlt</dc:creator> <pubDate>Mon, 19 Oct 2015 08:05:26 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/11726 https://svn.boost.org/trac10/ticket/11726 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">0001-Fix-undefined-behavior-in-.-support-detail-endian-en.patch</span> </li> </ul> <p> proposed patch (same as on <a class="missing wiki">GitHub</a>) </p> Ticket Joel de Guzman Sun, 14 Mar 2021 19:52:32 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/11726#comment:2 https://svn.boost.org/trac10/ticket/11726#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Fixed in <a class="ext-link" href="https://github.com/boostorg/spirit/pull/161"><span class="icon">​</span>https://github.com/boostorg/spirit/pull/161</a> </p> Ticket