@@ -1524,9 +1524,10 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
1524
1524
// larger than the maximum supported value of an integer then
1525
1525
// we decode the number as a double.
1526
1526
Location current = token.start_ ;
1527
- bool isNegative = *current == ' -' ;
1528
- if (isNegative)
1527
+ const bool isNegative = *current == ' -' ;
1528
+ if (isNegative) {
1529
1529
++current;
1530
+ }
1530
1531
1531
1532
// We assume we can represent the largest and smallest integer types as
1532
1533
// unsigned integers with separate sign. This is only true if they can fit
@@ -1554,9 +1555,9 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
1554
1555
// then take the inverse. This assumes that minLargestInt is only a single
1555
1556
// power of 10 different in magnitude, which we check above. For the last
1556
1557
// digit, we take the modulus before negating for the same reason.
1557
- static const Value::LargestUInt negative_threshold =
1558
+ static constexpr Value::LargestUInt negative_threshold =
1558
1559
Value::LargestUInt (-(Value::minLargestInt / 10 ));
1559
- static const Value::UInt negative_last_digit =
1560
+ static constexpr Value::UInt negative_last_digit =
1560
1561
Value::UInt (-(Value::minLargestInt % 10 ));
1561
1562
1562
1563
const Value::LargestUInt threshold =
@@ -1610,37 +1611,12 @@ bool OurReader::decodeDouble(Token& token) {
1610
1611
1611
1612
bool OurReader::decodeDouble (Token& token, Value& decoded) {
1612
1613
double value = 0 ;
1613
- const int bufferSize = 32 ;
1614
- int count;
1615
- ptrdiff_t const length = token.end_ - token.start_ ;
1616
-
1617
- // Sanity check to avoid buffer overflow exploits.
1618
- if (length < 0 ) {
1619
- return addError (" Unable to parse token length" , token);
1620
- }
1621
- auto const ulength = static_cast <size_t >(length);
1622
-
1623
- // Avoid using a string constant for the format control string given to
1624
- // sscanf, as this can cause hard to debug crashes on OS X. See here for more
1625
- // info:
1626
- //
1627
- // http://developer.apple.com/library/mac/#DOCUMENTATION/DeveloperTools/gcc-4.0.1/gcc/Incompatibilities.html
1628
- char format[] = " %lf" ;
1629
-
1630
- if (length <= bufferSize) {
1631
- Char buffer[bufferSize + 1 ];
1632
- memcpy (buffer, token.start_ , ulength);
1633
- buffer[length] = 0 ;
1634
- fixNumericLocaleInput (buffer, buffer + length);
1635
- count = sscanf (buffer, format, &value);
1636
- } else {
1637
- String buffer (token.start_ , token.end_ );
1638
- count = sscanf (buffer.c_str (), format, &value);
1639
- }
1640
-
1641
- if (count != 1 )
1614
+ const String buffer (token.start_ , token.end_ );
1615
+ IStringStream is (buffer);
1616
+ if (!(is >> value)) {
1642
1617
return addError (
1643
1618
" '" + String (token.start_ , token.end_ ) + " ' is not a number." , token);
1619
+ }
1644
1620
decoded = value;
1645
1621
return true ;
1646
1622
}
@@ -1658,13 +1634,13 @@ bool OurReader::decodeString(Token& token) {
1658
1634
1659
1635
bool OurReader::decodeString (Token& token, String& decoded) {
1660
1636
decoded.reserve (static_cast <size_t >(token.end_ - token.start_ - 2 ));
1661
- Location current = token.start_ + 1 ; // skip '"'
1662
- Location end = token.end_ - 1 ; // do not include '"'
1637
+ Location current = token.start_ + 1 ; // skip '"'
1638
+ Location end = token.end_ - 1 ; // do not include '"'
1663
1639
while (current != end) {
1664
1640
Char c = *current++;
1665
- if (c == ' "' )
1641
+ if (c == ' "' ) {
1666
1642
break ;
1667
- else if (c == ' \\ ' ) {
1643
+ } else if (c == ' \\ ' ) {
1668
1644
if (current == end)
1669
1645
return addError (" Empty escape sequence in string" , token, current);
1670
1646
Char escape = *current++;
0 commit comments