mirror of
https://github.com/nlohmann/json.git
synced 2026-09-09 17:58:00 +00:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
900bec8e0d | ||
|
|
23f07ad8b7 | ||
|
|
48bbe7ceaf | ||
|
|
973972bb5e |
File diff suppressed because it is too large
Load Diff
+440
-312
File diff suppressed because it is too large
Load Diff
@@ -1150,6 +1150,91 @@ TEST_CASE("BSON document size mismatch")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("BSON nesting does not consume the call stack")
|
||||||
|
{
|
||||||
|
// An embedded document or array used to be read by calling back into the
|
||||||
|
// document reader, so the native call stack grew with the nesting depth of
|
||||||
|
// the input (#5104). The open documents are kept on a heap stack now.
|
||||||
|
//
|
||||||
|
// Deeply nested values must not be compared, copied or dumped here: those
|
||||||
|
// operations are still recursive and would reintroduce the crash.
|
||||||
|
|
||||||
|
// A document nested deeply enough to have crashed. The bytes are built
|
||||||
|
// here rather than with to_bson(), because the writer still recurses once
|
||||||
|
// per level and would overflow the stack before the reader is ever
|
||||||
|
// reached. Every level is
|
||||||
|
// <int32 size> 0x03 'a' 0x00 <inner document> 0x00
|
||||||
|
// so a level is eight bytes larger than the one it holds, and the sizes
|
||||||
|
// can be filled in from the outside in.
|
||||||
|
const std::size_t depth = 30000;
|
||||||
|
std::vector<uint8_t> input;
|
||||||
|
input.reserve(5 + (8 * depth));
|
||||||
|
for (std::size_t i = 0; i < depth; ++i)
|
||||||
|
{
|
||||||
|
const auto size = static_cast<std::uint32_t>(5 + (8 * (depth - i)));
|
||||||
|
input.push_back(static_cast<uint8_t>(size & 0xFF));
|
||||||
|
input.push_back(static_cast<uint8_t>((size >> 8) & 0xFF));
|
||||||
|
input.push_back(static_cast<uint8_t>((size >> 16) & 0xFF));
|
||||||
|
input.push_back(static_cast<uint8_t>((size >> 24) & 0xFF));
|
||||||
|
input.push_back(0x03); // embedded document
|
||||||
|
input.push_back('a');
|
||||||
|
input.push_back(0x00);
|
||||||
|
}
|
||||||
|
// the innermost document is empty, then one terminator closes each level
|
||||||
|
input.insert(input.end(), {0x05, 0x00, 0x00, 0x00, 0x00});
|
||||||
|
input.insert(input.end(), depth, 0x00);
|
||||||
|
|
||||||
|
SECTION("a well-formed deep document is read through the SAX interface")
|
||||||
|
{
|
||||||
|
SaxCountdown accept_all(1000000);
|
||||||
|
CHECK(json::sax_parse(input, &accept_all, json::input_format_t::bson));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep document is read into a value")
|
||||||
|
{
|
||||||
|
json j = json::from_bson(input);
|
||||||
|
|
||||||
|
// walked rather than compared: comparing, copying or dumping a value
|
||||||
|
// this deep is still recursive
|
||||||
|
std::size_t measured = 0;
|
||||||
|
const json* q = &j;
|
||||||
|
while (q->is_object() && !q->empty())
|
||||||
|
{
|
||||||
|
q = &q->begin().value();
|
||||||
|
++measured;
|
||||||
|
}
|
||||||
|
CHECK(measured == depth);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("embedded documents and arrays are still read the same way")
|
||||||
|
{
|
||||||
|
const json values = {{"a", {{"b", {{"c", 1}}}}}};
|
||||||
|
CHECK(json::from_bson(json::to_bson(values)) == values);
|
||||||
|
|
||||||
|
const json array = {{"a", {1, 2, 3}}};
|
||||||
|
CHECK(json::from_bson(json::to_bson(array)) == array);
|
||||||
|
|
||||||
|
const json mixed = {{"a", {json{{"x", 1}}, json{{"y", 2}}}}};
|
||||||
|
CHECK(json::from_bson(json::to_bson(mixed)) == mixed);
|
||||||
|
|
||||||
|
CHECK(json::from_bson(json::to_bson(json::object())) == json::object());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a size that does not match is still reported per document")
|
||||||
|
{
|
||||||
|
// the embedded document claims one byte too many
|
||||||
|
std::vector<uint8_t> const bad =
|
||||||
|
{
|
||||||
|
0x15, 0x00, 0x00, 0x00, 0x03, 'a', 0x00,
|
||||||
|
0x0D, 0x00, 0x00, 0x00, 0x08, 'b', 0x00, 0x01, 0x00,
|
||||||
|
0x00
|
||||||
|
};
|
||||||
|
json _;
|
||||||
|
CHECK_THROWS_AS(_ = json::from_bson(bad), json::parse_error&);
|
||||||
|
CHECK(json::from_bson(bad, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("BSON numerical data")
|
TEST_CASE("BSON numerical data")
|
||||||
{
|
{
|
||||||
SECTION("number")
|
SECTION("number")
|
||||||
|
|||||||
@@ -2035,6 +2035,93 @@ TEST_CASE("CBOR definite length equal to the indefinite-length sentinel")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("CBOR nesting does not consume the call stack")
|
||||||
|
{
|
||||||
|
// Containers used to be read by calling back into the value reader once
|
||||||
|
// per element, and a tag by calling it for the tagged value, so the native
|
||||||
|
// call stack grew with the nesting depth of the input. Each of the three
|
||||||
|
// costs a single byte to encode -- 0x9F, 0x81 and 0xC2 -- so a payload of
|
||||||
|
// repeated bytes crashed the process (#5104). The containers are kept on a
|
||||||
|
// heap stack now, and a tag is read in a loop.
|
||||||
|
//
|
||||||
|
// Deeply nested values must not be compared, copied or dumped here: those
|
||||||
|
// operations are still recursive and would reintroduce the crash.
|
||||||
|
json _;
|
||||||
|
|
||||||
|
SECTION("indefinite-length containers")
|
||||||
|
{
|
||||||
|
const std::vector<uint8_t> input(500000, 0x9F);
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.parse_error.110] parse error at byte 500001: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_cbor(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("definite-length containers")
|
||||||
|
{
|
||||||
|
const std::vector<uint8_t> input(500000, 0x81);
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.parse_error.110] parse error at byte 500001: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_cbor(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("tags")
|
||||||
|
{
|
||||||
|
// a tag is not a value of its own, so a chain of them used to recurse
|
||||||
|
const std::vector<uint8_t> input(500000, 0xC2);
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input, true, true, json::cbor_tag_handler_t::ignore), "[json.exception.parse_error.110] parse error at byte 500001: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_cbor(input, true, false, json::cbor_tag_handler_t::ignore).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read through the SAX interface")
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> input(200000, 0x9F);
|
||||||
|
input.insert(input.end(), 200000, 0xFF);
|
||||||
|
|
||||||
|
SaxCountdown accept_all(1000000);
|
||||||
|
CHECK(json::sax_parse(input, &accept_all, json::input_format_t::cbor));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read into a value")
|
||||||
|
{
|
||||||
|
const std::size_t depth = 10000;
|
||||||
|
std::vector<uint8_t> input(depth, 0x81);
|
||||||
|
input.push_back(0x00);
|
||||||
|
|
||||||
|
json j = json::from_cbor(input);
|
||||||
|
|
||||||
|
std::size_t measured = 0;
|
||||||
|
const json* p = &j;
|
||||||
|
while (p->is_array() && !p->empty())
|
||||||
|
{
|
||||||
|
p = &p->front();
|
||||||
|
++measured;
|
||||||
|
}
|
||||||
|
CHECK(measured == depth);
|
||||||
|
CHECK(p->is_number());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("containers are still read the same way")
|
||||||
|
{
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x80})) == json::array());
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xA0})) == json::object());
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x9F, 0xFF})) == json::array());
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xBF, 0xFF})) == json::object());
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x9F, 0x01, 0x02, 0xFF})) == json({1, 2}));
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 'a', 0x01, 0xFF})) == json({{"a", 1}}));
|
||||||
|
// definite and indefinite forms nested inside each other
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x9F, 0x82, 0x01, 0x02, 0xA1, 0x61, 'k', 0xBF, 0xFF, 0xFF})) == json({{1, 2}, {{"k", json::object()}}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("tagged values are still read the same way")
|
||||||
|
{
|
||||||
|
const auto ignore = json::cbor_tag_handler_t::ignore;
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xC2, 0x01}), true, true, ignore) == json(1));
|
||||||
|
// a chain of tags resolves to the value that follows it
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xC2, 0xC2, 0xC2, 0x01}), true, true, ignore) == json(1));
|
||||||
|
// a tag inside a container, and one in front of a container
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x82, 0xC2, 0x01, 0x02}), true, true, ignore) == json({1, 2}));
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xC2, 0x82, 0x01, 0x02}), true, true, ignore) == json({1, 2}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("CBOR indefinite-length strings do not recurse per chunk")
|
TEST_CASE("CBOR indefinite-length strings do not recurse per chunk")
|
||||||
{
|
{
|
||||||
// Reading an indefinite-length string or byte array used to call itself
|
// Reading an indefinite-length string or byte array used to call itself
|
||||||
|
|||||||
@@ -1598,6 +1598,67 @@ TEST_CASE("MessagePack")
|
|||||||
}
|
}
|
||||||
|
|
||||||
// use this testcase outside [hide] to run it with Valgrind
|
// use this testcase outside [hide] to run it with Valgrind
|
||||||
|
TEST_CASE("MessagePack nesting does not consume the call stack")
|
||||||
|
{
|
||||||
|
// Reading a container used to call back into the value reader once per
|
||||||
|
// element, so the native call stack grew with the nesting depth of the
|
||||||
|
// input: one frame per byte for repeated 0x91 (a one-element array), which
|
||||||
|
// crashes the process long before the input is exhausted (#5104). The
|
||||||
|
// containers are kept on a heap stack now.
|
||||||
|
//
|
||||||
|
// Note that deeply nested values must not be compared, copied or dumped
|
||||||
|
// here: those operations are still recursive, and would reintroduce the
|
||||||
|
// very crash this checks for. Depth is measured by descending instead.
|
||||||
|
|
||||||
|
SECTION("an unterminated chain is reported, not crashed on")
|
||||||
|
{
|
||||||
|
json _;
|
||||||
|
const std::vector<uint8_t> input(300000, 0x91);
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(input), "[json.exception.parse_error.110] parse error at byte 300001: syntax error while parsing MessagePack value: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_msgpack(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read through the SAX interface")
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> input(300000, 0x91);
|
||||||
|
input.push_back(0x01); // innermost value
|
||||||
|
|
||||||
|
SaxCountdown accept_all(600001);
|
||||||
|
CHECK(json::sax_parse(input, &accept_all, json::input_format_t::msgpack));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read into a value")
|
||||||
|
{
|
||||||
|
const std::size_t depth = 10000;
|
||||||
|
std::vector<uint8_t> input(depth, 0x91);
|
||||||
|
input.push_back(0x01);
|
||||||
|
|
||||||
|
json j = json::from_msgpack(input);
|
||||||
|
|
||||||
|
std::size_t measured = 0;
|
||||||
|
const json* p = &j;
|
||||||
|
while (p->is_array() && !p->empty())
|
||||||
|
{
|
||||||
|
p = &p->front();
|
||||||
|
++measured;
|
||||||
|
}
|
||||||
|
CHECK(measured == depth);
|
||||||
|
CHECK(p->is_number());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("containers are still read the same way")
|
||||||
|
{
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0x90})) == json::array());
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0x80})) == json::object());
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0x92, 0x90, 0x80})) == json({json::array(), json::object()}));
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0x91, 0x91, 0x91, 0x90})) == json({{{json::array()}}}));
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0x81, 0xA1, 'a', 0x81, 0xA1, 'b', 0x92, 0x01, 0x02})) == json({{"a", {{"b", {1, 2}}}}}));
|
||||||
|
// array 16 and map 32, i.e. the counted forms
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0xDC, 0x00, 0x02, 0x01, 0x02})) == json({1, 2}));
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0xDF, 0x00, 0x00, 0x00, 0x01, 0xA1, 'k', 0xC3})) == json({{"k", true}}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("single MessagePack roundtrip")
|
TEST_CASE("single MessagePack roundtrip")
|
||||||
{
|
{
|
||||||
SECTION("sample.json")
|
SECTION("sample.json")
|
||||||
|
|||||||
@@ -2149,6 +2149,111 @@ TEST_CASE("UBJSON")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("UBJSON nesting does not consume the call stack")
|
||||||
|
{
|
||||||
|
// Containers used to be read by calling back into the value reader once
|
||||||
|
// per element, so the native call stack grew with the nesting depth of the
|
||||||
|
// input. '[' alone opens a container, so a payload of repeated '[' crashed
|
||||||
|
// the process (#5104), as did the optimized forms, which reach the same
|
||||||
|
// path through a type or size annotation. The containers are kept on a
|
||||||
|
// heap stack now.
|
||||||
|
//
|
||||||
|
// Deeply nested values must not be compared, copied or dumped here: those
|
||||||
|
// operations are still recursive and would reintroduce the crash.
|
||||||
|
json _;
|
||||||
|
|
||||||
|
SECTION("containers that end at a marker")
|
||||||
|
{
|
||||||
|
const std::vector<uint8_t> input(500000, '[');
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(input), "[json.exception.parse_error.110] parse error at byte 500001: syntax error while parsing UBJSON value: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_ubjson(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("containers with a size")
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> input;
|
||||||
|
for (std::size_t i = 0; i < 100000; ++i)
|
||||||
|
{
|
||||||
|
input.push_back('[');
|
||||||
|
input.push_back('#');
|
||||||
|
input.push_back('i');
|
||||||
|
input.push_back(1);
|
||||||
|
}
|
||||||
|
CHECK_THROWS_AS(_ = json::from_ubjson(input), json::parse_error&);
|
||||||
|
CHECK(json::from_ubjson(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("containers with a type and a size")
|
||||||
|
{
|
||||||
|
// '[' is a permitted optimized type in UBJSON, so each element of such
|
||||||
|
// a container is itself a container, read without a marker of its own
|
||||||
|
std::vector<uint8_t> input;
|
||||||
|
for (std::size_t i = 0; i < 100000; ++i)
|
||||||
|
{
|
||||||
|
const std::vector<uint8_t> level = {'[', '$', '[', '#', 'i', 1};
|
||||||
|
input.insert(input.end(), level.begin(), level.end());
|
||||||
|
}
|
||||||
|
CHECK_THROWS_AS(_ = json::from_ubjson(input), json::parse_error&);
|
||||||
|
CHECK(json::from_ubjson(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read through the SAX interface")
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> input(100000, '[');
|
||||||
|
input.insert(input.end(), 100000, ']');
|
||||||
|
|
||||||
|
SaxCountdown accept_all(1000000);
|
||||||
|
CHECK(json::sax_parse(input, &accept_all, json::input_format_t::ubjson));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read into a value")
|
||||||
|
{
|
||||||
|
const std::size_t depth = 10000;
|
||||||
|
std::vector<uint8_t> input(depth, '[');
|
||||||
|
input.insert(input.end(), depth, ']');
|
||||||
|
|
||||||
|
json j = json::from_ubjson(input);
|
||||||
|
|
||||||
|
std::size_t measured = 0;
|
||||||
|
const json* p = &j;
|
||||||
|
while (p->is_array() && !p->empty())
|
||||||
|
{
|
||||||
|
p = &p->front();
|
||||||
|
++measured;
|
||||||
|
}
|
||||||
|
// the innermost array is empty, so the descent stops one level short
|
||||||
|
CHECK(measured == depth - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("containers are still read the same way")
|
||||||
|
{
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', ']'})) == json::array());
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'{', '}'})) == json::object());
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '#', 'i', 0})) == json::array());
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'{', '#', 'i', 0})) == json::object());
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'i', '#', 'i', 2, 1, 2})) == json({1, 2}));
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '#', 'i', 2, 'i', 1, 'i', 2})) == json({1, 2}));
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'{', '$', 'i', '#', 'i', 1, 'i', 1, 'a', 1})) == json({{"a", 1}}));
|
||||||
|
// a no-op is not a value, so a container of them holds none
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'N', '#', 'i', 2})) == json::array());
|
||||||
|
// sized and unsized forms nested inside one another
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '[', '#', 'i', 2, 'i', 1, 'i', 2, ']'})) == json({{1, 2}}));
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '#', 'i', 1, '[', 'i', 1, ']'})) == json({{1}}));
|
||||||
|
// an optimized container of containers
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', '[', '#', 'i', 2, 'i', 1, ']', 'i', 2, ']'})) == json({{1}, {2}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("BJData containers are still read the same way")
|
||||||
|
{
|
||||||
|
// the ND-array wrapper and the binary shortcut are complete values,
|
||||||
|
// not containers the reader descends into
|
||||||
|
CHECK(json::from_bjdata(std::vector<uint8_t>({'[', '$', 'U', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 1, 2, 3, 4, 5, 6})) ==
|
||||||
|
json({{"_ArrayType_", "uint8"}, {"_ArraySize_", {2, 3}}, {"_ArrayData_", {1, 2, 3, 4, 5, 6}}}));
|
||||||
|
CHECK(json::from_bjdata(std::vector<uint8_t>({'[', '$', 'i', '#', 'i', 2, 1, 2})) == json({1, 2}));
|
||||||
|
CHECK(json::from_bjdata(std::vector<uint8_t>({'[', '[', 'i', 1, ']', ']'})) == json({{1}}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("UBJSON optimized arrays of a valueless type are bounded")
|
TEST_CASE("UBJSON optimized arrays of a valueless type are bounded")
|
||||||
{
|
{
|
||||||
// An element of type 'Z', 'T' or 'F' is encoded by its marker alone, so an
|
// An element of type 'Z', 'T' or 'F' is encoded by its marker alone, so an
|
||||||
|
|||||||
Reference in New Issue
Block a user