diff --git a/include/rapidjson/internal/strtod.h b/include/rapidjson/internal/strtod.h index a4dfed3..dfca22b 100644 --- a/include/rapidjson/internal/strtod.h +++ b/include/rapidjson/internal/strtod.h @@ -20,6 +20,7 @@ #include "diyfp.h" #include "pow10.h" #include +#include RAPIDJSON_NAMESPACE_BEGIN namespace internal { @@ -260,16 +261,22 @@ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t } // Trim right-most digits - const int kMaxDecimalDigit = 780; + const int kMaxDecimalDigit = 767 + 1; if (dLen > kMaxDecimalDigit) { dExp += dLen - kMaxDecimalDigit; dLen = kMaxDecimalDigit; } - // If too small, underflow to zero - if (dLen + dExp < -324) + // If too small, underflow to zero. + // Any x <= 10^-324 is interpreted as zero. + if (dLen + dExp <= -324) return 0.0; + // If too large, overflow to infinity. + // Any x >= 10^309 is interpreted as +infinity. + if (dLen + dExp > 309) + return std::numeric_limits::infinity(); + if (StrtodDiyFp(decimals, dLen, dExp, &result)) return result;