Boost C++ Libraries: Ticket #5983: Stack overflow with Karma floating point generator https://svn.boost.org/trac10/ticket/5983 <p> This spirit code causes a stack overflow on MSVC 9 and seg fault on GCC 4.2.1 with boost 1.43 and trunk. </p> <pre class="wiki">#include &lt;boost/spirit/include/karma.hpp&gt; #include &lt;string&gt; using namespace std; template &lt;typename T&gt; struct double12_policy : boost::spirit::karma::real_policies&lt;T&gt; { // we want to generate up to 12 fractional digits static unsigned int precision(T) { return 12; } }; string to_string(double value) { using namespace boost::spirit::karma; typedef real_generator&lt;double, double12_policy&lt;double&gt; &gt; double12_type; static const double12_type double12 = double12_type(); char buffer[256]; char* p = buffer; generate(p, double12, value); return string(&amp;buffer[0], p); } int main() { cout &lt;&lt; to_string(7.0714098800910119e-313); return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5983 Trac 1.4.3 Hartmut Kaiser Wed, 05 Oct 2011 01:03:35 GMT status changed https://svn.boost.org/trac10/ticket/5983#comment:1 https://svn.boost.org/trac10/ticket/5983#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> <code>7.0714098800910119e-313</code> is a denormalized floating point number and Karma currently does not support printing those. Nevertheless, the fact that you're seeing a stack overflow is definitely a bug which I will try to fix asap. </p> <p> Independently, because of it being denormalized (the smallest normalized value is <code>2.2250738585072014e-308</code>) it does not make any sense to request 12 digits precision anyways, as only 15-5 == 10 digits precision are left for this number. </p> Ticket Daniel Krügler <daniel.kruegler@…> Sun, 04 May 2014 13:11:58 GMT attachment set https://svn.boost.org/trac10/ticket/5983 https://svn.boost.org/trac10/ticket/5983 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">real_utils.diff</span> </li> </ul> <p> SVN patch file </p> Ticket Daniel Krügler <daniel.kruegler@…> Sun, 04 May 2014 13:29:31 GMT <link>https://svn.boost.org/trac10/ticket/5983#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5983#comment:2</guid> <description> <p> I stumbled across the same issue yesterday using boost 1.55, that is the missing support for denormalized numbers by karma::generate. This is a very nasty bug for us and it basically forbids is to use karma::generate for floating point numbers in our source code, which is really unfortunate due to the otherwise excellent properties of this function and the library. </p> <p> Since I'm seeing no progress on this bug since three years I would like to help solving it. </p> <p> First, here is a more specific test-case: </p> <pre class="wiki">#include &lt;limits&gt; #include &lt;stdexcept&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include "boost/spirit/include/karma.hpp" template &lt;class T&gt; std::string to_string(const T&amp; value) { std::string res; if (!boost::spirit::karma::generate(std::back_inserter(res), value)) throw std::invalid_argument("Could not convert argument to string"); return res; } int main() { try { std::string s = to_string(std::numeric_limits&lt;double&gt;::denorm_min()); std::cout &lt;&lt; s &lt;&lt; std::endl; } catch (...) { std::cout &lt;&lt; "Error!" &lt;&lt; std::endl; } } </pre><p> which crashes due to an assertion failure in "boost/spirit/home/support/detail/pow10.hpp", line 87. The error is understandable, because boost::spirit::traits::pow10 can only handle positive dimensions and it is impossible to map the (negative) exponent of a denormalized number to an exponential value in the domain of the corresponding type by a single multiplication. </p> <p> Second, I have created an patch file against the recent boost SVN head. Please consider to apply the attached file real_utils.diff. </p> <p> If you have any further questions or requirements for a patch, please let me know. </p> </description> <category>Ticket</category> </item> <item> <author>xreborner@…</author> <pubDate>Sat, 09 Jan 2016 16:16:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5983#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5983#comment:3</guid> <description> <p> I also encountered this problem. Four year passed, still not solved? </p> </description> <category>Ticket</category> </item> <item> <author>mike.gresens@…</author> <pubDate>Mon, 18 Dec 2017 11:08:40 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5983#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5983#comment:4</guid> <description> <p> Should be fixed in <a class="ext-link" href="https://github.com/boostorg/spirit/pull/328"><span class="icon">​</span>https://github.com/boostorg/spirit/pull/328</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Joel de Guzman</dc:creator> <pubDate>Mon, 18 Dec 2017 23:26:16 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5983#comment:5 https://svn.boost.org/trac10/ticket/5983#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket