Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 58 additions & 8 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,36 @@ namespace Json {

using CharReaderPtr = std::unique_ptr<CharReader>;

// Returns true when [begin, end) is a well-formed JSON number token. Used by
// decodeDouble() to distinguish floating-point overflow from malformed tokens
// like "0e" or "0e+".
bool isWellFormedJsonNumber(const char* begin, const char* end) {
const char* p = begin;
if (p != end && *p == '-')
++p;
if (p == end || *p < '0' || *p > '9')
return false;
while (p != end && *p >= '0' && *p <= '9')
++p;
if (p != end && *p == '.') {
++p;
if (p == end || *p < '0' || *p > '9')
return false;
while (p != end && *p >= '0' && *p <= '9')
++p;
}
if (p != end && (*p == 'e' || *p == 'E')) {
++p;
if (p != end && (*p == '+' || *p == '-'))
++p;
if (p == end || *p < '0' || *p > '9')
return false;
while (p != end && *p >= '0' && *p <= '9')
++p;
}
return p == end;
}

// Implementation of class Features
// ////////////////////////////////

Expand Down Expand Up @@ -587,12 +617,22 @@ bool Reader::decodeDouble(Token& token, Value& decoded) {
// operator>> sets failbit for a subnormal result (underflow) even though
// it produced the correctly-rounded value, which made such numbers fail to
// parse back after jsoncpp serialized them. Keep a subnormal value instead
// of rejecting it. See issue #1427. Other failures -- malformed numbers
// like "0e" or "0e+", or non-numbers -- leave the value at zero/non-finite
// and are still rejected.
else if (!std::isinf(value) && std::fpclassify(value) != FP_SUBNORMAL)
// of rejecting it. See issue #1427.
else if (std::fpclassify(value) == FP_SUBNORMAL) {
// keep the correctly-rounded subnormal value
}
// Some standard libraries (e.g. MSVC) set failbit on overflow without
// writing the max()/lowest() sentinels, leaving `value` untouched. If the
// token is a well-formed number, a failed conversion means it is out of
// range: map it to +/-infinity so parsing behaves identically everywhere
// (see issues #1446 and #1405).
else if (isWellFormedJsonNumber(token.start_, token.end_)) {
value = *token.start_ == '-' ? -std::numeric_limits<double>::infinity()
: std::numeric_limits<double>::infinity();
} else {
return addError(
"'" + String(token.start_, token.end_) + "' is not a number.", token);
}
}
decoded = value;
return true;
Expand Down Expand Up @@ -1678,12 +1718,22 @@ bool OurReader::decodeDouble(Token& token, Value& decoded) {
// operator>> sets failbit for a subnormal result (underflow) even though
// it produced the correctly-rounded value, which made such numbers fail to
// parse back after jsoncpp serialized them. Keep a subnormal value instead
// of rejecting it. See issue #1427. Other failures -- malformed numbers
// like "0e" or "0e+", or non-numbers -- leave the value at zero/non-finite
// and are still rejected.
else if (!std::isinf(value) && std::fpclassify(value) != FP_SUBNORMAL)
// of rejecting it. See issue #1427.
else if (std::fpclassify(value) == FP_SUBNORMAL) {
// keep the correctly-rounded subnormal value
}
// Some standard libraries (e.g. MSVC) set failbit on overflow without
// writing the max()/lowest() sentinels, leaving `value` untouched. If the
// token is a well-formed number, a failed conversion means it is out of
// range: map it to +/-infinity so parsing behaves identically everywhere
// (see issues #1446 and #1405).
else if (isWellFormedJsonNumber(token.start_, token.end_)) {
value = *token.start_ == '-' ? -std::numeric_limits<double>::infinity()
: std::numeric_limits<double>::infinity();
Comment on lines +1730 to +1732

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Tiny numbers become infinity

If the standard library sets failbit and returns zero for a tiny number such as 1e-9999, zero skips the FP_SUBNORMAL branch. isWellFormedJsonNumber then makes the reader report success with infinity instead of zero or a parse error. Both decodeDouble implementations have this problem. Distinguish overflow from underflow before assigning infinity, and add positive and negative underflow tests.

Knowledge Base Used: Parser internals

} else {
return addError(
"'" + String(token.start_, token.end_) + "' is not a number.", token);
}
}
decoded = value;
return true;
Expand Down
80 changes: 80 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3198,6 +3198,25 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseSpecialFloat) {
"expected.\n");
}

JSONTEST_FIXTURE_LOCAL(ReaderTest, parseOverflow) {
// The legacy Reader has the same overflow handling as CharReader;
// out-of-range numbers must parse to +/-infinity (see #1446, #1405).
{
std::string doc = "[-1e+9999, 1e+9999]";
JSONTEST_ASSERT(reader->parse(doc, root));
JSONTEST_ASSERT_EQUAL(root.size(), 2u);
JSONTEST_ASSERT(std::isinf(root[0].asDouble()));
JSONTEST_ASSERT(root[0].asDouble() < 0);
JSONTEST_ASSERT(std::isinf(root[1].asDouble()));
JSONTEST_ASSERT(root[1].asDouble() > 0);
}
{
// Malformed numbers are still rejected.
std::string doc = "[0e]";
JSONTEST_ASSERT(!reader->parse(doc, root));
}
}

JSONTEST_FIXTURE_LOCAL(ReaderTest, strictModeParseNumber) {
setStrictMode();
checkParse(
Expand Down Expand Up @@ -3349,6 +3368,67 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseSubnormal) {
}
}

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseOverflow) {
// Regression test for #1446/#1405: out-of-range numbers must parse to
// +/-infinity on every platform. Some standard libraries (e.g. MSVC) report
// overflow as a failed extraction without writing the max()/lowest()
// sentinels, which used to make tokens like "-1e+9999" fail to parse.
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
Json::String errs;

const struct {
const char* doc;
double expected;
} cases[] = {
{"[1e+9999]", std::numeric_limits<double>::infinity()},
{"[-1e+9999]", -std::numeric_limits<double>::infinity()},
{"[1e309]", std::numeric_limits<double>::infinity()},
{"[-1e309]", -std::numeric_limits<double>::infinity()},
// An integer too large for any integral or floating-point type maps to
// infinity as well.
{"[9e99999999999999999999999999999999999999999999999999999999999999]",
std::numeric_limits<double>::infinity()},
Comment on lines +3388 to +3391

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Integer overflow lacks coverage

The case described as an oversized integer uses 9e..., so it tests another exponent rather than an all-digit integer. Replace it with a number containing more than 309 digits and no exponent. That covers the integer-limit path through decodeNumber into decodeDouble, which the new tests currently miss.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

};
for (const auto& c : cases) {
Json::Value root;
bool ok = reader->parse(c.doc, c.doc + std::strlen(c.doc), &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(errs.empty());
JSONTEST_ASSERT(std::isinf(root[0].asDouble()));
JSONTEST_ASSERT_EQUAL(c.expected, root[0].asDouble());
}

// The exact shape of test/data/legacy_test_real_13.json.
{
Json::Value root;
char const doc[] = "[-1e+9999, 1e+9999]";
bool ok = reader->parse(doc, doc + sizeof(doc) - 1, &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT_EQUAL(root.size(), 2u);
JSONTEST_ASSERT(std::isinf(root[0].asDouble()));
JSONTEST_ASSERT(root[0].asDouble() < 0);
JSONTEST_ASSERT(std::isinf(root[1].asDouble()));
JSONTEST_ASSERT(root[1].asDouble() > 0);
}

// Infinity written by the default writer (useSpecialFloats=false)
// round-trips.
{
Json::Value v(Json::arrayValue);
v[0] = std::numeric_limits<double>::infinity();
v[1] = -std::numeric_limits<double>::infinity();
const Json::String doc = Json::writeString(Json::StreamWriterBuilder(), v);
Json::Value root;
bool ok = reader->parse(doc.data(), doc.data() + doc.size(), &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(std::isinf(root[0].asDouble()));
JSONTEST_ASSERT(root[0].asDouble() > 0);
JSONTEST_ASSERT(std::isinf(root[1].asDouble()));
JSONTEST_ASSERT(root[1].asDouble() < 0);
}
}

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseString) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
Expand Down
Loading