From a2813b67396c300cf152897179b6e24669b2ceb1 Mon Sep 17 00:00:00 2001 From: abolz Date: Fri, 15 Jun 2018 17:10:36 +0200 Subject: [PATCH] Limit exponents --- include/rapidjson/internal/strtod.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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;