@@ -573,8 +573,6 @@ Reader::decodeNumber( Token &token )
573573 Value::LargestUInt maxIntegerValue = isNegative ? Value::LargestUInt (-Value::minLargestInt)
574574 : Value::maxLargestUInt;
575575 Value::LargestUInt threshold = maxIntegerValue / 10 ;
576- Value::UInt lastDigitThreshold = Value::UInt ( maxIntegerValue % 10 );
577- assert ( lastDigitThreshold >=0 && lastDigitThreshold <= 9 );
578576 Value::LargestUInt value = 0 ;
579577 while ( current < token.end_ )
580578 {
@@ -584,10 +582,13 @@ Reader::decodeNumber( Token &token )
584582 Value::UInt digit (c - ' 0' );
585583 if ( value >= threshold )
586584 {
587- // If the current digit is not the last one, or if it is
588- // greater than the last digit of the maximum integer value,
589- // the parse the number as a double.
590- if ( current != token.end_ || digit > lastDigitThreshold )
585+ // We've hit or exceeded the max value divided by 10 (rounded down). If
586+ // a) we've only just touched the limit, b) this is the last digit, and
587+ // c) it's small enough to fit in that rounding delta, we're okay.
588+ // Otherwise treat this number as a double to avoid overflow.
589+ if (value > threshold ||
590+ current != token.end_ ||
591+ digit > maxIntegerValue % 10 )
591592 {
592593 return decodeDouble ( token );
593594 }
0 commit comments