Avoid undefined behavior in enum deserialization
atoi() has undefined behavior when given a string that can't be parsed as an integer. This patch replaces atoi() with boost_lexical_cast() and throws an exception when we get something that's not a number.
This commit is contained in:
parent
a7376ceba2
commit
8d99ea94e6
1 changed files with 11 additions and 1 deletions
|
|
@ -7,6 +7,7 @@
|
|||
*/
|
||||
|
||||
#include <fc/utility.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/preprocessor/seq/for_each.hpp>
|
||||
#include <boost/preprocessor/seq/enum.hpp>
|
||||
#include <boost/preprocessor/seq/size.hpp>
|
||||
|
|
@ -148,7 +149,16 @@ template<> struct reflector<ENUM> { \
|
|||
} \
|
||||
static ENUM from_string( const char* s ) { \
|
||||
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_FROM_STRING, ENUM, FIELDS ) \
|
||||
return ENUM(atoi(s));\
|
||||
int64_t i; \
|
||||
try \
|
||||
{ \
|
||||
i = boost::lexical_cast<int64_t>(s); \
|
||||
} \
|
||||
catch( const boost::bad_lexical_cast& e ) \
|
||||
{ \
|
||||
fc::throw_bad_enum_cast( s, BOOST_PP_STRINGIZE(ENUM) ); \
|
||||
} \
|
||||
return ENUM(i);\
|
||||
} \
|
||||
}; \
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue