Boost C++ Libraries: Ticket #8700: multiprecision : Trying to cast to unsigned from cpp_int > 19 bits causes crash https://svn.boost.org/trac10/ticket/8700 <p> The code segment below shows an iteration through a list of fields. Each field will be filled with a sub-set of the cpp_int number. </p> <p> If the cpp_int number is 20-bits or larger, the second line of the 'for' loop (where the cast to unsigned occurs) crashes bit-time. </p> <p> When I comment out the cast, the code works file. If I reduce the size of the cpp_int 'i_addr' to 19 bits or less, it works fine. </p> <p> 20 bits or more causes a complete crash. </p> <p> Ticket <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8423" title="#8423: Bugs: miller_rabin_test core dump (closed: worksforme)">#8423</a> comments about a similar crash and that it was fixed for the next release. I was unable to find the fix. Note that in that example, the 32-bit value crashes while the 16-bit one does not. </p> <p> Very similar to my case, so I suspect it is the same bug. </p> <p> Can someone please confirm this? </p> <p> Mingw 10.2 - GCC 4.8.1 - Boost 1.53.0 - <a class="missing wiki">FreeType</a> 2.4.12, libogg 1.3.1, PCRE 8.33, pngcrush 1.7.60. </p> <p> <em>-----------------------------------------------------------</em> </p> <p> typedef boost :: multiprecision :: cpp_int Bit_Reg ; </p> <p> Bit_Reg i_addr ; <em> This is incremented elsewhere, but used in this loop </em></p> <p> for (auto * i_field : table-&gt;i_field_list) { </p> <blockquote> <p> Bit_Reg i_value = i_addr &gt;&gt; i_field-&gt;bits.lsb ; </p> </blockquote> <blockquote> <p> i_field-&gt;value = i_value.template convert_to &lt;unsigned&gt; () ; </p> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8700 Trac 1.4.3 John Maddock Sun, 16 Jun 2013 08:09:12 GMT <link>https://svn.boost.org/trac10/ticket/8700#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8700#comment:1</guid> <description> <p> Can you provide a self-contained test case please? Note that the beta for 1.54 is out now, so it might be easiest for you to test against that? </p> </description> <category>Ticket</category> </item> <item> <author>danq@…</author> <pubDate>Sun, 16 Jun 2013 21:26:35 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8700#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8700#comment:2</guid> <description> <p> I created a stripped-down test case, but it does not fail. No matter how large a count, the conversion works. Sigh... </p> <p> I'm not sure how to go about integrating 1.54 into my toolset. I am using MinGW 10.2 distribution (a simple unzip and copy to destination folder type install). </p> <p> One thought that just occurred to me is that the run-time stack probably increases as the cpp_int size increases (confirm?). Perhaps what is happening in my application code is the stack is heavily used and overflows somehow when I increases the bit size to 20 bits. </p> <p> This might explain why the test case works. It has minimal stack requirements. </p> <p> A side note (may be related): </p> <p> This: </p> <blockquote> <p> Bit_Reg i_value = i_addr &gt;&gt; i_field-&gt;bits.lsb ; </p> </blockquote> <blockquote> <p> i_field-&gt;value = i_value.template convert_to &lt;unsigned&gt; () ; </p> </blockquote> <p> Compiles while this: </p> <blockquote> <p> i_field-&gt;value = static_cast &lt;unsigned&gt; (i_addr &gt;&gt; i_field-&gt;bits.lsb) ; </p> </blockquote> <p> does not. </p> <p> However, this: </p> <blockquote> <p> Bit_Reg i_value = i_addr &gt;&gt; i_field-&gt;bits.lsb ; </p> </blockquote> <blockquote> <p> i_field-&gt;value = static_cast &lt;unsigned&gt; (i_value) ; </p> </blockquote> <p> works. </p> <p> It seems that if I try to pass a temporary to the static_cast operator, the compiler complains. </p> <p> I noticed this while trying to create a test case. Note: either method still fails when I reach 20 bits. </p> <p> May or may not be related to the problem I am seeing. </p> <p> Suggestions on how to install 1.54 beta into my (simple) Mingw environment? </p> <p> Cheers! </p> <p> Dan </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Mon, 17 Jun 2013 08:02:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8700#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8700#comment:3</guid> <description> <blockquote class="citation"> <p> Suggestions on how to install 1.54 beta into my (simple) Mingw environment? </p> </blockquote> <p> Just unpack the zip and place it in your path - multiprecision is header only. If you need other Boost libs, the easiest way would be to temporarily rename boost/multiprecision in the 1.53 tree and then copy the version from 1.54 over to test. </p> <p> BTW bit usage shouldn't effect stack usage in cpp_int. Have you run under gdb and/or looked up the error code to see if it's a stack overflow? </p> </description> <category>Ticket</category> </item> <item> <author>danq@…</author> <pubDate>Mon, 17 Jun 2013 12:35:12 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8700#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8700#comment:4</guid> <description> <p> News: Last night, I significantly increased the size of the run-time stack and the problem went away. Since you mentioned that cpp_int does not effect stack usage, I suspect that somewhere else in my code, the stack was getting loaded and by sheer chance, wound up clobbering my cpp_int variable. </p> <p> I will play with GDB to see if I can pinpoint the cause. </p> <p> I will also take a look at 1.54 and try to reproduce the problem with that version. </p> <p> Thanks! </p> <p> Dan </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Fri, 21 Jun 2013 16:09:42 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8700#comment:5 https://svn.boost.org/trac10/ticket/8700#comment:5 <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">invalid</span> </li> </ul> <p> Closing for now, please reopen if you confirm a Boost bug. </p> Ticket