Opened 7 years ago

Closed 7 years ago

#11669 closed Bugs (fixed)

lexical_cast crashes on certain conditions for floating point types

Reported by: alday.boost.trac@… 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)

converter_lexical.hpp.patch (1.3 KB ) - added by Juan Alday <alday.boost.trac@…> 7 years ago.

Download all attachments as: .zip

Change History (4)

by Juan Alday <alday.boost.trac@…>, 7 years ago

Attachment: converter_lexical.hpp.patch added

comment:1 by rmgiroux@…, 7 years ago

Cc: rmgiroux@… added

comment:2 by Antony Polukhin, 7 years ago

Milestone: To Be DeterminedBoost 1.60.0
Status: newassigned

Fixed in develop branch.

Will merge to the master branch as soon as the tests will cycle through.

Great thanks for providing patch!

comment:3 by Antony Polukhin, 7 years ago

Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.