Opened 7 years ago
Closed 7 years ago
#11669 closed Bugs (fixed)
lexical_cast crashes on certain conditions for floating point types
Reported by: | Owned by: | Antony Polukhin | |
---|---|---|---|
Milestone: | Boost 1.60.0 | Component: | lexical_cast |
Version: | Boost 1.59.0 | Severity: | Problem |
Keywords: | Cc: | rmgiroux@… |
Description
In the presence of BOOST_LCAST_NO_COMPILE_TIME_PRECISION, there is no specialization of lcast_src_length<Source> for floating types, so lexical_converter_impl will allocate only 2 bytes and then try to do a sprintf, making the program crash.
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.
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.
Basically, instead of
#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION template<class Source> struct lcast_src_length<Source, BOOST_DEDUCED_TYPENAME boost::enable_if<boost::is_float<Source> >::type> { ... BOOST_STATIC_CONSTANT(std::size_t, value =5 + lcast_precision<Source>::value + 6); }; #endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
I'd like to move the ifndef inside, as in
template<class Source> struct lcast_src_length<Source, BOOST_DEDUCED_TYPENAME boost::enable_if<boost::is_float<Source> >::type> { #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION ... BOOST_STATIC_CONSTANT(std::size_t, value =5 + lcast_precision<Source>::value + 6); #else BOOST_STATIC_CONSTANT(std::size_t, value = 156); #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
The easiest way to reproduce it is:
#include <string> #define BOOST_LCAST_NO_COMPILE_TIME_PRECISION #include <boost/lexical_cast.hpp> int main() { boost::lexical_cast<std::string>(2.12345); }
Attachments (1)
Change History (4)
by , 7 years ago
Attachment: | converter_lexical.hpp.patch added |
---|
comment:1 by , 7 years ago
Cc: | added |
---|
comment:2 by , 7 years ago
Milestone: | To Be Determined → Boost 1.60.0 |
---|---|
Status: | new → assigned |
comment:3 by , 7 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Fixed in develop branch.
Will merge to the master branch as soon as the tests will cycle through.
Great thanks for providing patch!