diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 80ba5ffb9..46f062286 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -24,6 +24,7 @@ #include #include #include +#include MathLib::biguint MathLib::toULongNumber(const std::string & str) { @@ -64,7 +65,13 @@ MathLib::biguint MathLib::toULongNumber(const std::string & str) } if (isFloat(str)) { - return static_cast(std::atof(str.c_str())); + // Things are going to be less precise now: the value can't b represented in the biguint type. + // Use min/max values as an approximation. See #5843 + const double doubleval = std::atof(str.c_str()); + if (doubleval > (double)std::numeric_limits::max()) + return std::numeric_limits::max(); + else + return static_cast(doubleval); } biguint ret = 0; @@ -112,7 +119,15 @@ MathLib::bigint MathLib::toLongNumber(const std::string & str) } if (isFloat(str)) { - return static_cast(std::atof(str.c_str())); + // Things are going to be less precise now: the value can't be represented in the bigint type. + // Use min/max values as an approximation. See #5843 + const double doubleval = std::atof(str.c_str()); + if (doubleval > (double)std::numeric_limits::max()) + return std::numeric_limits::max(); + else if (doubleval < (double)std::numeric_limits::min()) + return std::numeric_limits::min(); + else + return static_cast(doubleval); } bigint ret = 0;