Skip to content

Commit a2813b6

Browse files
committed
Limit exponents
1 parent 6cd5cd7 commit a2813b6

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

include/rapidjson/internal/strtod.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "diyfp.h"
2121
#include "pow10.h"
2222
#include <climits>
23+
#include <limits>
2324

2425
RAPIDJSON_NAMESPACE_BEGIN
2526
namespace internal {
@@ -260,16 +261,22 @@ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t
260261
}
261262

262263
// Trim right-most digits
263-
const int kMaxDecimalDigit = 780;
264+
const int kMaxDecimalDigit = 767 + 1;
264265
if (dLen > kMaxDecimalDigit) {
265266
dExp += dLen - kMaxDecimalDigit;
266267
dLen = kMaxDecimalDigit;
267268
}
268269

269-
// If too small, underflow to zero
270-
if (dLen + dExp < -324)
270+
// If too small, underflow to zero.
271+
// Any x <= 10^-324 is interpreted as zero.
272+
if (dLen + dExp <= -324)
271273
return 0.0;
272274

275+
// If too large, overflow to infinity.
276+
// Any x >= 10^309 is interpreted as +infinity.
277+
if (dLen + dExp > 309)
278+
return std::numeric_limits<double>::infinity();
279+
273280
if (StrtodDiyFp(decimals, dLen, dExp, &result))
274281
return result;
275282

0 commit comments

Comments
 (0)