mirror of
https://github.com/nlohmann/json.git
synced 2026-09-07 00:37:58 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89994acc2c | ||
|
|
7dc308d952 | ||
|
|
68e4094cdc | ||
|
|
5b26411166 | ||
|
|
d89acce09a | ||
|
|
bcd5af62c7 | ||
|
|
25a4333a31 | ||
|
|
c3219fdc30 |
@@ -69,6 +69,12 @@ The library uses the following mapping from JSON values types to UBJSON types ac
|
||||
Note that `use_size = true` alone may result in larger representations - the benefit of this parameter is that the
|
||||
receiving side is immediately informed on the number of elements of the container.
|
||||
|
||||
An array whose type marker is `Z` (null), `T` (true) or `F` (false) stores no payload at all, because the marker
|
||||
already is the value. Its declared count is therefore the only thing that decides how much memory the receiving side
|
||||
allocates, and a handful of bytes can describe billions of elements. `from_ubjson` rejects such an array with
|
||||
[`out_of_range.408`](../../home/exceptions.md#jsonexceptionout_of_range408) when the count exceeds 1,048,576, and
|
||||
`to_ubjson` writes longer arrays of these types without the annotation, so any value it produces can be read back.
|
||||
|
||||
!!! info "Binary values"
|
||||
|
||||
If the JSON data contains the binary type, the value stored is a list of integers, as suggested by the UBJSON
|
||||
|
||||
@@ -868,6 +868,12 @@ The size of an array or object in a [binary format](../features/binary_formats/i
|
||||
the size following `#` for [UBJSON](../features/binary_formats/ubjson.md)/[BJData](../features/binary_formats/bjdata.md),
|
||||
or the encoded length for [CBOR](../features/binary_formats/cbor.md).
|
||||
|
||||
The exception is also thrown for a [UBJSON](../features/binary_formats/ubjson.md) array of a type that is encoded by its
|
||||
marker alone (`Z`, `T` or `F`) whose declared count exceeds 1,048,576. Such an array has no payload, so its count alone
|
||||
decides how much memory is allocated, and a handful of bytes would otherwise describe billions of values.
|
||||
[`to_ubjson`](../api/basic_json/to_ubjson.md) writes longer arrays of these types without the size and type annotation,
|
||||
so any value it produces can still be read back.
|
||||
|
||||
!!! failure "Example messages"
|
||||
|
||||
```
|
||||
@@ -879,6 +885,9 @@ or the encoded length for [CBOR](../features/binary_formats/cbor.md).
|
||||
```
|
||||
[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive map size
|
||||
```
|
||||
```
|
||||
[json.exception.out_of_range.408] syntax error while parsing UBJSON size: excessive array size
|
||||
```
|
||||
|
||||
### json.exception.out_of_range.409
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -301,16 +301,6 @@ class json_sax_dom_parser
|
||||
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
|
||||
}
|
||||
|
||||
if (len != detail::unknown_size())
|
||||
{
|
||||
// reserve upfront to avoid repeated reallocations while adding elements,
|
||||
// but cap the reservation so a bogus/hostile length (which is not bounded
|
||||
// by max_size(), unlike e.g. std::vector) cannot trigger an oversized
|
||||
// allocation for a small or truncated input
|
||||
constexpr std::size_t reserve_cap = 16384;
|
||||
ref_stack.back()->m_data.m_value.array->reserve(len < reserve_cap ? len : reserve_cap);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -671,16 +661,6 @@ class json_sax_dom_callback_parser
|
||||
{
|
||||
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
|
||||
}
|
||||
|
||||
if (len != detail::unknown_size())
|
||||
{
|
||||
// reserve upfront to avoid repeated reallocations while adding elements,
|
||||
// but cap the reservation so a bogus/hostile length (which is not bounded
|
||||
// by max_size(), unlike e.g. std::vector) cannot trigger an oversized
|
||||
// allocation for a small or truncated input
|
||||
constexpr std::size_t reserve_cap = 16384;
|
||||
ref_stack.back()->m_data.m_value.array->reserve(len < reserve_cap ? len : reserve_cap);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -826,7 +826,17 @@ class binary_writer
|
||||
|
||||
std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
|
||||
|
||||
if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
|
||||
// an optimized array of a valueless type carries no payload, so a
|
||||
// reader has nothing but the declared count to bound the allocation
|
||||
// by and refuses an excessive one. Write the unoptimized form for
|
||||
// those, at one byte per element, so the result can be read back.
|
||||
// Objects are not affected: every element is preceded by its key.
|
||||
const bool valueless_type = (first_prefix == 'Z' || first_prefix == 'T' || first_prefix == 'F');
|
||||
const bool excessive_valueless = valueless_type
|
||||
&& j.m_data.m_value.array->size() > detail::max_valueless_container_size;
|
||||
|
||||
if (same_prefix && !excessive_valueless
|
||||
&& !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
|
||||
{
|
||||
prefix_required = false;
|
||||
oa->write_character(to_char_type('$'));
|
||||
|
||||
+14
-14
@@ -4474,7 +4474,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
/// @brief create a JSON value from an input in CBOR format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||
@@ -4491,7 +4491,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -4517,7 +4517,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
/// @brief create a JSON value from an input in MessagePack format
|
||||
@@ -4532,7 +4532,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
/// @brief create a JSON value from an input in MessagePack format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||
@@ -4548,7 +4548,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -4572,7 +4572,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
/// @brief create a JSON value from an input in UBJSON format
|
||||
@@ -4587,7 +4587,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
/// @brief create a JSON value from an input in UBJSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||
@@ -4603,7 +4603,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -4627,7 +4627,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
/// @brief create a JSON value from an input in BJData format
|
||||
@@ -4642,7 +4642,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
/// @brief create a JSON value from an input in BJData format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||
@@ -4658,7 +4658,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
/// @brief create a JSON value from an input in BSON format
|
||||
@@ -4673,7 +4673,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
/// @brief create a JSON value from an input in BSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||
@@ -4689,7 +4689,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -4713,7 +4713,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||
return res ? result : basic_json(value_t::discarded);
|
||||
return res ? std::move(result) : basic_json(value_t::discarded);
|
||||
}
|
||||
/// @}
|
||||
|
||||
|
||||
+615
-393
File diff suppressed because it is too large
Load Diff
+18
-108
@@ -3288,8 +3288,10 @@ TEST_CASE("BJData")
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR1), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||
CHECK(json::from_bjdata(vR1, true, false).is_discarded());
|
||||
|
||||
// a dimension vector that opens another one is rejected where the
|
||||
// nested '[' is read, rather than after it has been descended into
|
||||
std::vector<uint8_t> const vR2 = {'[', '$', 'i', '#', '[', '#', '[', 'i', 1, ']', ']', 1};
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR2), "[json.exception.parse_error.113] parse error at byte 11: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x5D", json::parse_error&);
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR2), "[json.exception.parse_error.113] parse error at byte 7: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||
CHECK(json::from_bjdata(vR2, true, false).is_discarded());
|
||||
|
||||
std::vector<uint8_t> const vR3 = {'[', '#', '[', 'i', '2', 'i', 2, ']'};
|
||||
@@ -3297,7 +3299,7 @@ TEST_CASE("BJData")
|
||||
CHECK(json::from_bjdata(vR3, true, false).is_discarded());
|
||||
|
||||
std::vector<uint8_t> const vR4 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', 1, ']', 1};
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR4), "[json.exception.parse_error.110] parse error at byte 14: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR4), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||
CHECK(json::from_bjdata(vR4, true, false).is_discarded());
|
||||
|
||||
std::vector<uint8_t> const vR5 = {'[', '$', 'i', '#', '[', '[', '[', ']', ']', ']'};
|
||||
@@ -3305,12 +3307,25 @@ TEST_CASE("BJData")
|
||||
CHECK(json::from_bjdata(vR5, true, false).is_discarded());
|
||||
|
||||
std::vector<uint8_t> const vR6 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR6), "[json.exception.parse_error.112] parse error at byte 14: syntax error while parsing BJData size: ndarray can not be recursive", json::parse_error&);
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR6), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||
CHECK(json::from_bjdata(vR6, true, false).is_discarded());
|
||||
|
||||
std::vector<uint8_t> const vH = {'[', 'H', '[', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vH), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||
CHECK(json::from_bjdata(vH, true, false).is_discarded());
|
||||
|
||||
// Every "#[" of this chain used to open another dimension vector
|
||||
// and cost several stack frames before anything was rejected, so a
|
||||
// long enough chain crashed the process (see #5104). The nested
|
||||
// vector is refused where it is read, so the length is irrelevant.
|
||||
std::vector<uint8_t> vRdeep = {'['};
|
||||
for (std::size_t i = 0; i < 100000; ++i)
|
||||
{
|
||||
vRdeep.push_back('#');
|
||||
vRdeep.push_back('[');
|
||||
}
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vRdeep), "[json.exception.parse_error.113] parse error at byte 5: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||
CHECK(json::from_bjdata(vRdeep, true, false).is_discarded());
|
||||
}
|
||||
|
||||
SECTION("objects")
|
||||
@@ -3489,111 +3504,6 @@ TEST_CASE("BJData")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("issue #5405 - array reserve for definite-length BJData arrays")
|
||||
{
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
// this SECTION relies on catching a thrown exception to distinguish
|
||||
// which of two acceptable, bounded rejections a hostile header took;
|
||||
// under JSON_NOEXCEPTION, JSON_THROW never produces a catchable C++
|
||||
// exception (it aborts instead), so this cannot be tested that way here
|
||||
SECTION("a huge claimed length with no element data must not over-allocate")
|
||||
{
|
||||
// optimized form [$type#count: type 'i' (int8), count as a four-byte
|
||||
// little-endian 'l' (int32) of 0x7FFFFFFF (2147483647), but no
|
||||
// element data at all. max_size() for a std::vector is far larger
|
||||
// than this count, so it does not reject the header outright; the
|
||||
// (capped) reservation must not attempt to allocate space for
|
||||
// billions of elements before the missing data is detected.
|
||||
json _;
|
||||
const std::vector<uint8_t> input = {'[', '$', 'i', '#', 'l', 0xFF, 0xFF, 0xFF, 0x7F};
|
||||
// On a platform where std::vector<json>::max_size() is smaller than
|
||||
// the claimed count (e.g. 32-bit, where max_size() is bounded by a
|
||||
// 32-bit SIZE_MAX divided by sizeof(json)), the SAX consumer's own
|
||||
// check rejects the header outright (out_of_range.408, with the
|
||||
// claimed count in the message) instead of accepting it and only
|
||||
// finding it short of data once the (capped) reservation looks for
|
||||
// element bytes that were never provided (parse_error.110). Either
|
||||
// is an acceptable, bounded rejection of the hostile header -- the
|
||||
// property under test is that no path attempts to allocate space
|
||||
// for billions of elements.
|
||||
bool threw = false;
|
||||
try
|
||||
{
|
||||
_ = json::from_bjdata(input);
|
||||
}
|
||||
catch (const json::parse_error& e)
|
||||
{
|
||||
threw = true;
|
||||
CHECK(e.id == 110);
|
||||
CHECK(std::string(e.what()) == "[json.exception.parse_error.110] parse error at byte 10: syntax error while parsing BJData number: unexpected end of input");
|
||||
}
|
||||
catch (const json::out_of_range& e)
|
||||
{
|
||||
threw = true;
|
||||
CHECK(e.id == 408);
|
||||
CHECK(std::string(e.what()).find("excessive array size") != std::string::npos);
|
||||
}
|
||||
CHECK(threw);
|
||||
|
||||
// json_sax_dom_parser::start_array()'s max_size() check (unlike the
|
||||
// scanner's own parse_error path) throws unconditionally via
|
||||
// JSON_THROW rather than going through sax->parse_error(), so it is
|
||||
// not gated by allow_exceptions=false on a platform where this
|
||||
// header hits that check (e.g. 32-bit, see above) -- allow either
|
||||
// a discarded result or the same out_of_range it throws with
|
||||
// exceptions enabled.
|
||||
try
|
||||
{
|
||||
CHECK(json::from_bjdata(input, true, false).is_discarded());
|
||||
}
|
||||
catch (const json::out_of_range& e)
|
||||
{
|
||||
CHECK(e.id == 408);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("arrays of various sizes decode to the same value as before the reserve optimization")
|
||||
{
|
||||
for (const auto size :
|
||||
{
|
||||
std::size_t{0}, std::size_t{1}, std::size_t{5}, // small
|
||||
std::size_t{16384}, // exactly at the reserve cap
|
||||
std::size_t{20000} // above the reserve cap
|
||||
})
|
||||
{
|
||||
CAPTURE(size)
|
||||
json j = json::array();
|
||||
for (std::size_t i = 0; i < size; ++i)
|
||||
{
|
||||
j.push_back(static_cast<int>(i % 1000));
|
||||
}
|
||||
|
||||
// exercise both the plain and the optimized [$type#count encoding
|
||||
const auto packed_plain = json::to_bjdata(j);
|
||||
CHECK(json::from_bjdata(packed_plain) == j);
|
||||
|
||||
const auto packed_optimized = json::to_bjdata(j, true, true);
|
||||
CHECK(json::from_bjdata(packed_optimized) == j);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("a user-defined SAX consumer is unaffected by the internal DOM reserve optimization")
|
||||
{
|
||||
// the reserve() call is local to json_sax_dom_parser / json_sax_dom_callback_parser;
|
||||
// a custom SAX consumer that does not touch a DOM array sees identical events
|
||||
json j = json::array();
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
j.push_back(i);
|
||||
}
|
||||
const auto packed = json::to_bjdata(j, true, true);
|
||||
|
||||
SaxCountdown scp(1000000); // large enough to never trigger an abort
|
||||
CHECK(json::sax_parse(packed, &scp, json::input_format_t::bjdata));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Universal Binary JSON Specification Examples 1")
|
||||
{
|
||||
SECTION("Null Value")
|
||||
|
||||
@@ -1011,6 +1011,76 @@ 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, built by to_bson so
|
||||
// that every one of its size prefixes is correct
|
||||
const std::size_t depth = 30000;
|
||||
json deep = json::object();
|
||||
json* p = &deep;
|
||||
for (std::size_t i = 0; i < depth; ++i)
|
||||
{
|
||||
(*p)["a"] = json::object();
|
||||
p = &(*p)["a"];
|
||||
}
|
||||
const std::vector<uint8_t> input = json::to_bson(deep);
|
||||
|
||||
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);
|
||||
|
||||
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")
|
||||
{
|
||||
SECTION("number")
|
||||
|
||||
+122
-69
@@ -2035,89 +2035,142 @@ TEST_CASE("CBOR definite length equal to the indefinite-length sentinel")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("issue #5405 - array reserve for definite-length CBOR arrays")
|
||||
TEST_CASE("CBOR nesting does not consume the call stack")
|
||||
{
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
// this SECTION relies on catching a thrown exception to distinguish
|
||||
// which of two acceptable, bounded rejections a hostile header took;
|
||||
// under JSON_NOEXCEPTION, JSON_THROW never produces a catchable C++
|
||||
// exception (it aborts instead), so this cannot be tested that way here
|
||||
SECTION("a huge claimed length with no element data must not over-allocate")
|
||||
{
|
||||
// 0x9A: array with a four-byte length; claims 0xFFFFFFFF (4294967295)
|
||||
// elements but provides none. max_size() for a std::vector is far
|
||||
// larger than this count, so it does not reject the header outright;
|
||||
// the (capped) reservation must not attempt to allocate space for
|
||||
// billions of elements before the missing data is detected.
|
||||
// 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 _;
|
||||
const std::vector<uint8_t> input = {0x9A, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
// On a platform where std::size_t is narrower than 64 bits (e.g.
|
||||
// 32-bit), the claimed count 0xFFFFFFFF coincides with that
|
||||
// platform's detail::unknown_size() sentinel (SIZE_MAX), so the
|
||||
// format-level size check rejects it outright (out_of_range.408,
|
||||
// "excessive ... size") before the SAX consumer's own max_size()
|
||||
// check would even run; on a 64-bit platform it passes both of
|
||||
// those checks and is only found short of data once the (capped)
|
||||
// reservation looks for element bytes that were never provided
|
||||
// (parse_error.110). Either is an acceptable, bounded rejection of
|
||||
// the hostile header -- the property under test is that no path
|
||||
// attempts to allocate space for billions of elements.
|
||||
bool threw = false;
|
||||
try
|
||||
|
||||
SECTION("indefinite-length containers")
|
||||
{
|
||||
_ = json::from_cbor(input);
|
||||
}
|
||||
catch (const json::parse_error& e)
|
||||
{
|
||||
threw = true;
|
||||
CHECK(e.id == 110);
|
||||
CHECK(std::string(e.what()) == "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing CBOR value: unexpected end of input");
|
||||
}
|
||||
catch (const json::out_of_range& e)
|
||||
{
|
||||
threw = true;
|
||||
CHECK(e.id == 408);
|
||||
CHECK(std::string(e.what()).find("excessive") != std::string::npos);
|
||||
}
|
||||
CHECK(threw);
|
||||
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());
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("arrays of various sizes decode to the same value as before the reserve optimization")
|
||||
SECTION("definite-length containers")
|
||||
{
|
||||
for (const auto size :
|
||||
{
|
||||
std::size_t{0}, std::size_t{1}, std::size_t{5}, // small
|
||||
std::size_t{16384}, // exactly at the reserve cap
|
||||
std::size_t{20000} // above the reserve cap
|
||||
})
|
||||
{
|
||||
CAPTURE(size)
|
||||
json j = json::array();
|
||||
for (std::size_t i = 0; i < size; ++i)
|
||||
{
|
||||
j.push_back(static_cast<int>(i % 1000));
|
||||
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());
|
||||
}
|
||||
|
||||
const auto packed = json::to_cbor(j);
|
||||
CHECK(json::from_cbor(packed) == j);
|
||||
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}));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("a user-defined SAX consumer is unaffected by the internal DOM reserve optimization")
|
||||
TEST_CASE("CBOR indefinite-length strings do not recurse per chunk")
|
||||
{
|
||||
// the reserve() call is local to json_sax_dom_parser / json_sax_dom_callback_parser;
|
||||
// a custom SAX consumer that does not touch a DOM array sees identical events
|
||||
json j = json::array();
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
j.push_back(i);
|
||||
}
|
||||
const auto packed = json::to_cbor(j);
|
||||
// Reading an indefinite-length string or byte array used to call itself
|
||||
// once per chunk, so a payload of repeated 0x7F (or 0x5F) bytes exhausted
|
||||
// the call stack before any of the input was rejected. The open levels are
|
||||
// counted now, and the levels below prove the reader still reads the same
|
||||
// values and reports the same errors at the same byte offsets.
|
||||
json _;
|
||||
|
||||
SaxCountdown scp(1000000); // large enough to never trigger an abort
|
||||
CHECK(json::sax_parse(packed, &scp, json::input_format_t::cbor));
|
||||
SECTION("many open levels are reported, not crashed on")
|
||||
{
|
||||
const std::vector<uint8_t> input(200000, 0x7F);
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.parse_error.110] parse error at byte 200001: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
|
||||
CHECK(json::from_cbor(input, true, false).is_discarded());
|
||||
}
|
||||
|
||||
SECTION("many open levels are reported, not crashed on (binary)")
|
||||
{
|
||||
const std::vector<uint8_t> input(200000, 0x5F);
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.parse_error.110] parse error at byte 200001: syntax error while parsing CBOR binary: unexpected end of input", json::parse_error&);
|
||||
CHECK(json::from_cbor(input, true, false).is_discarded());
|
||||
}
|
||||
|
||||
SECTION("chunks are still concatenated")
|
||||
{
|
||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0xFF})) == json(""));
|
||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0x61, 0x61, 0xFF})) == json("a"));
|
||||
// nested indefinite-length strings are concatenated across levels
|
||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0x7F, 0x61, 0x61, 0xFF, 0x61, 0x62, 0xFF})) == json("ab"));
|
||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0x7F, 0x7F, 0x61, 0x7A, 0xFF, 0xFF, 0xFF})) == json("z"));
|
||||
CHECK(json::from_cbor(std::vector<uint8_t>({0xA1, 0x7F, 0x61, 0x61, 0xFF, 0x01})) == json({{"a", 1}}));
|
||||
}
|
||||
|
||||
SECTION("chunks are still concatenated (binary)")
|
||||
{
|
||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x5F, 0x41, 0x61, 0xFF})) == json::binary({0x61}));
|
||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x5F, 0x5F, 0x41, 0x61, 0xFF, 0x41, 0x62, 0xFF})) == json::binary({0x61, 0x62}));
|
||||
}
|
||||
|
||||
SECTION("a chunk that is not a string is still rejected")
|
||||
{
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_cbor(std::vector<uint8_t>({0x7F, 0x7F, 0x00})), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x00", json::parse_error&);
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_cbor(std::vector<uint8_t>({0x5F, 0x5F, 0x00})), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing CBOR binary: expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x00", json::parse_error&);
|
||||
}
|
||||
|
||||
SECTION("a break marker outside an indefinite-length string is not a string")
|
||||
{
|
||||
// 0xFF only closes a string that was opened; on its own it is not one
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_cbor(std::vector<uint8_t>({0xA1, 0xFF, 0x01})), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0xFF", json::parse_error&);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+50
-74
@@ -1597,92 +1597,68 @@ TEST_CASE("MessagePack")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("issue #5405 - array reserve for definite-length MessagePack arrays")
|
||||
// use this testcase outside [hide] to run it with Valgrind
|
||||
TEST_CASE("MessagePack nesting does not consume the call stack")
|
||||
{
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
// this SECTION relies on catching a thrown exception to distinguish
|
||||
// which of two acceptable, bounded rejections a hostile header took;
|
||||
// under JSON_NOEXCEPTION, JSON_THROW never produces a catchable C++
|
||||
// exception (it aborts instead), so this cannot be tested that way here
|
||||
SECTION("a huge claimed length with no element data must not over-allocate")
|
||||
// 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")
|
||||
{
|
||||
// 0xdd: array 32 (four-byte length); claims 0xFFFFFFFF (4294967295)
|
||||
// elements but provides none. max_size() for a std::vector is far
|
||||
// larger than this count, so it does not reject the header outright;
|
||||
// the (capped) reservation must not attempt to allocate space for
|
||||
// billions of elements before the missing data is detected.
|
||||
json _;
|
||||
const std::vector<uint8_t> input = {0xdd, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
// On a platform where std::size_t is narrower than 64 bits (e.g.
|
||||
// 32-bit), the claimed count 0xFFFFFFFF coincides with that
|
||||
// platform's SIZE_MAX, which some size-narrowing checks treat the
|
||||
// same as detail::unknown_size(); it may then be rejected before
|
||||
// the SAX consumer's own max_size() check (out_of_range.408) rather
|
||||
// than being accepted and only found short of data once the
|
||||
// (capped) reservation looks for element bytes that were never
|
||||
// provided (parse_error.110). Either is an acceptable, bounded
|
||||
// rejection of the hostile header -- the property under test is
|
||||
// that no path attempts to allocate space for billions of elements.
|
||||
bool threw = false;
|
||||
try
|
||||
{
|
||||
_ = json::from_msgpack(input);
|
||||
}
|
||||
catch (const json::parse_error& e)
|
||||
{
|
||||
threw = true;
|
||||
CHECK(e.id == 110);
|
||||
CHECK(std::string(e.what()) == "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing MessagePack value: unexpected end of input");
|
||||
}
|
||||
catch (const json::out_of_range& e)
|
||||
{
|
||||
threw = true;
|
||||
CHECK(e.id == 408);
|
||||
CHECK(std::string(e.what()).find("excessive") != std::string::npos);
|
||||
}
|
||||
CHECK(threw);
|
||||
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());
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("arrays of various sizes decode to the same value as before the reserve optimization")
|
||||
SECTION("a well-formed deep value is read through the SAX interface")
|
||||
{
|
||||
for (const auto size :
|
||||
{
|
||||
std::size_t{0}, std::size_t{1}, std::size_t{5}, // small
|
||||
std::size_t{16384}, // exactly at the reserve cap
|
||||
std::size_t{20000} // above the reserve cap
|
||||
})
|
||||
{
|
||||
CAPTURE(size)
|
||||
json j = json::array();
|
||||
for (std::size_t i = 0; i < size; ++i)
|
||||
{
|
||||
j.push_back(static_cast<int>(i % 1000));
|
||||
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));
|
||||
}
|
||||
|
||||
const auto packed = json::to_msgpack(j);
|
||||
CHECK(json::from_msgpack(packed) == j);
|
||||
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}}));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("a user-defined SAX consumer is unaffected by the internal DOM reserve optimization")
|
||||
{
|
||||
// the reserve() call is local to json_sax_dom_parser / json_sax_dom_callback_parser;
|
||||
// a custom SAX consumer that does not touch a DOM array sees identical events
|
||||
json j = json::array();
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
j.push_back(i);
|
||||
}
|
||||
const auto packed = json::to_msgpack(j);
|
||||
|
||||
SaxCountdown scp(1000000); // large enough to never trigger an abort
|
||||
CHECK(json::sax_parse(packed, &scp, json::input_format_t::msgpack));
|
||||
}
|
||||
}
|
||||
|
||||
// use this testcase outside [hide] to run it with Valgrind
|
||||
TEST_CASE("single MessagePack roundtrip")
|
||||
{
|
||||
SECTION("sample.json")
|
||||
|
||||
+140
-85
@@ -2149,108 +2149,163 @@ TEST_CASE("UBJSON")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("issue #5405 - array reserve for definite-length UBJSON arrays")
|
||||
TEST_CASE("UBJSON nesting does not consume the call stack")
|
||||
{
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
// this SECTION relies on catching a thrown exception to distinguish
|
||||
// which of two acceptable, bounded rejections a hostile header took;
|
||||
// under JSON_NOEXCEPTION, JSON_THROW never produces a catchable C++
|
||||
// exception (it aborts instead), so this cannot be tested that way here
|
||||
SECTION("a huge claimed length with no element data must not over-allocate")
|
||||
{
|
||||
// optimized form [$type#count: type 'i' (int8), count as a four-byte
|
||||
// 'l' (int32) of 0x7FFFFFFF (2147483647), but no element data at all.
|
||||
// max_size() for a std::vector is far larger than this count, so it
|
||||
// does not reject the header outright; the (capped) reservation must
|
||||
// not attempt to allocate space for billions of elements before the
|
||||
// missing data is detected.
|
||||
// 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 _;
|
||||
const std::vector<uint8_t> input = {'[', '$', 'i', '#', 'l', 0x7F, 0xFF, 0xFF, 0xFF};
|
||||
// On a platform where std::vector<json>::max_size() is smaller than
|
||||
// the claimed count (e.g. 32-bit, where max_size() is bounded by a
|
||||
// 32-bit SIZE_MAX divided by sizeof(json)), the SAX consumer's own
|
||||
// check rejects the header outright (out_of_range.408, with the
|
||||
// claimed count in the message) instead of accepting it and only
|
||||
// finding it short of data once the (capped) reservation looks for
|
||||
// element bytes that were never provided (parse_error.110). Either
|
||||
// is an acceptable, bounded rejection of the hostile header -- the
|
||||
// property under test is that no path attempts to allocate space
|
||||
// for billions of elements.
|
||||
bool threw = false;
|
||||
try
|
||||
{
|
||||
_ = json::from_ubjson(input);
|
||||
}
|
||||
catch (const json::parse_error& e)
|
||||
{
|
||||
threw = true;
|
||||
CHECK(e.id == 110);
|
||||
CHECK(std::string(e.what()) == "[json.exception.parse_error.110] parse error at byte 10: syntax error while parsing UBJSON number: unexpected end of input");
|
||||
}
|
||||
catch (const json::out_of_range& e)
|
||||
{
|
||||
threw = true;
|
||||
CHECK(e.id == 408);
|
||||
CHECK(std::string(e.what()).find("excessive array size") != std::string::npos);
|
||||
}
|
||||
CHECK(threw);
|
||||
|
||||
// json_sax_dom_parser::start_array()'s max_size() check (unlike the
|
||||
// scanner's own parse_error path) throws unconditionally via
|
||||
// JSON_THROW rather than going through sax->parse_error(), so it is
|
||||
// not gated by allow_exceptions=false on a platform where this
|
||||
// header hits that check (e.g. 32-bit, see above) -- allow either
|
||||
// a discarded result or the same out_of_range it throws with
|
||||
// exceptions enabled.
|
||||
try
|
||||
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());
|
||||
}
|
||||
catch (const json::out_of_range& e)
|
||||
{
|
||||
CHECK(e.id == 408);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("arrays of various sizes decode to the same value as before the reserve optimization")
|
||||
SECTION("containers with a size")
|
||||
{
|
||||
for (const auto size :
|
||||
std::vector<uint8_t> input;
|
||||
for (std::size_t i = 0; i < 100000; ++i)
|
||||
{
|
||||
std::size_t{0}, std::size_t{1}, std::size_t{5}, // small
|
||||
std::size_t{16384}, // exactly at the reserve cap
|
||||
std::size_t{20000} // above the reserve cap
|
||||
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")
|
||||
{
|
||||
// An element of type 'Z', 'T' or 'F' is encoded by its marker alone, so an
|
||||
// optimized array of one of those has no payload and the declared count is
|
||||
// the only thing deciding how much is allocated. Ten bytes used to produce
|
||||
// billions of values (#2793); every other type costs at least one byte per
|
||||
// element and is bounded by the end of the input.
|
||||
json _;
|
||||
|
||||
SECTION("an excessive count is rejected")
|
||||
{
|
||||
// 'l' is a big-endian int32: 0x7FFFFFFF elements, about 34 GB of value
|
||||
for (const auto marker :
|
||||
{'Z', 'T', 'F'
|
||||
})
|
||||
{
|
||||
CAPTURE(size)
|
||||
json j = json::array();
|
||||
for (std::size_t i = 0; i < size; ++i)
|
||||
const std::vector<uint8_t> input = {'[', '$', static_cast<uint8_t>(marker), '#', 'l', 0x7F, 0xFF, 0xFF, 0xFF};
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(input), "[json.exception.out_of_range.408] syntax error while parsing UBJSON size: excessive array size", json::out_of_range&);
|
||||
CHECK(json::from_ubjson(input, true, false).is_discarded());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("ordinary counts are unaffected")
|
||||
{
|
||||
j.push_back(static_cast<int>(i % 1000));
|
||||
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'Z', '#', 'i', 3})) == json({nullptr, nullptr, nullptr}));
|
||||
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'T', '#', 'i', 2})) == json({true, true}));
|
||||
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'F', '#', 'i', 2})) == json({false, false}));
|
||||
// 'N' is a no-op rather than a value, and still yields an empty array
|
||||
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'N', '#', 'i', 2})) == json::array());
|
||||
}
|
||||
|
||||
// exercise both the plain and the optimized [$type#count encoding
|
||||
const auto packed_plain = json::to_ubjson(j);
|
||||
CHECK(json::from_ubjson(packed_plain) == j);
|
||||
|
||||
const auto packed_optimized = json::to_ubjson(j, true, true);
|
||||
CHECK(json::from_ubjson(packed_optimized) == j);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("a user-defined SAX consumer is unaffected by the internal DOM reserve optimization")
|
||||
SECTION("a type with a payload is unaffected")
|
||||
{
|
||||
// the reserve() call is local to json_sax_dom_parser / json_sax_dom_callback_parser;
|
||||
// a custom SAX consumer that does not touch a DOM array sees identical events
|
||||
json j = json::array();
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
j.push_back(i);
|
||||
// the same count for 'U' is bounded by the end of the input instead
|
||||
const std::vector<uint8_t> input = {'[', '$', 'U', '#', 'l', 0x7F, 0xFF, 0xFF, 0xFF};
|
||||
CHECK_THROWS_AS(_ = json::from_ubjson(input), json::parse_error&);
|
||||
}
|
||||
const auto packed = json::to_ubjson(j, true, true);
|
||||
|
||||
SaxCountdown scp(1000000); // large enough to never trigger an abort
|
||||
CHECK(json::sax_parse(packed, &scp, json::input_format_t::ubjson));
|
||||
SECTION("the writer stays within what the reader accepts")
|
||||
{
|
||||
// below the limit the optimized form is used and is tiny; above it the
|
||||
// writer falls back so that the result can still be read back
|
||||
json const at_limit(1048576, nullptr);
|
||||
const auto v_at_limit = json::to_ubjson(at_limit, true, true);
|
||||
CHECK(v_at_limit.size() == 9);
|
||||
CHECK(v_at_limit.at(1) == '$');
|
||||
CHECK(json::from_ubjson(v_at_limit) == at_limit);
|
||||
|
||||
json const above_limit(1048577, nullptr);
|
||||
const auto v_above_limit = json::to_ubjson(above_limit, true, true);
|
||||
CHECK(v_above_limit.at(1) != '$');
|
||||
CHECK(json::from_ubjson(v_above_limit) == above_limit);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user