Boost C++ Libraries: Ticket #13223: Undefined behavior inside assign_bits while importing data to cpp_int https://svn.boost.org/trac10/ticket/13223 <p> Trying to import bits from unsigned char array results in undefined behavior as reported by Clang. </p> <p> Minimal example: </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;cstdint&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/multiprecision/cpp_int.hpp&gt;</span><span class="cp"></span> <span class="k">using</span> <span class="n">mp_int_t</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">multiprecision</span><span class="o">::</span><span class="n">int128_t</span><span class="p">;</span> <span class="k">using</span> <span class="n">source_t</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="kt">uint8_t</span><span class="o">&gt;</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">source_t</span> <span class="n">src</span><span class="p">(</span><span class="mi">16</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> <span class="n">mp_int_t</span> <span class="n">result</span><span class="p">;</span> <span class="n">import_bits</span><span class="p">(</span><span class="n">result</span><span class="p">,</span> <span class="n">src</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">src</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span> <span class="mi">0</span><span class="p">,</span> <span class="nb">false</span><span class="p">);</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><p> Compile command: </p> <div class="wiki-code"><div class="code"><pre>clang++ main.cpp <span class="se">\</span> -g -O1 -fsanitize<span class="o">=</span>undefined -fno-omit-frame-pointer <span class="se">\</span> -fno-optimize-sibling-calls -std<span class="o">=</span>c++11 -o main </pre></div></div><p> Runtime output: </p> <pre class="wiki">boost/multiprecision/cpp_int/import_export.hpp:64:21: runtime error: shift exponent 128 is too large for 8-bit type 'unsigned char' </pre><p> Environment information: </p> <ul><li>Ubuntu 16.04 LTS 64bit </li><li>Tested with Clang 3.8.0 and Clang 5.0.0 </li></ul> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13223 Trac 1.4.3 John Maddock Mon, 01 Jan 2018 17:17:37 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/13223#comment:1 https://svn.boost.org/trac10/ticket/13223#comment:1 <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/multiprecision/commit/416ce0a712566b9adca17399c9e6a371e78ddc59"><span class="icon">​</span>https://github.com/boostorg/multiprecision/commit/416ce0a712566b9adca17399c9e6a371e78ddc59</a> </p> Ticket