From cccddc20bf2abb9aae8341fb831528f0500214b5 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Wed, 21 May 2014 21:36:17 +0200 Subject: [PATCH] #5843 MathLib::toLongNumber()/MathLib::toULongNumber() now use min/max values for bigint/biguint type in case the number to converted cannot be converted properly --- lib/mathlib.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) 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;