diff --git a/src/ripple/protocol/impl/STParsedJSON.cpp b/src/ripple/protocol/impl/STParsedJSON.cpp index 487cb22041..5aa7374dd5 100644 --- a/src/ripple/protocol/impl/STParsedJSON.cpp +++ b/src/ripple/protocol/impl/STParsedJSON.cpp @@ -42,14 +42,24 @@ namespace ripple { namespace STParsedJSONDetail { - -template -static T range_check_cast (U value, T minimum, T maximum) +template +constexpr +std::enable_if_t::value && std::is_signed::value, U> +to_unsigned (S value) { - if ((value < minimum) || (value > maximum)) + if (value < 0 || std::numeric_limits::max () < value) Throw ("Value out of range"); + return static_cast (value); +} - return static_cast (value); +template +constexpr +std::enable_if_t::value && std::is_unsigned::value, U1> +to_unsigned (U2 value) +{ + if (std::numeric_limits::max () < value) + Throw ("Value out of range"); + return static_cast (value); } static std::string make_name (std::string const& object, @@ -192,8 +202,7 @@ static boost::optional parseLeaf ( } ret = detail::make_stvar (field, - range_check_cast ( - value.asInt (), 0, 255)); + static_cast (value.asInt ())); } else if (value.isUInt ()) { @@ -204,8 +213,7 @@ static boost::optional parseLeaf ( } ret = detail::make_stvar (field, - range_check_cast ( - value.asUInt (), 0, 255)); + static_cast (value.asUInt ())); } else { @@ -268,14 +276,12 @@ static boost::optional parseLeaf ( else if (value.isInt ()) { ret = detail::make_stvar (field, - range_check_cast ( - value.asInt (), 0, 65535)); + to_unsigned (value.asInt ())); } else if (value.isUInt ()) { ret = detail::make_stvar (field, - range_check_cast ( - value.asUInt (), 0, 65535)); + to_unsigned (value.asUInt ())); } else { @@ -303,8 +309,7 @@ static boost::optional parseLeaf ( else if (value.isInt ()) { ret = detail::make_stvar (field, - range_check_cast ( - value.asInt (), 0u, 4294967295u)); + to_unsigned (value.asInt ())); } else if (value.isUInt ()) { @@ -336,8 +341,7 @@ static boost::optional parseLeaf ( else if (value.isInt ()) { ret = detail::make_stvar (field, - range_check_cast ( - value.asInt (), 0, 18446744073709551615ull)); + to_unsigned (value.asInt ())); } else if (value.isUInt ()) {