Skip to content

Commit 9ad6e8b

Browse files
committed
Change OurReader to use string stream
1 parent 7e1a02d commit 9ad6e8b

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

src/lib_json/json_reader.cpp

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,9 +1524,10 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
15241524
// larger than the maximum supported value of an integer then
15251525
// we decode the number as a double.
15261526
Location current = token.start_;
1527-
bool isNegative = *current == '-';
1528-
if (isNegative)
1527+
const bool isNegative = *current == '-';
1528+
if (isNegative) {
15291529
++current;
1530+
}
15301531

15311532
// We assume we can represent the largest and smallest integer types as
15321533
// unsigned integers with separate sign. This is only true if they can fit
@@ -1554,9 +1555,9 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
15541555
// then take the inverse. This assumes that minLargestInt is only a single
15551556
// power of 10 different in magnitude, which we check above. For the last
15561557
// 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 =
15581559
Value::LargestUInt(-(Value::minLargestInt / 10));
1559-
static const Value::UInt negative_last_digit =
1560+
static constexpr Value::UInt negative_last_digit =
15601561
Value::UInt(-(Value::minLargestInt % 10));
15611562

15621563
const Value::LargestUInt threshold =
@@ -1610,37 +1611,12 @@ bool OurReader::decodeDouble(Token& token) {
16101611

16111612
bool OurReader::decodeDouble(Token& token, Value& decoded) {
16121613
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)) {
16421617
return addError(
16431618
"'" + String(token.start_, token.end_) + "' is not a number.", token);
1619+
}
16441620
decoded = value;
16451621
return true;
16461622
}
@@ -1658,13 +1634,13 @@ bool OurReader::decodeString(Token& token) {
16581634

16591635
bool OurReader::decodeString(Token& token, String& decoded) {
16601636
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 '"'
16631639
while (current != end) {
16641640
Char c = *current++;
1665-
if (c == '"')
1641+
if (c == '"') {
16661642
break;
1667-
else if (c == '\\') {
1643+
} else if (c == '\\') {
16681644
if (current == end)
16691645
return addError("Empty escape sequence in string", token, current);
16701646
Char escape = *current++;

0 commit comments

Comments
 (0)