Boost C++ Libraries: Ticket #11669: lexical_cast crashes on certain conditions for floating point types https://svn.boost.org/trac10/ticket/11669 <p> In the presence of BOOST_LCAST_NO_COMPILE_TIME_PRECISION, there is no specialization of lcast_src_length&lt;Source&gt; for floating types, so lexical_converter_impl will allocate only 2 bytes and then try to do a sprintf, making the program crash. </p> <p> While the specialization for integral types checks for the presence of BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS, and ifdefs an arbitrary value or one calculated via std::numeric_limits, the specialization for floats gets wiped out completely in the presence of BOOST_LCAST_NO_COMPILE_TIME_PRECISION. </p> <p> Could you please do for floats what you do for integrals? I attach a patch that assigns an (arbitrary) max length of 156 to the representation of floats. </p> <p> Basically, instead of </p> <pre class="wiki">#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION template&lt;class Source&gt; struct lcast_src_length&lt;Source, BOOST_DEDUCED_TYPENAME boost::enable_if&lt;boost::is_float&lt;Source&gt; &gt;::type&gt; { ... BOOST_STATIC_CONSTANT(std::size_t, value =5 + lcast_precision&lt;Source&gt;::value + 6); }; #endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION </pre><p> I'd like to move the ifndef inside, as in </p> <pre class="wiki"> template&lt;class Source&gt; struct lcast_src_length&lt;Source, BOOST_DEDUCED_TYPENAME boost::enable_if&lt;boost::is_float&lt;Source&gt; &gt;::type&gt; { #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION ... BOOST_STATIC_CONSTANT(std::size_t, value =5 + lcast_precision&lt;Source&gt;::value + 6); #else BOOST_STATIC_CONSTANT(std::size_t, value = 156); #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION </pre><p> The easiest way to reproduce it is: </p> <pre class="wiki">#include &lt;string&gt; #define BOOST_LCAST_NO_COMPILE_TIME_PRECISION #include &lt;boost/lexical_cast.hpp&gt; int main() { boost::lexical_cast&lt;std::string&gt;(2.12345); } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11669 Trac 1.4.3 Juan Alday <alday.boost.trac@…> Fri, 18 Sep 2015 18:46:18 GMT attachment set https://svn.boost.org/trac10/ticket/11669 https://svn.boost.org/trac10/ticket/11669 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">converter_lexical.hpp.patch</span> </li> </ul> Ticket rmgiroux@… Fri, 18 Sep 2015 18:54:50 GMT cc set https://svn.boost.org/trac10/ticket/11669#comment:1 https://svn.boost.org/trac10/ticket/11669#comment:1 <ul> <li><strong>cc</strong> <span class="trac-author">rmgiroux@…</span> added </li> </ul> Ticket Antony Polukhin Fri, 25 Sep 2015 17:34:40 GMT status, milestone changed https://svn.boost.org/trac10/ticket/11669#comment:2 https://svn.boost.org/trac10/ticket/11669#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.60.0</span> </li> </ul> <p> Fixed in <a class="ext-link" href="https://github.com/boostorg/lexical_cast/commit/0f2ce4ba7e2652cbf97fa9713fc6674d5323c9c3"><span class="icon">​</span>develop branch</a>. </p> <p> Will merge to the master branch as soon as the tests will cycle through. </p> <p> Great thanks for providing patch! </p> Ticket Antony Polukhin Tue, 17 Nov 2015 19:49:56 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/11669#comment:3 https://svn.boost.org/trac10/ticket/11669#comment:3 <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