mirror of
https://github.com/nlohmann/json.git
synced 2026-10-16 03:07:12 +00:00
Match literals without a stack array and keep parser states in bytes
scan_after_whitespace() built the expected text of true, false, and null in local std::array objects. With -fstack-protector-strong (the default of Ubuntu's GCC, among others), a local array gives the function a stack canary check, which then ran on every token. scan_literal() now takes the text after the first character as a string literal, with its length as a template parameter; for contiguous input it compares the rest at once and falls back to the byte loop on a mismatch, so errors are reported at the same character. It is out of line, which keeps the token dispatch small. The parser kept the nesting of arrays and objects in a std::vector<bool>, whose push_back() GCC calls out of line; it is a std::vector<std::uint8_t> now. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
5 files changed
+154
-53
No files matched your search
@@ -13,6 +13,7 @@
|
||||
using nlohmann::json;
|
||||
|
||||
#include <array> // array
|
||||
#include <cctype> // toupper
|
||||
#include <cstdint> // uint32_t, uint64_t
|
||||
#include <cstdio> // snprintf
|
||||
#include <cmath> // isfinite
|
||||
@@ -596,6 +597,63 @@ TEST_CASE("lexer number fast path")
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("lexer literal fast path")
|
||||
{
|
||||
// For contiguous input, the rest of a literal is compared at once, and a
|
||||
// mismatch is left to the byte path, which reports the error at the
|
||||
// character that differs. Every truncation and every wrong character of
|
||||
// each literal, at the end of the input or followed by more text, must
|
||||
// give the byte path's result (a std::list is read one character at a
|
||||
// time).
|
||||
const auto outcome = [](const std::string & doc, bool byte_path) -> std::string
|
||||
{
|
||||
JSON_TRY
|
||||
{
|
||||
const std::list<char> chars(doc.begin(), doc.end());
|
||||
return (byte_path ? json::parse(chars.begin(), chars.end()) : json::parse(doc)).dump();
|
||||
}
|
||||
JSON_CATCH (const json::exception & e)
|
||||
{
|
||||
return e.what();
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<std::string> mismatches;
|
||||
for (const std::string literal :
|
||||
{"true", "false", "null"
|
||||
})
|
||||
{
|
||||
std::vector<std::string> variants;
|
||||
for (std::size_t length = 1; length <= literal.size(); ++length)
|
||||
{
|
||||
variants.push_back(literal.substr(0, length));
|
||||
}
|
||||
for (std::size_t i = 1; i < literal.size(); ++i)
|
||||
{
|
||||
std::string changed = literal;
|
||||
changed[i] = 'X';
|
||||
variants.push_back(changed);
|
||||
changed[i] = static_cast<char>(std::toupper(static_cast<unsigned char>(literal[i])));
|
||||
variants.push_back(changed);
|
||||
}
|
||||
for (const auto& variant : variants)
|
||||
{
|
||||
for (const std::string& doc :
|
||||
{
|
||||
variant, "[" + variant + "]", "[" + variant, variant + " ", variant + "1", "{\"k\":" + variant + "}"
|
||||
})
|
||||
{
|
||||
if (outcome(doc, false) != outcome(doc, true))
|
||||
{
|
||||
mismatches.push_back(doc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CAPTURE(mismatches)
|
||||
CHECK(mismatches.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("lexer string fast path")
|
||||
{
|
||||
// Build a byte string from explicit values: a hex escape in a string
|
||||
|
||||
@@ -171,7 +171,8 @@ std::vector<std::string> corpus()
|
||||
R"("unterminated)", "\"control\x01character\"", "\"ill-formed \xc3\x28 UTF-8\"", R"("bad \x escape")",
|
||||
R"("\ud800 lone surrogate")",
|
||||
// literals
|
||||
"true", "false", "null", "tru", "nul", "falsey",
|
||||
"true", "false", "null", "tru", "nul", "falsey", "[true,false,null]", "nulL", "truex", "[fals e]", "t",
|
||||
"true1", "null null", "[false]",
|
||||
// structures
|
||||
"[]", "{}", "[1,2,3]", R"({"a":1,"b":[true,false,null],"c":{"d":"e"}})", "[1,]", R"({"a" 1})", "[[[[[]]]]]",
|
||||
// pretty-printed
|
||||
|
||||
Reference in new issue
Block a user