mirror of
https://github.com/nlohmann/json.git
synced 2026-09-07 00:37:58 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e3393b45c | ||
|
|
0236475eef |
@@ -69,12 +69,6 @@ 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
|
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.
|
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"
|
!!! info "Binary values"
|
||||||
|
|
||||||
If the JSON data contains the binary type, the value stored is a list of integers, as suggested by the UBJSON
|
If the JSON data contains the binary type, the value stored is a list of integers, as suggested by the UBJSON
|
||||||
|
|||||||
@@ -868,12 +868,6 @@ 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),
|
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).
|
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"
|
!!! failure "Example messages"
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -885,9 +879,6 @@ so any value it produces can still be read back.
|
|||||||
```
|
```
|
||||||
[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 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
|
### json.exception.out_of_range.409
|
||||||
|
|
||||||
|
|||||||
@@ -58,26 +58,6 @@ inline bool little_endianness(int num = 1) noexcept
|
|||||||
return *reinterpret_cast<char*>(&num) == 1;
|
return *reinterpret_cast<char*>(&num) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief largest element count accepted for a UBJSON container of a valueless type
|
|
||||||
|
|
||||||
An element of type 'Z' (null), 'T' (true) or 'F' (false) is encoded by its
|
|
||||||
type marker alone, so an optimized container of one of those types has no
|
|
||||||
payload at all and its declared count is the only thing that decides how much
|
|
||||||
is allocated: `[$Z#L` followed by a large count turns some ten bytes of input
|
|
||||||
into that many values (see #2793, which reports 35 GB and 150 seconds). Every
|
|
||||||
other type costs at least one byte per element and is bounded by the end of
|
|
||||||
the input.
|
|
||||||
|
|
||||||
This is a sanity bound rather than a security boundary, and it is far above
|
|
||||||
any container met in practice. @ref binary_writer falls back to the
|
|
||||||
unoptimized encoding for longer containers, so that a value serialized by
|
|
||||||
this library can always be read back.
|
|
||||||
|
|
||||||
@sa https://github.com/nlohmann/json/issues/2793
|
|
||||||
*/
|
|
||||||
JSON_INLINE_VARIABLE constexpr std::size_t max_valueless_container_size = 1 << 20;
|
|
||||||
|
|
||||||
///////////////////
|
///////////////////
|
||||||
// binary reader //
|
// binary reader //
|
||||||
///////////////////
|
///////////////////
|
||||||
@@ -130,7 +110,6 @@ class binary_reader
|
|||||||
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
|
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
|
||||||
{
|
{
|
||||||
sax = sax_;
|
sax = sax_;
|
||||||
container_stack.clear();
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
switch (format)
|
switch (format)
|
||||||
@@ -180,57 +159,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
////////////////////////
|
|
||||||
// nested containers //
|
|
||||||
////////////////////////
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief a container that has been opened and not closed yet
|
|
||||||
|
|
||||||
The binary readers do not call themselves once per nesting level. Like
|
|
||||||
@ref parser::sax_parse_internal, which does the same for JSON text, they
|
|
||||||
keep the containers they are inside of on a heap-allocated stack, so that
|
|
||||||
the native call stack does not grow with the nesting depth of the input
|
|
||||||
and a deeply nested value is bounded by memory rather than by the stack
|
|
||||||
(see #5104).
|
|
||||||
|
|
||||||
The members are ordered widest first: frames are stored in a vector, and
|
|
||||||
declaring the `bool` first would pad the struct out for no reason.
|
|
||||||
*/
|
|
||||||
struct container_frame
|
|
||||||
{
|
|
||||||
/// number of elements that have not been read yet
|
|
||||||
std::size_t remaining = 0;
|
|
||||||
/// whether to close this container with end_object() or end_array()
|
|
||||||
bool is_object = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief open a nested array or object
|
|
||||||
|
|
||||||
Emits the SAX start event and records the container. This is the only
|
|
||||||
place the binary readers start a container, so a check that rejects one
|
|
||||||
can be made here and is then guaranteed to run before the start event.
|
|
||||||
|
|
||||||
@param[in] is_object whether an object (true) or an array (false) begins
|
|
||||||
@param[in] len number of elements the container declares
|
|
||||||
|
|
||||||
@return whether the SAX parser accepted the start event
|
|
||||||
*/
|
|
||||||
bool enter_container(const bool is_object, const std::size_t len)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->start_object(len) : !sax->start_array(len)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
container_frame frame;
|
|
||||||
frame.remaining = len;
|
|
||||||
frame.is_object = is_object;
|
|
||||||
container_stack.push_back(frame);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//////////
|
//////////
|
||||||
// BSON //
|
// BSON //
|
||||||
//////////
|
//////////
|
||||||
@@ -563,12 +491,9 @@ class binary_reader
|
|||||||
|
|
||||||
@return whether a valid CBOR value was passed to the SAX parser
|
@return whether a valid CBOR value was passed to the SAX parser
|
||||||
*/
|
*/
|
||||||
bool parse_cbor_value(const bool get_char,
|
bool parse_cbor_internal(const bool get_char,
|
||||||
const cbor_tag_handler_t tag_handler,
|
const cbor_tag_handler_t tag_handler)
|
||||||
bool& tag_pending)
|
|
||||||
{
|
{
|
||||||
tag_pending = false;
|
|
||||||
|
|
||||||
switch (get_char ? get() : current)
|
switch (get_char ? get() : current)
|
||||||
{
|
{
|
||||||
// EOF
|
// EOF
|
||||||
@@ -760,37 +685,37 @@ class binary_reader
|
|||||||
case 0x95:
|
case 0x95:
|
||||||
case 0x96:
|
case 0x96:
|
||||||
case 0x97:
|
case 0x97:
|
||||||
return enter_container(/*is_object*/false,
|
return get_cbor_array(
|
||||||
conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
|
conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
|
||||||
|
|
||||||
case 0x98: // array (one-byte uint8_t for n follows)
|
case 0x98: // array (one-byte uint8_t for n follows)
|
||||||
{
|
{
|
||||||
std::uint8_t len{};
|
std::uint8_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && enter_container(/*is_object*/false, static_cast<std::size_t>(len));
|
return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x99: // array (two-byte uint16_t for n follow)
|
case 0x99: // array (two-byte uint16_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && enter_container(/*is_object*/false, static_cast<std::size_t>(len));
|
return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9A: // array (four-byte uint32_t for n follow)
|
case 0x9A: // array (four-byte uint32_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && enter_container(/*is_object*/false, size);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && get_cbor_array(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9B: // array (eight-byte uint64_t for n follow)
|
case 0x9B: // array (eight-byte uint64_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint64_t len{};
|
std::uint64_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && enter_container(/*is_object*/false, size);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && get_cbor_array(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9F: // array (indefinite length)
|
case 0x9F: // array (indefinite length)
|
||||||
return enter_container(/*is_object*/false, detail::unknown_size());
|
return get_cbor_array(detail::unknown_size(), tag_handler);
|
||||||
|
|
||||||
// map (0x00..0x17 pairs of data items follow)
|
// map (0x00..0x17 pairs of data items follow)
|
||||||
case 0xA0:
|
case 0xA0:
|
||||||
@@ -817,36 +742,36 @@ class binary_reader
|
|||||||
case 0xB5:
|
case 0xB5:
|
||||||
case 0xB6:
|
case 0xB6:
|
||||||
case 0xB7:
|
case 0xB7:
|
||||||
return enter_container(/*is_object*/true, conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
|
return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
|
||||||
|
|
||||||
case 0xB8: // map (one-byte uint8_t for n follows)
|
case 0xB8: // map (one-byte uint8_t for n follows)
|
||||||
{
|
{
|
||||||
std::uint8_t len{};
|
std::uint8_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && enter_container(/*is_object*/true, static_cast<std::size_t>(len));
|
return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xB9: // map (two-byte uint16_t for n follow)
|
case 0xB9: // map (two-byte uint16_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && enter_container(/*is_object*/true, static_cast<std::size_t>(len));
|
return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBA: // map (four-byte uint32_t for n follow)
|
case 0xBA: // map (four-byte uint32_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && enter_container(/*is_object*/true, size);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && get_cbor_object(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBB: // map (eight-byte uint64_t for n follow)
|
case 0xBB: // map (eight-byte uint64_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint64_t len{};
|
std::uint64_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && enter_container(/*is_object*/true, size);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && get_cbor_object(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBF: // map (indefinite length)
|
case 0xBF: // map (indefinite length)
|
||||||
return enter_container(/*is_object*/true, detail::unknown_size());
|
return get_cbor_object(detail::unknown_size(), tag_handler);
|
||||||
|
|
||||||
case 0xC0: // tagged item
|
case 0xC0: // tagged item
|
||||||
case 0xC1:
|
case 0xC1:
|
||||||
@@ -930,10 +855,7 @@ class binary_reader
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// the tagged value follows; it is read by the loop in
|
return parse_cbor_internal(true, tag_handler);
|
||||||
// parse_cbor_internal() rather than by recursing here
|
|
||||||
tag_pending = true;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case cbor_tag_handler_t::store:
|
case cbor_tag_handler_t::store:
|
||||||
@@ -983,11 +905,7 @@ class binary_reader
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
return parse_cbor_internal(true, tag_handler);
|
||||||
// as above, the tagged value is read by the caller
|
|
||||||
tag_pending = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
get();
|
get();
|
||||||
return get_cbor_binary(b) && sax->binary(b);
|
return get_cbor_binary(b) && sax->binary(b);
|
||||||
@@ -1078,21 +996,23 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief reads a definite-length CBOR string
|
@brief reads a CBOR string
|
||||||
|
|
||||||
Reads everything @ref get_cbor_string accepts except the indefinite-length
|
This function first reads starting bytes to determine the expected
|
||||||
form, which that function handles itself. The bytes are appended to @a
|
string length and then copies this number of bytes into a string.
|
||||||
result, so consecutive chunks of an indefinite-length string can be read
|
Additionally, CBOR's strings with indefinite lengths are supported.
|
||||||
into the same string.
|
|
||||||
|
|
||||||
@param[out] result string the bytes are appended to
|
@param[out] result created string
|
||||||
|
|
||||||
@return whether string creation completed
|
@return whether string creation completed
|
||||||
|
|
||||||
@pre @a current is not EOF
|
|
||||||
*/
|
*/
|
||||||
bool get_cbor_string_chunk(string_t& result)
|
bool get_cbor_string(string_t& result)
|
||||||
{
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
// UTF-8 string (0x00..0x17 bytes follow)
|
// UTF-8 string (0x00..0x17 bytes follow)
|
||||||
@@ -1148,6 +1068,20 @@ class binary_reader
|
|||||||
return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
|
return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 0x7F: // UTF-8 string (indefinite length)
|
||||||
|
{
|
||||||
|
while (get() != 0xFF)
|
||||||
|
{
|
||||||
|
string_t chunk;
|
||||||
|
if (!get_cbor_string(chunk))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
result.append(chunk);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
auto last_token = get_token_string();
|
auto last_token = get_token_string();
|
||||||
@@ -1158,82 +1092,23 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief reads a CBOR string
|
@brief reads a CBOR byte array
|
||||||
|
|
||||||
This function first reads starting bytes to determine the expected
|
This function first reads starting bytes to determine the expected
|
||||||
string length and then copies this number of bytes into a string.
|
byte array length and then copies this number of bytes into the byte array.
|
||||||
Additionally, CBOR's strings with indefinite lengths are supported.
|
Additionally, CBOR's byte arrays with indefinite lengths are supported.
|
||||||
|
|
||||||
@param[out] result created string
|
@param[out] result created byte array
|
||||||
|
|
||||||
@return whether string creation completed
|
|
||||||
*/
|
|
||||||
bool get_cbor_string(string_t& result)
|
|
||||||
{
|
|
||||||
// number of indefinite-length strings that have been opened and not
|
|
||||||
// closed yet. RFC 8949, Section 3.2.3 does not permit nesting them,
|
|
||||||
// but this reader has always accepted it, so the open levels are
|
|
||||||
// counted instead of recursed through, which overflowed the stack for
|
|
||||||
// an input of repeated 0x7F bytes (see #5104). Every chunk is appended
|
|
||||||
// to the same result, so no per-level state is needed.
|
|
||||||
std::size_t open = 0;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == 0x7F) // UTF-8 string (indefinite length)
|
|
||||||
{
|
|
||||||
++open;
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// a break marker closes the innermost indefinite-length string;
|
|
||||||
// outside of one it is not a string and falls through to the error
|
|
||||||
if (open != 0 && current == 0xFF)
|
|
||||||
{
|
|
||||||
if (--open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string_chunk(result)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief reads a definite-length CBOR byte array
|
|
||||||
|
|
||||||
Reads everything @ref get_cbor_binary accepts except the indefinite-length
|
|
||||||
form, which that function handles itself. The bytes are appended to @a
|
|
||||||
result, so consecutive chunks of an indefinite-length byte array can be
|
|
||||||
read into the same byte array.
|
|
||||||
|
|
||||||
@param[out] result byte array the bytes are appended to
|
|
||||||
|
|
||||||
@return whether byte array creation completed
|
@return whether byte array creation completed
|
||||||
|
|
||||||
@pre @a current is not EOF
|
|
||||||
*/
|
*/
|
||||||
bool get_cbor_binary_chunk(binary_t& result)
|
bool get_cbor_binary(binary_t& result)
|
||||||
{
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
// Binary data (0x00..0x17 bytes follow)
|
// Binary data (0x00..0x17 bytes follow)
|
||||||
@@ -1293,6 +1168,20 @@ class binary_reader
|
|||||||
get_binary(input_format_t::cbor, len, result);
|
get_binary(input_format_t::cbor, len, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 0x5F: // Binary data (indefinite length)
|
||||||
|
{
|
||||||
|
while (get() != 0xFF)
|
||||||
|
{
|
||||||
|
binary_t chunk;
|
||||||
|
if (!get_cbor_binary(chunk))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
result.insert(result.end(), chunk.begin(), chunk.end());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
auto last_token = get_token_string();
|
auto last_token = get_token_string();
|
||||||
@@ -1302,63 +1191,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief reads a CBOR byte array
|
|
||||||
|
|
||||||
This function first reads starting bytes to determine the expected
|
|
||||||
byte array length and then copies this number of bytes into the byte array.
|
|
||||||
Additionally, CBOR's byte arrays with indefinite lengths are supported.
|
|
||||||
|
|
||||||
@param[out] result created byte array
|
|
||||||
|
|
||||||
@return whether byte array creation completed
|
|
||||||
*/
|
|
||||||
bool get_cbor_binary(binary_t& result)
|
|
||||||
{
|
|
||||||
// the open indefinite-length byte arrays are counted rather than
|
|
||||||
// recursed through, for the reason given in @ref get_cbor_string
|
|
||||||
std::size_t open = 0;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == 0x5F) // Binary data (indefinite length)
|
|
||||||
{
|
|
||||||
++open;
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// a break marker closes the innermost indefinite-length byte
|
|
||||||
// array; outside of one it falls through to the error below
|
|
||||||
if (open != 0 && current == 0xFF)
|
|
||||||
{
|
|
||||||
if (--open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_binary_chunk(result)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief narrow a definite CBOR array/map length to std::size_t
|
@brief narrow a definite CBOR array/map length to std::size_t
|
||||||
|
|
||||||
@@ -1385,110 +1217,96 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief read a CBOR value and everything nested inside it
|
@param[in] len the length of the array or detail::unknown_size() for an
|
||||||
|
array of indefinite size
|
||||||
Reads values until the one that was begun here is complete, resuming the
|
|
||||||
enclosing container after each element, so that the nesting depth of the
|
|
||||||
input costs heap rather than native stack (see #5104).
|
|
||||||
|
|
||||||
@param[in] get_char whether a new character should be retrieved from the
|
|
||||||
input (true) or whether the last read character
|
|
||||||
@a current should be considered instead
|
|
||||||
@param[in] tag_handler how CBOR tags should be treated
|
@param[in] tag_handler how CBOR tags should be treated
|
||||||
|
@return whether array creation completed
|
||||||
@return whether reading the value succeeded
|
|
||||||
*/
|
*/
|
||||||
bool parse_cbor_internal(const bool get_char,
|
bool get_cbor_array(const std::size_t len,
|
||||||
const cbor_tag_handler_t tag_handler)
|
const cbor_tag_handler_t tag_handler)
|
||||||
{
|
{
|
||||||
// whether the next value starts at a fresh byte or at the one already
|
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
|
||||||
// read into `current`
|
|
||||||
bool fetch = get_char;
|
|
||||||
|
|
||||||
// the key currently being read; hoisted out of the loop so that its
|
|
||||||
// capacity is reused across elements and across nesting levels
|
|
||||||
string_t key;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
if (!container_stack.empty())
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len != detail::unknown_size())
|
||||||
|
{
|
||||||
|
for (std::size_t i = 0; i < len; ++i)
|
||||||
{
|
{
|
||||||
// the reference is not held across parse_cbor_value() below,
|
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
||||||
// which can push onto the stack and reallocate it
|
|
||||||
container_frame& top = container_stack.back();
|
|
||||||
bool at_end;
|
|
||||||
|
|
||||||
if (top.remaining != npos)
|
|
||||||
{
|
{
|
||||||
// definite length: the container ends once its elements
|
return false;
|
||||||
// have been read
|
|
||||||
at_end = (top.remaining == 0);
|
|
||||||
if (!at_end)
|
|
||||||
{
|
|
||||||
// claim the element about to be read
|
|
||||||
--top.remaining;
|
|
||||||
if (top.is_object)
|
|
||||||
{
|
|
||||||
get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fetch = true;
|
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (get() != 0xFF)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
|
||||||
{
|
{
|
||||||
// indefinite length: the container ends at a break marker.
|
return false;
|
||||||
// Testing for it consumes a byte, which is the first byte
|
|
||||||
// of the next element when it is not one.
|
|
||||||
at_end = (get() == 0xFF);
|
|
||||||
fetch = top.is_object;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (at_end)
|
return sax->end_array();
|
||||||
{
|
}
|
||||||
const bool is_object = top.is_object;
|
|
||||||
container_stack.pop_back();
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->end_object() : !sax->end_array()))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// the value begun here is complete once its container is
|
|
||||||
if (container_stack.empty())
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (top.is_object)
|
/*!
|
||||||
|
@param[in] len the length of the object or detail::unknown_size() for an
|
||||||
|
object of indefinite size
|
||||||
|
@param[in] tag_handler how CBOR tags should be treated
|
||||||
|
@return whether object creation completed
|
||||||
|
*/
|
||||||
|
bool get_cbor_object(const std::size_t len,
|
||||||
|
const cbor_tag_handler_t tag_handler)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len != 0)
|
||||||
|
{
|
||||||
|
string_t key;
|
||||||
|
if (len != detail::unknown_size())
|
||||||
|
{
|
||||||
|
for (std::size_t i = 0; i < len; ++i)
|
||||||
{
|
{
|
||||||
key.clear();
|
get();
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
fetch = true;
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
key.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// a tag is not a value of its own: read on until the tagged value
|
|
||||||
bool tag_pending;
|
|
||||||
do
|
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_value(fetch, tag_handler, tag_pending)))
|
while (get() != 0xFF)
|
||||||
{
|
{
|
||||||
return false;
|
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
||||||
}
|
{
|
||||||
fetch = true;
|
return false;
|
||||||
}
|
}
|
||||||
while (tag_pending);
|
|
||||||
|
|
||||||
// a value that opened a container left it on the stack; one that
|
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
||||||
// did not, and that was not inside a container, was the whole value
|
{
|
||||||
if (container_stack.empty())
|
return false;
|
||||||
{
|
}
|
||||||
return true;
|
key.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return sax->end_object();
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////
|
/////////////
|
||||||
@@ -1498,17 +1316,7 @@ class binary_reader
|
|||||||
/*!
|
/*!
|
||||||
@return whether a valid MessagePack value was passed to the SAX parser
|
@return whether a valid MessagePack value was passed to the SAX parser
|
||||||
*/
|
*/
|
||||||
/*!
|
bool parse_msgpack_internal()
|
||||||
@brief read one MessagePack value
|
|
||||||
|
|
||||||
Reads a single value and passes it to the SAX parser. A value that begins
|
|
||||||
a container is not read to its end: the container is opened with
|
|
||||||
@ref enter_container and its elements are read by
|
|
||||||
@ref parse_msgpack_internal, so that nesting does not consume native stack.
|
|
||||||
|
|
||||||
@return whether reading the value succeeded
|
|
||||||
*/
|
|
||||||
bool parse_msgpack_value()
|
|
||||||
{
|
{
|
||||||
switch (get())
|
switch (get())
|
||||||
{
|
{
|
||||||
@@ -1664,7 +1472,7 @@ class binary_reader
|
|||||||
case 0x8D:
|
case 0x8D:
|
||||||
case 0x8E:
|
case 0x8E:
|
||||||
case 0x8F:
|
case 0x8F:
|
||||||
return enter_container(/*is_object*/true, conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
||||||
|
|
||||||
// fixarray
|
// fixarray
|
||||||
case 0x90:
|
case 0x90:
|
||||||
@@ -1683,7 +1491,7 @@ class binary_reader
|
|||||||
case 0x9D:
|
case 0x9D:
|
||||||
case 0x9E:
|
case 0x9E:
|
||||||
case 0x9F:
|
case 0x9F:
|
||||||
return enter_container(/*is_object*/false, conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
||||||
|
|
||||||
// fixstr
|
// fixstr
|
||||||
case 0xA0:
|
case 0xA0:
|
||||||
@@ -1814,25 +1622,25 @@ class binary_reader
|
|||||||
case 0xDC: // array 16
|
case 0xDC: // array 16
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && enter_container(/*is_object*/false, static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xDD: // array 32
|
case 0xDD: // array 32
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && enter_container(/*is_object*/false, conditional_static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xDE: // map 16
|
case 0xDE: // map 16
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && enter_container(/*is_object*/true, static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xDF: // map 32
|
case 0xDF: // map 32
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && enter_container(/*is_object*/true, conditional_static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
// negative fixint
|
// negative fixint
|
||||||
@@ -2080,69 +1888,55 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief read a MessagePack value and everything nested inside it
|
@param[in] len the length of the array
|
||||||
|
@return whether array creation completed
|
||||||
Reads values until the one that was begun here is complete, resuming the
|
|
||||||
enclosing container each time an element ends, so that the nesting depth
|
|
||||||
of the input costs heap rather than native stack (see #5104).
|
|
||||||
|
|
||||||
@return whether reading the value succeeded
|
|
||||||
*/
|
*/
|
||||||
bool parse_msgpack_internal()
|
bool get_msgpack_array(const std::size_t len)
|
||||||
{
|
{
|
||||||
// the key currently being read; hoisted out of the loop so that its
|
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
|
||||||
// capacity is reused across elements and across nesting levels
|
|
||||||
string_t key;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
if (!container_stack.empty())
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < len; ++i)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
|
||||||
{
|
{
|
||||||
// copied out before anything can push onto the stack and
|
return false;
|
||||||
// invalidate a reference into it
|
|
||||||
const bool is_object = container_stack.back().is_object;
|
|
||||||
|
|
||||||
if (container_stack.back().remaining == 0)
|
|
||||||
{
|
|
||||||
container_stack.pop_back();
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->end_object() : !sax->end_array()))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// the value begun here is complete once its container is
|
|
||||||
if (container_stack.empty())
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// claim the element about to be read
|
|
||||||
--container_stack.back().remaining;
|
|
||||||
|
|
||||||
if (is_object)
|
|
||||||
{
|
|
||||||
get();
|
|
||||||
key.clear();
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_value()))
|
return sax->end_array();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@param[in] len the length of the object
|
||||||
|
@return whether object creation completed
|
||||||
|
*/
|
||||||
|
bool get_msgpack_object(const std::size_t len)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
string_t key;
|
||||||
|
for (std::size_t i = 0; i < len; ++i)
|
||||||
|
{
|
||||||
|
get();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// a value that opened a container left it on the stack; one that
|
if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
|
||||||
// did not, and that was not inside a container, was the whole value
|
|
||||||
if (container_stack.empty())
|
|
||||||
{
|
{
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
key.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return sax->end_object();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////
|
////////////
|
||||||
@@ -2597,12 +2391,7 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
result.first = npos; // size
|
result.first = npos; // size
|
||||||
result.second = 0; // type
|
result.second = 0; // type
|
||||||
// seed the flag with the caller's context: inside an ndarray dimension
|
bool is_ndarray = false;
|
||||||
// vector another ndarray is not allowed, and get_ubjson_size_value()
|
|
||||||
// rejects it up front instead of reading it and reporting afterwards.
|
|
||||||
// Seeding it with `false` made every '#' of a "[#[#[..." chain descend
|
|
||||||
// another level, which overflowed the stack (see #5104).
|
|
||||||
bool is_ndarray = inside_ndarray;
|
|
||||||
|
|
||||||
get_ignore_noop();
|
get_ignore_noop();
|
||||||
|
|
||||||
@@ -2635,11 +2424,13 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
||||||
// an ndarray was read here only if the flag flipped; when it was
|
if (input_format == input_format_t::bjdata && is_ndarray)
|
||||||
// seeded true, get_ubjson_size_value() already rejected the nested
|
|
||||||
// dimension vector
|
|
||||||
if (input_format == input_format_t::bjdata && is_ndarray && !inside_ndarray)
|
|
||||||
{
|
{
|
||||||
|
if (inside_ndarray)
|
||||||
|
{
|
||||||
|
return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
|
||||||
|
exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
|
||||||
|
}
|
||||||
result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
|
result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
|
||||||
}
|
}
|
||||||
return is_error;
|
return is_error;
|
||||||
@@ -2648,7 +2439,7 @@ class binary_reader
|
|||||||
if (current == '#')
|
if (current == '#')
|
||||||
{
|
{
|
||||||
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
||||||
if (input_format == input_format_t::bjdata && is_ndarray && !inside_ndarray)
|
if (input_format == input_format_t::bjdata && is_ndarray)
|
||||||
{
|
{
|
||||||
return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
|
return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
|
||||||
exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
|
exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
|
||||||
@@ -2919,17 +2710,6 @@ class binary_reader
|
|||||||
|
|
||||||
if (size_and_type.first != npos)
|
if (size_and_type.first != npos)
|
||||||
{
|
{
|
||||||
// reading an element of a valueless type consumes no input, so the
|
|
||||||
// declared count alone decides how much is allocated; the check is
|
|
||||||
// made before the start event so that no container is opened that
|
|
||||||
// is then abandoned. See @ref max_valueless_container_size.
|
|
||||||
if (JSON_HEDLEY_UNLIKELY((size_and_type.second == 'Z' || size_and_type.second == 'T' || size_and_type.second == 'F')
|
|
||||||
&& size_and_type.first > max_valueless_container_size))
|
|
||||||
{
|
|
||||||
return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
|
|
||||||
exception_message(input_format, "excessive array size", "size"), nullptr));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
|
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -3447,9 +3227,6 @@ class binary_reader
|
|||||||
/// the SAX parser
|
/// the SAX parser
|
||||||
json_sax_t* sax = nullptr;
|
json_sax_t* sax = nullptr;
|
||||||
|
|
||||||
/// the containers that have been opened and not closed yet; see @ref container_frame
|
|
||||||
std::vector<container_frame> container_stack{};
|
|
||||||
|
|
||||||
// excluded markers in bjdata optimized type
|
// excluded markers in bjdata optimized type
|
||||||
#define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
|
#define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
|
||||||
make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
|
make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
|
||||||
|
|||||||
@@ -826,17 +826,7 @@ class binary_writer
|
|||||||
|
|
||||||
std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
|
std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
|
||||||
|
|
||||||
// an optimized array of a valueless type carries no payload, so a
|
if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
|
||||||
// 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;
|
prefix_required = false;
|
||||||
oa->write_character(to_char_type('$'));
|
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));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? 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)
|
/// @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));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
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);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// 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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in MessagePack format
|
/// @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));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? 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)
|
/// @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));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
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);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// 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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in UBJSON format
|
/// @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));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? 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)
|
/// @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));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
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);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// 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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BJData format
|
/// @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));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? 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)
|
/// @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));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BSON format
|
/// @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));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? 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)
|
/// @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));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
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);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// 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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
|
|||||||
+199
-432
@@ -10745,26 +10745,6 @@ inline bool little_endianness(int num = 1) noexcept
|
|||||||
return *reinterpret_cast<char*>(&num) == 1;
|
return *reinterpret_cast<char*>(&num) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief largest element count accepted for a UBJSON container of a valueless type
|
|
||||||
|
|
||||||
An element of type 'Z' (null), 'T' (true) or 'F' (false) is encoded by its
|
|
||||||
type marker alone, so an optimized container of one of those types has no
|
|
||||||
payload at all and its declared count is the only thing that decides how much
|
|
||||||
is allocated: `[$Z#L` followed by a large count turns some ten bytes of input
|
|
||||||
into that many values (see #2793, which reports 35 GB and 150 seconds). Every
|
|
||||||
other type costs at least one byte per element and is bounded by the end of
|
|
||||||
the input.
|
|
||||||
|
|
||||||
This is a sanity bound rather than a security boundary, and it is far above
|
|
||||||
any container met in practice. @ref binary_writer falls back to the
|
|
||||||
unoptimized encoding for longer containers, so that a value serialized by
|
|
||||||
this library can always be read back.
|
|
||||||
|
|
||||||
@sa https://github.com/nlohmann/json/issues/2793
|
|
||||||
*/
|
|
||||||
JSON_INLINE_VARIABLE constexpr std::size_t max_valueless_container_size = 1 << 20;
|
|
||||||
|
|
||||||
///////////////////
|
///////////////////
|
||||||
// binary reader //
|
// binary reader //
|
||||||
///////////////////
|
///////////////////
|
||||||
@@ -10817,7 +10797,6 @@ class binary_reader
|
|||||||
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
|
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
|
||||||
{
|
{
|
||||||
sax = sax_;
|
sax = sax_;
|
||||||
container_stack.clear();
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
switch (format)
|
switch (format)
|
||||||
@@ -10867,57 +10846,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
////////////////////////
|
|
||||||
// nested containers //
|
|
||||||
////////////////////////
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief a container that has been opened and not closed yet
|
|
||||||
|
|
||||||
The binary readers do not call themselves once per nesting level. Like
|
|
||||||
@ref parser::sax_parse_internal, which does the same for JSON text, they
|
|
||||||
keep the containers they are inside of on a heap-allocated stack, so that
|
|
||||||
the native call stack does not grow with the nesting depth of the input
|
|
||||||
and a deeply nested value is bounded by memory rather than by the stack
|
|
||||||
(see #5104).
|
|
||||||
|
|
||||||
The members are ordered widest first: frames are stored in a vector, and
|
|
||||||
declaring the `bool` first would pad the struct out for no reason.
|
|
||||||
*/
|
|
||||||
struct container_frame
|
|
||||||
{
|
|
||||||
/// number of elements that have not been read yet
|
|
||||||
std::size_t remaining = 0;
|
|
||||||
/// whether to close this container with end_object() or end_array()
|
|
||||||
bool is_object = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief open a nested array or object
|
|
||||||
|
|
||||||
Emits the SAX start event and records the container. This is the only
|
|
||||||
place the binary readers start a container, so a check that rejects one
|
|
||||||
can be made here and is then guaranteed to run before the start event.
|
|
||||||
|
|
||||||
@param[in] is_object whether an object (true) or an array (false) begins
|
|
||||||
@param[in] len number of elements the container declares
|
|
||||||
|
|
||||||
@return whether the SAX parser accepted the start event
|
|
||||||
*/
|
|
||||||
bool enter_container(const bool is_object, const std::size_t len)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->start_object(len) : !sax->start_array(len)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
container_frame frame;
|
|
||||||
frame.remaining = len;
|
|
||||||
frame.is_object = is_object;
|
|
||||||
container_stack.push_back(frame);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//////////
|
//////////
|
||||||
// BSON //
|
// BSON //
|
||||||
//////////
|
//////////
|
||||||
@@ -11250,12 +11178,9 @@ class binary_reader
|
|||||||
|
|
||||||
@return whether a valid CBOR value was passed to the SAX parser
|
@return whether a valid CBOR value was passed to the SAX parser
|
||||||
*/
|
*/
|
||||||
bool parse_cbor_value(const bool get_char,
|
bool parse_cbor_internal(const bool get_char,
|
||||||
const cbor_tag_handler_t tag_handler,
|
const cbor_tag_handler_t tag_handler)
|
||||||
bool& tag_pending)
|
|
||||||
{
|
{
|
||||||
tag_pending = false;
|
|
||||||
|
|
||||||
switch (get_char ? get() : current)
|
switch (get_char ? get() : current)
|
||||||
{
|
{
|
||||||
// EOF
|
// EOF
|
||||||
@@ -11447,37 +11372,37 @@ class binary_reader
|
|||||||
case 0x95:
|
case 0x95:
|
||||||
case 0x96:
|
case 0x96:
|
||||||
case 0x97:
|
case 0x97:
|
||||||
return enter_container(/*is_object*/false,
|
return get_cbor_array(
|
||||||
conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
|
conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
|
||||||
|
|
||||||
case 0x98: // array (one-byte uint8_t for n follows)
|
case 0x98: // array (one-byte uint8_t for n follows)
|
||||||
{
|
{
|
||||||
std::uint8_t len{};
|
std::uint8_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && enter_container(/*is_object*/false, static_cast<std::size_t>(len));
|
return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x99: // array (two-byte uint16_t for n follow)
|
case 0x99: // array (two-byte uint16_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && enter_container(/*is_object*/false, static_cast<std::size_t>(len));
|
return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9A: // array (four-byte uint32_t for n follow)
|
case 0x9A: // array (four-byte uint32_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && enter_container(/*is_object*/false, size);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && get_cbor_array(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9B: // array (eight-byte uint64_t for n follow)
|
case 0x9B: // array (eight-byte uint64_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint64_t len{};
|
std::uint64_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && enter_container(/*is_object*/false, size);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && get_cbor_array(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9F: // array (indefinite length)
|
case 0x9F: // array (indefinite length)
|
||||||
return enter_container(/*is_object*/false, detail::unknown_size());
|
return get_cbor_array(detail::unknown_size(), tag_handler);
|
||||||
|
|
||||||
// map (0x00..0x17 pairs of data items follow)
|
// map (0x00..0x17 pairs of data items follow)
|
||||||
case 0xA0:
|
case 0xA0:
|
||||||
@@ -11504,36 +11429,36 @@ class binary_reader
|
|||||||
case 0xB5:
|
case 0xB5:
|
||||||
case 0xB6:
|
case 0xB6:
|
||||||
case 0xB7:
|
case 0xB7:
|
||||||
return enter_container(/*is_object*/true, conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
|
return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
|
||||||
|
|
||||||
case 0xB8: // map (one-byte uint8_t for n follows)
|
case 0xB8: // map (one-byte uint8_t for n follows)
|
||||||
{
|
{
|
||||||
std::uint8_t len{};
|
std::uint8_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && enter_container(/*is_object*/true, static_cast<std::size_t>(len));
|
return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xB9: // map (two-byte uint16_t for n follow)
|
case 0xB9: // map (two-byte uint16_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && enter_container(/*is_object*/true, static_cast<std::size_t>(len));
|
return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBA: // map (four-byte uint32_t for n follow)
|
case 0xBA: // map (four-byte uint32_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && enter_container(/*is_object*/true, size);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && get_cbor_object(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBB: // map (eight-byte uint64_t for n follow)
|
case 0xBB: // map (eight-byte uint64_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint64_t len{};
|
std::uint64_t len{};
|
||||||
std::size_t size{};
|
std::size_t size{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && enter_container(/*is_object*/true, size);
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && get_cbor_object(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBF: // map (indefinite length)
|
case 0xBF: // map (indefinite length)
|
||||||
return enter_container(/*is_object*/true, detail::unknown_size());
|
return get_cbor_object(detail::unknown_size(), tag_handler);
|
||||||
|
|
||||||
case 0xC0: // tagged item
|
case 0xC0: // tagged item
|
||||||
case 0xC1:
|
case 0xC1:
|
||||||
@@ -11617,10 +11542,7 @@ class binary_reader
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// the tagged value follows; it is read by the loop in
|
return parse_cbor_internal(true, tag_handler);
|
||||||
// parse_cbor_internal() rather than by recursing here
|
|
||||||
tag_pending = true;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case cbor_tag_handler_t::store:
|
case cbor_tag_handler_t::store:
|
||||||
@@ -11670,11 +11592,7 @@ class binary_reader
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
return parse_cbor_internal(true, tag_handler);
|
||||||
// as above, the tagged value is read by the caller
|
|
||||||
tag_pending = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
get();
|
get();
|
||||||
return get_cbor_binary(b) && sax->binary(b);
|
return get_cbor_binary(b) && sax->binary(b);
|
||||||
@@ -11765,21 +11683,23 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief reads a definite-length CBOR string
|
@brief reads a CBOR string
|
||||||
|
|
||||||
Reads everything @ref get_cbor_string accepts except the indefinite-length
|
This function first reads starting bytes to determine the expected
|
||||||
form, which that function handles itself. The bytes are appended to @a
|
string length and then copies this number of bytes into a string.
|
||||||
result, so consecutive chunks of an indefinite-length string can be read
|
Additionally, CBOR's strings with indefinite lengths are supported.
|
||||||
into the same string.
|
|
||||||
|
|
||||||
@param[out] result string the bytes are appended to
|
@param[out] result created string
|
||||||
|
|
||||||
@return whether string creation completed
|
@return whether string creation completed
|
||||||
|
|
||||||
@pre @a current is not EOF
|
|
||||||
*/
|
*/
|
||||||
bool get_cbor_string_chunk(string_t& result)
|
bool get_cbor_string(string_t& result)
|
||||||
{
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
// UTF-8 string (0x00..0x17 bytes follow)
|
// UTF-8 string (0x00..0x17 bytes follow)
|
||||||
@@ -11835,6 +11755,20 @@ class binary_reader
|
|||||||
return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
|
return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 0x7F: // UTF-8 string (indefinite length)
|
||||||
|
{
|
||||||
|
while (get() != 0xFF)
|
||||||
|
{
|
||||||
|
string_t chunk;
|
||||||
|
if (!get_cbor_string(chunk))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
result.append(chunk);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
auto last_token = get_token_string();
|
auto last_token = get_token_string();
|
||||||
@@ -11845,82 +11779,23 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief reads a CBOR string
|
@brief reads a CBOR byte array
|
||||||
|
|
||||||
This function first reads starting bytes to determine the expected
|
This function first reads starting bytes to determine the expected
|
||||||
string length and then copies this number of bytes into a string.
|
byte array length and then copies this number of bytes into the byte array.
|
||||||
Additionally, CBOR's strings with indefinite lengths are supported.
|
Additionally, CBOR's byte arrays with indefinite lengths are supported.
|
||||||
|
|
||||||
@param[out] result created string
|
@param[out] result created byte array
|
||||||
|
|
||||||
@return whether string creation completed
|
|
||||||
*/
|
|
||||||
bool get_cbor_string(string_t& result)
|
|
||||||
{
|
|
||||||
// number of indefinite-length strings that have been opened and not
|
|
||||||
// closed yet. RFC 8949, Section 3.2.3 does not permit nesting them,
|
|
||||||
// but this reader has always accepted it, so the open levels are
|
|
||||||
// counted instead of recursed through, which overflowed the stack for
|
|
||||||
// an input of repeated 0x7F bytes (see #5104). Every chunk is appended
|
|
||||||
// to the same result, so no per-level state is needed.
|
|
||||||
std::size_t open = 0;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == 0x7F) // UTF-8 string (indefinite length)
|
|
||||||
{
|
|
||||||
++open;
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// a break marker closes the innermost indefinite-length string;
|
|
||||||
// outside of one it is not a string and falls through to the error
|
|
||||||
if (open != 0 && current == 0xFF)
|
|
||||||
{
|
|
||||||
if (--open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string_chunk(result)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief reads a definite-length CBOR byte array
|
|
||||||
|
|
||||||
Reads everything @ref get_cbor_binary accepts except the indefinite-length
|
|
||||||
form, which that function handles itself. The bytes are appended to @a
|
|
||||||
result, so consecutive chunks of an indefinite-length byte array can be
|
|
||||||
read into the same byte array.
|
|
||||||
|
|
||||||
@param[out] result byte array the bytes are appended to
|
|
||||||
|
|
||||||
@return whether byte array creation completed
|
@return whether byte array creation completed
|
||||||
|
|
||||||
@pre @a current is not EOF
|
|
||||||
*/
|
*/
|
||||||
bool get_cbor_binary_chunk(binary_t& result)
|
bool get_cbor_binary(binary_t& result)
|
||||||
{
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
// Binary data (0x00..0x17 bytes follow)
|
// Binary data (0x00..0x17 bytes follow)
|
||||||
@@ -11980,6 +11855,20 @@ class binary_reader
|
|||||||
get_binary(input_format_t::cbor, len, result);
|
get_binary(input_format_t::cbor, len, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 0x5F: // Binary data (indefinite length)
|
||||||
|
{
|
||||||
|
while (get() != 0xFF)
|
||||||
|
{
|
||||||
|
binary_t chunk;
|
||||||
|
if (!get_cbor_binary(chunk))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
result.insert(result.end(), chunk.begin(), chunk.end());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
auto last_token = get_token_string();
|
auto last_token = get_token_string();
|
||||||
@@ -11989,63 +11878,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief reads a CBOR byte array
|
|
||||||
|
|
||||||
This function first reads starting bytes to determine the expected
|
|
||||||
byte array length and then copies this number of bytes into the byte array.
|
|
||||||
Additionally, CBOR's byte arrays with indefinite lengths are supported.
|
|
||||||
|
|
||||||
@param[out] result created byte array
|
|
||||||
|
|
||||||
@return whether byte array creation completed
|
|
||||||
*/
|
|
||||||
bool get_cbor_binary(binary_t& result)
|
|
||||||
{
|
|
||||||
// the open indefinite-length byte arrays are counted rather than
|
|
||||||
// recursed through, for the reason given in @ref get_cbor_string
|
|
||||||
std::size_t open = 0;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == 0x5F) // Binary data (indefinite length)
|
|
||||||
{
|
|
||||||
++open;
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// a break marker closes the innermost indefinite-length byte
|
|
||||||
// array; outside of one it falls through to the error below
|
|
||||||
if (open != 0 && current == 0xFF)
|
|
||||||
{
|
|
||||||
if (--open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_binary_chunk(result)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief narrow a definite CBOR array/map length to std::size_t
|
@brief narrow a definite CBOR array/map length to std::size_t
|
||||||
|
|
||||||
@@ -12072,110 +11904,96 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief read a CBOR value and everything nested inside it
|
@param[in] len the length of the array or detail::unknown_size() for an
|
||||||
|
array of indefinite size
|
||||||
Reads values until the one that was begun here is complete, resuming the
|
|
||||||
enclosing container after each element, so that the nesting depth of the
|
|
||||||
input costs heap rather than native stack (see #5104).
|
|
||||||
|
|
||||||
@param[in] get_char whether a new character should be retrieved from the
|
|
||||||
input (true) or whether the last read character
|
|
||||||
@a current should be considered instead
|
|
||||||
@param[in] tag_handler how CBOR tags should be treated
|
@param[in] tag_handler how CBOR tags should be treated
|
||||||
|
@return whether array creation completed
|
||||||
@return whether reading the value succeeded
|
|
||||||
*/
|
*/
|
||||||
bool parse_cbor_internal(const bool get_char,
|
bool get_cbor_array(const std::size_t len,
|
||||||
const cbor_tag_handler_t tag_handler)
|
const cbor_tag_handler_t tag_handler)
|
||||||
{
|
{
|
||||||
// whether the next value starts at a fresh byte or at the one already
|
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
|
||||||
// read into `current`
|
|
||||||
bool fetch = get_char;
|
|
||||||
|
|
||||||
// the key currently being read; hoisted out of the loop so that its
|
|
||||||
// capacity is reused across elements and across nesting levels
|
|
||||||
string_t key;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
if (!container_stack.empty())
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len != detail::unknown_size())
|
||||||
|
{
|
||||||
|
for (std::size_t i = 0; i < len; ++i)
|
||||||
{
|
{
|
||||||
// the reference is not held across parse_cbor_value() below,
|
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
||||||
// which can push onto the stack and reallocate it
|
|
||||||
container_frame& top = container_stack.back();
|
|
||||||
bool at_end;
|
|
||||||
|
|
||||||
if (top.remaining != npos)
|
|
||||||
{
|
{
|
||||||
// definite length: the container ends once its elements
|
return false;
|
||||||
// have been read
|
|
||||||
at_end = (top.remaining == 0);
|
|
||||||
if (!at_end)
|
|
||||||
{
|
|
||||||
// claim the element about to be read
|
|
||||||
--top.remaining;
|
|
||||||
if (top.is_object)
|
|
||||||
{
|
|
||||||
get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fetch = true;
|
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (get() != 0xFF)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
|
||||||
{
|
{
|
||||||
// indefinite length: the container ends at a break marker.
|
return false;
|
||||||
// Testing for it consumes a byte, which is the first byte
|
|
||||||
// of the next element when it is not one.
|
|
||||||
at_end = (get() == 0xFF);
|
|
||||||
fetch = top.is_object;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (at_end)
|
return sax->end_array();
|
||||||
{
|
}
|
||||||
const bool is_object = top.is_object;
|
|
||||||
container_stack.pop_back();
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->end_object() : !sax->end_array()))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// the value begun here is complete once its container is
|
|
||||||
if (container_stack.empty())
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (top.is_object)
|
/*!
|
||||||
|
@param[in] len the length of the object or detail::unknown_size() for an
|
||||||
|
object of indefinite size
|
||||||
|
@param[in] tag_handler how CBOR tags should be treated
|
||||||
|
@return whether object creation completed
|
||||||
|
*/
|
||||||
|
bool get_cbor_object(const std::size_t len,
|
||||||
|
const cbor_tag_handler_t tag_handler)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len != 0)
|
||||||
|
{
|
||||||
|
string_t key;
|
||||||
|
if (len != detail::unknown_size())
|
||||||
|
{
|
||||||
|
for (std::size_t i = 0; i < len; ++i)
|
||||||
{
|
{
|
||||||
key.clear();
|
get();
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
fetch = true;
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
key.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// a tag is not a value of its own: read on until the tagged value
|
|
||||||
bool tag_pending;
|
|
||||||
do
|
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_value(fetch, tag_handler, tag_pending)))
|
while (get() != 0xFF)
|
||||||
{
|
{
|
||||||
return false;
|
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
|
||||||
}
|
{
|
||||||
fetch = true;
|
return false;
|
||||||
}
|
}
|
||||||
while (tag_pending);
|
|
||||||
|
|
||||||
// a value that opened a container left it on the stack; one that
|
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
|
||||||
// did not, and that was not inside a container, was the whole value
|
{
|
||||||
if (container_stack.empty())
|
return false;
|
||||||
{
|
}
|
||||||
return true;
|
key.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return sax->end_object();
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////
|
/////////////
|
||||||
@@ -12185,17 +12003,7 @@ class binary_reader
|
|||||||
/*!
|
/*!
|
||||||
@return whether a valid MessagePack value was passed to the SAX parser
|
@return whether a valid MessagePack value was passed to the SAX parser
|
||||||
*/
|
*/
|
||||||
/*!
|
bool parse_msgpack_internal()
|
||||||
@brief read one MessagePack value
|
|
||||||
|
|
||||||
Reads a single value and passes it to the SAX parser. A value that begins
|
|
||||||
a container is not read to its end: the container is opened with
|
|
||||||
@ref enter_container and its elements are read by
|
|
||||||
@ref parse_msgpack_internal, so that nesting does not consume native stack.
|
|
||||||
|
|
||||||
@return whether reading the value succeeded
|
|
||||||
*/
|
|
||||||
bool parse_msgpack_value()
|
|
||||||
{
|
{
|
||||||
switch (get())
|
switch (get())
|
||||||
{
|
{
|
||||||
@@ -12351,7 +12159,7 @@ class binary_reader
|
|||||||
case 0x8D:
|
case 0x8D:
|
||||||
case 0x8E:
|
case 0x8E:
|
||||||
case 0x8F:
|
case 0x8F:
|
||||||
return enter_container(/*is_object*/true, conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
||||||
|
|
||||||
// fixarray
|
// fixarray
|
||||||
case 0x90:
|
case 0x90:
|
||||||
@@ -12370,7 +12178,7 @@ class binary_reader
|
|||||||
case 0x9D:
|
case 0x9D:
|
||||||
case 0x9E:
|
case 0x9E:
|
||||||
case 0x9F:
|
case 0x9F:
|
||||||
return enter_container(/*is_object*/false, conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
||||||
|
|
||||||
// fixstr
|
// fixstr
|
||||||
case 0xA0:
|
case 0xA0:
|
||||||
@@ -12501,25 +12309,25 @@ class binary_reader
|
|||||||
case 0xDC: // array 16
|
case 0xDC: // array 16
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && enter_container(/*is_object*/false, static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xDD: // array 32
|
case 0xDD: // array 32
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && enter_container(/*is_object*/false, conditional_static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xDE: // map 16
|
case 0xDE: // map 16
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && enter_container(/*is_object*/true, static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xDF: // map 32
|
case 0xDF: // map 32
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && enter_container(/*is_object*/true, conditional_static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
// negative fixint
|
// negative fixint
|
||||||
@@ -12767,69 +12575,55 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief read a MessagePack value and everything nested inside it
|
@param[in] len the length of the array
|
||||||
|
@return whether array creation completed
|
||||||
Reads values until the one that was begun here is complete, resuming the
|
|
||||||
enclosing container each time an element ends, so that the nesting depth
|
|
||||||
of the input costs heap rather than native stack (see #5104).
|
|
||||||
|
|
||||||
@return whether reading the value succeeded
|
|
||||||
*/
|
*/
|
||||||
bool parse_msgpack_internal()
|
bool get_msgpack_array(const std::size_t len)
|
||||||
{
|
{
|
||||||
// the key currently being read; hoisted out of the loop so that its
|
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
|
||||||
// capacity is reused across elements and across nesting levels
|
|
||||||
string_t key;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
if (!container_stack.empty())
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < len; ++i)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
|
||||||
{
|
{
|
||||||
// copied out before anything can push onto the stack and
|
return false;
|
||||||
// invalidate a reference into it
|
|
||||||
const bool is_object = container_stack.back().is_object;
|
|
||||||
|
|
||||||
if (container_stack.back().remaining == 0)
|
|
||||||
{
|
|
||||||
container_stack.pop_back();
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->end_object() : !sax->end_array()))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// the value begun here is complete once its container is
|
|
||||||
if (container_stack.empty())
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// claim the element about to be read
|
|
||||||
--container_stack.back().remaining;
|
|
||||||
|
|
||||||
if (is_object)
|
|
||||||
{
|
|
||||||
get();
|
|
||||||
key.clear();
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_value()))
|
return sax->end_array();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@param[in] len the length of the object
|
||||||
|
@return whether object creation completed
|
||||||
|
*/
|
||||||
|
bool get_msgpack_object(const std::size_t len)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
string_t key;
|
||||||
|
for (std::size_t i = 0; i < len; ++i)
|
||||||
|
{
|
||||||
|
get();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// a value that opened a container left it on the stack; one that
|
if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
|
||||||
// did not, and that was not inside a container, was the whole value
|
|
||||||
if (container_stack.empty())
|
|
||||||
{
|
{
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
key.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return sax->end_object();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////
|
////////////
|
||||||
@@ -13284,12 +13078,7 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
result.first = npos; // size
|
result.first = npos; // size
|
||||||
result.second = 0; // type
|
result.second = 0; // type
|
||||||
// seed the flag with the caller's context: inside an ndarray dimension
|
bool is_ndarray = false;
|
||||||
// vector another ndarray is not allowed, and get_ubjson_size_value()
|
|
||||||
// rejects it up front instead of reading it and reporting afterwards.
|
|
||||||
// Seeding it with `false` made every '#' of a "[#[#[..." chain descend
|
|
||||||
// another level, which overflowed the stack (see #5104).
|
|
||||||
bool is_ndarray = inside_ndarray;
|
|
||||||
|
|
||||||
get_ignore_noop();
|
get_ignore_noop();
|
||||||
|
|
||||||
@@ -13322,11 +13111,13 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
||||||
// an ndarray was read here only if the flag flipped; when it was
|
if (input_format == input_format_t::bjdata && is_ndarray)
|
||||||
// seeded true, get_ubjson_size_value() already rejected the nested
|
|
||||||
// dimension vector
|
|
||||||
if (input_format == input_format_t::bjdata && is_ndarray && !inside_ndarray)
|
|
||||||
{
|
{
|
||||||
|
if (inside_ndarray)
|
||||||
|
{
|
||||||
|
return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
|
||||||
|
exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
|
||||||
|
}
|
||||||
result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
|
result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
|
||||||
}
|
}
|
||||||
return is_error;
|
return is_error;
|
||||||
@@ -13335,7 +13126,7 @@ class binary_reader
|
|||||||
if (current == '#')
|
if (current == '#')
|
||||||
{
|
{
|
||||||
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
||||||
if (input_format == input_format_t::bjdata && is_ndarray && !inside_ndarray)
|
if (input_format == input_format_t::bjdata && is_ndarray)
|
||||||
{
|
{
|
||||||
return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
|
return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
|
||||||
exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
|
exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
|
||||||
@@ -13606,17 +13397,6 @@ class binary_reader
|
|||||||
|
|
||||||
if (size_and_type.first != npos)
|
if (size_and_type.first != npos)
|
||||||
{
|
{
|
||||||
// reading an element of a valueless type consumes no input, so the
|
|
||||||
// declared count alone decides how much is allocated; the check is
|
|
||||||
// made before the start event so that no container is opened that
|
|
||||||
// is then abandoned. See @ref max_valueless_container_size.
|
|
||||||
if (JSON_HEDLEY_UNLIKELY((size_and_type.second == 'Z' || size_and_type.second == 'T' || size_and_type.second == 'F')
|
|
||||||
&& size_and_type.first > max_valueless_container_size))
|
|
||||||
{
|
|
||||||
return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
|
|
||||||
exception_message(input_format, "excessive array size", "size"), nullptr));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
|
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -14134,9 +13914,6 @@ class binary_reader
|
|||||||
/// the SAX parser
|
/// the SAX parser
|
||||||
json_sax_t* sax = nullptr;
|
json_sax_t* sax = nullptr;
|
||||||
|
|
||||||
/// the containers that have been opened and not closed yet; see @ref container_frame
|
|
||||||
std::vector<container_frame> container_stack{};
|
|
||||||
|
|
||||||
// excluded markers in bjdata optimized type
|
// excluded markers in bjdata optimized type
|
||||||
#define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
|
#define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
|
||||||
make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
|
make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
|
||||||
@@ -18057,17 +17834,7 @@ class binary_writer
|
|||||||
|
|
||||||
std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
|
std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
|
||||||
|
|
||||||
// an optimized array of a valueless type carries no payload, so a
|
if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
|
||||||
// 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;
|
prefix_required = false;
|
||||||
oa->write_character(to_char_type('$'));
|
oa->write_character(to_char_type('$'));
|
||||||
@@ -26135,7 +25902,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? 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)
|
/// @brief create a JSON value from an input in CBOR format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -26152,7 +25919,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -26178,7 +25945,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// 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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in MessagePack format
|
/// @brief create a JSON value from an input in MessagePack format
|
||||||
@@ -26193,7 +25960,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? 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)
|
/// @brief create a JSON value from an input in MessagePack format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -26209,7 +25976,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -26233,7 +26000,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// 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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in UBJSON format
|
/// @brief create a JSON value from an input in UBJSON format
|
||||||
@@ -26248,7 +26015,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? 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)
|
/// @brief create a JSON value from an input in UBJSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -26264,7 +26031,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -26288,7 +26055,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// 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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BJData format
|
/// @brief create a JSON value from an input in BJData format
|
||||||
@@ -26303,7 +26070,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? 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)
|
/// @brief create a JSON value from an input in BJData format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -26319,7 +26086,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BSON format
|
/// @brief create a JSON value from an input in BSON format
|
||||||
@@ -26334,7 +26101,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? 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)
|
/// @brief create a JSON value from an input in BSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -26350,7 +26117,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -26374,7 +26141,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// 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]
|
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 ? std::move(result) : basic_json(value_t::discarded);
|
return res ? result : basic_json(value_t::discarded);
|
||||||
}
|
}
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
|
|||||||
@@ -3288,10 +3288,8 @@ 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_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());
|
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};
|
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 7: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
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(json::from_bjdata(vR2, true, false).is_discarded());
|
CHECK(json::from_bjdata(vR2, true, false).is_discarded());
|
||||||
|
|
||||||
std::vector<uint8_t> const vR3 = {'[', '#', '[', 'i', '2', 'i', 2, ']'};
|
std::vector<uint8_t> const vR3 = {'[', '#', '[', 'i', '2', 'i', 2, ']'};
|
||||||
@@ -3299,7 +3297,7 @@ TEST_CASE("BJData")
|
|||||||
CHECK(json::from_bjdata(vR3, true, false).is_discarded());
|
CHECK(json::from_bjdata(vR3, true, false).is_discarded());
|
||||||
|
|
||||||
std::vector<uint8_t> const vR4 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', 1, ']', 1};
|
std::vector<uint8_t> const vR4 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', 1, ']', 1};
|
||||||
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_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(json::from_bjdata(vR4, true, false).is_discarded());
|
CHECK(json::from_bjdata(vR4, true, false).is_discarded());
|
||||||
|
|
||||||
std::vector<uint8_t> const vR5 = {'[', '$', 'i', '#', '[', '[', '[', ']', ']', ']'};
|
std::vector<uint8_t> const vR5 = {'[', '$', 'i', '#', '[', '[', '[', ']', ']', ']'};
|
||||||
@@ -3307,25 +3305,12 @@ TEST_CASE("BJData")
|
|||||||
CHECK(json::from_bjdata(vR5, true, false).is_discarded());
|
CHECK(json::from_bjdata(vR5, true, false).is_discarded());
|
||||||
|
|
||||||
std::vector<uint8_t> const vR6 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
|
std::vector<uint8_t> const vR6 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
|
||||||
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_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(json::from_bjdata(vR6, true, false).is_discarded());
|
CHECK(json::from_bjdata(vR6, true, false).is_discarded());
|
||||||
|
|
||||||
std::vector<uint8_t> const vH = {'[', 'H', '[', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
|
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_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());
|
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")
|
SECTION("objects")
|
||||||
|
|||||||
@@ -2035,145 +2035,6 @@ 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")
|
|
||||||
{
|
|
||||||
// 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 _;
|
|
||||||
|
|
||||||
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&);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("CBOR roundtrips" * doctest::skip())
|
TEST_CASE("CBOR roundtrips" * doctest::skip())
|
||||||
{
|
{
|
||||||
SECTION("input from flynn")
|
SECTION("input from flynn")
|
||||||
|
|||||||
@@ -1598,67 +1598,6 @@ 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")
|
||||||
|
|||||||
@@ -0,0 +1,489 @@
|
|||||||
|
// __ _____ _____ _____
|
||||||
|
// __| | __| | | | JSON for Modern C++ (supporting code)
|
||||||
|
// | | |__ | | | | | | version 3.12.0
|
||||||
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||||
|
//
|
||||||
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||||
|
// SPDX-FileCopyrightText: 2018 Vitaliy Manushkin <agri@akamo.info>
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
// This file closes a test-coverage gap described in GitHub issue #5421:
|
||||||
|
// nlohmann::ordered_json (and other non-default basic_json specializations,
|
||||||
|
// such as the alt_string-based one from unit-alt-string.cpp) were never
|
||||||
|
// exercised through the binary formats (CBOR/MessagePack/UBJSON/BSON/BJData)
|
||||||
|
// or through flatten()/unflatten()/diff()/patch()/merge_patch().
|
||||||
|
|
||||||
|
#include "doctest_compatibility.h"
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using nlohmann::json;
|
||||||
|
using nlohmann::ordered_json;
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
// alt_json: a second, independent copy of the custom-string_t basic_json
|
||||||
|
// specialization defined in unit-alt-string.cpp.
|
||||||
|
//
|
||||||
|
// It is duplicated here (rather than shared via a header) because every
|
||||||
|
// unit-*.cpp file in this test suite is compiled into its own standalone
|
||||||
|
// executable (see tests/CMakeLists.txt), so there is no ODR concern in
|
||||||
|
// having the same class name defined in multiple translation units.
|
||||||
|
//
|
||||||
|
// Two members had to be added relative to the original alt_string
|
||||||
|
// (a constructor from std::string, and a find(char, pos) overload) because
|
||||||
|
// the original type was never used with the binary writers/readers before
|
||||||
|
// this file: BSON's array/document writer converts std::to_string() results
|
||||||
|
// and checks for embedded NUL characters via find(char), and the UBJSON/BSON
|
||||||
|
// high-precision-number path constructs the SAX string_t argument from a
|
||||||
|
// std::string. Neither path is exercised anywhere else in the test suite for
|
||||||
|
// this type, which is presumably why the gap was never noticed.
|
||||||
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
class alt_string;
|
||||||
|
bool operator<(const char* op1, const alt_string& op2) noexcept; // NOLINT(misc-use-internal-linkage)
|
||||||
|
void int_to_string(alt_string& target, std::size_t value); // NOLINT(misc-use-internal-linkage)
|
||||||
|
|
||||||
|
class alt_string
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using value_type = std::string::value_type;
|
||||||
|
|
||||||
|
static constexpr auto npos = (std::numeric_limits<std::size_t>::max)();
|
||||||
|
|
||||||
|
alt_string(const char* str): str_impl(str) {}
|
||||||
|
alt_string(const char* str, std::size_t count): str_impl(str, count) {}
|
||||||
|
alt_string(std::string str): str_impl(std::move(str)) {}
|
||||||
|
alt_string(size_t count, char chr): str_impl(count, chr) {}
|
||||||
|
alt_string() = default;
|
||||||
|
|
||||||
|
alt_string& append(char ch)
|
||||||
|
{
|
||||||
|
str_impl.push_back(ch);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
alt_string& append(const alt_string& str)
|
||||||
|
{
|
||||||
|
str_impl.append(str.str_impl);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
alt_string& append(const char* s, std::size_t length)
|
||||||
|
{
|
||||||
|
str_impl.append(s, length);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_back(char c)
|
||||||
|
{
|
||||||
|
str_impl.push_back(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename op_type>
|
||||||
|
bool operator==(const op_type& op) const
|
||||||
|
{
|
||||||
|
return str_impl == op;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const alt_string& op) const
|
||||||
|
{
|
||||||
|
return str_impl == op.str_impl;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename op_type>
|
||||||
|
bool operator!=(const op_type& op) const
|
||||||
|
{
|
||||||
|
return str_impl != op;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const alt_string& op) const
|
||||||
|
{
|
||||||
|
return str_impl != op.str_impl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t size() const noexcept
|
||||||
|
{
|
||||||
|
return str_impl.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
void resize(std::size_t n)
|
||||||
|
{
|
||||||
|
str_impl.resize(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void resize(std::size_t n, char c)
|
||||||
|
{
|
||||||
|
str_impl.resize(n, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename op_type>
|
||||||
|
bool operator<(const op_type& op) const noexcept
|
||||||
|
{
|
||||||
|
return str_impl < op;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator<(const alt_string& op) const noexcept
|
||||||
|
{
|
||||||
|
return str_impl < op.str_impl;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* c_str() const
|
||||||
|
{
|
||||||
|
return str_impl.c_str();
|
||||||
|
}
|
||||||
|
|
||||||
|
char& operator[](std::size_t index)
|
||||||
|
{
|
||||||
|
return str_impl[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
const char& operator[](std::size_t index) const
|
||||||
|
{
|
||||||
|
return str_impl[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
char& back()
|
||||||
|
{
|
||||||
|
return str_impl.back();
|
||||||
|
}
|
||||||
|
|
||||||
|
const char& back() const
|
||||||
|
{
|
||||||
|
return str_impl.back();
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
str_impl.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
const value_type* data() const
|
||||||
|
{
|
||||||
|
return str_impl.data();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool empty() const
|
||||||
|
{
|
||||||
|
return str_impl.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t find(const alt_string& str, std::size_t pos = 0) const
|
||||||
|
{
|
||||||
|
return str_impl.find(str.str_impl, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// needed by binary_writer's BSON support, which probes string keys for
|
||||||
|
// embedded NUL characters via find(char)
|
||||||
|
std::size_t find(char c, std::size_t pos = 0) const
|
||||||
|
{
|
||||||
|
return str_impl.find(c, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t find_first_of(char c, std::size_t pos = 0) const
|
||||||
|
{
|
||||||
|
return str_impl.find_first_of(c, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
alt_string substr(std::size_t pos = 0, std::size_t count = npos) const
|
||||||
|
{
|
||||||
|
const std::string s = str_impl.substr(pos, count);
|
||||||
|
return {s.data(), s.size()};
|
||||||
|
}
|
||||||
|
|
||||||
|
alt_string& replace(std::size_t pos, std::size_t count, const alt_string& str)
|
||||||
|
{
|
||||||
|
str_impl.replace(pos, count, str.str_impl);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reserve(std::size_t new_cap = 0)
|
||||||
|
{
|
||||||
|
str_impl.reserve(new_cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string str_impl {}; // NOLINT(readability-redundant-member-init)
|
||||||
|
|
||||||
|
friend bool operator<(const char* /*op1*/, const alt_string& /*op2*/) noexcept;
|
||||||
|
};
|
||||||
|
|
||||||
|
void int_to_string(alt_string& target, std::size_t value)
|
||||||
|
{
|
||||||
|
target = std::to_string(value).c_str();
|
||||||
|
}
|
||||||
|
|
||||||
|
using alt_json = nlohmann::basic_json <
|
||||||
|
std::map,
|
||||||
|
std::vector,
|
||||||
|
alt_string,
|
||||||
|
bool,
|
||||||
|
std::int64_t,
|
||||||
|
std::uint64_t,
|
||||||
|
double,
|
||||||
|
std::allocator,
|
||||||
|
nlohmann::adl_serializer >;
|
||||||
|
|
||||||
|
bool operator<(const char* op1, const alt_string& op2) noexcept
|
||||||
|
{
|
||||||
|
return op1 < op2.str_impl;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
// collects the object keys of j, in iteration order
|
||||||
|
std::vector<std::string> collect_keys(const ordered_json& j)
|
||||||
|
{
|
||||||
|
std::vector<std::string> result;
|
||||||
|
for (auto it = j.cbegin(); it != j.cend(); ++it)
|
||||||
|
{
|
||||||
|
result.push_back(it.key());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// a nested object/array value with keys inserted in non-alphabetical order,
|
||||||
|
// used to check both round-trip equality and (for ordered_json) that
|
||||||
|
// insertion order survives a trip through a binary format
|
||||||
|
ordered_json make_rich_ordered_json()
|
||||||
|
{
|
||||||
|
ordered_json j;
|
||||||
|
j["zebra"] = 1;
|
||||||
|
j["apple"] = ordered_json::array({1, 2, 3});
|
||||||
|
j["mango"]["z_nested"] = true;
|
||||||
|
j["mango"]["a_nested"] = nullptr;
|
||||||
|
j["banana"] = "some text";
|
||||||
|
j["cherry"] = 3.14;
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
alt_json make_rich_alt_json()
|
||||||
|
{
|
||||||
|
alt_json j;
|
||||||
|
j["zebra"] = 1;
|
||||||
|
j["apple"] = alt_json::array({1, 2, 3});
|
||||||
|
j["mango"]["z_nested"] = true;
|
||||||
|
j["mango"]["a_nested"] = nullptr;
|
||||||
|
j["banana"] = "some text";
|
||||||
|
j["cherry"] = 3.14;
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST_CASE("ordered_json across binary formats")
|
||||||
|
{
|
||||||
|
const ordered_json original = make_rich_ordered_json();
|
||||||
|
const std::vector<std::string> original_keys = collect_keys(original);
|
||||||
|
const std::vector<std::string> original_mango_keys = collect_keys(original["mango"]);
|
||||||
|
|
||||||
|
SECTION("CBOR")
|
||||||
|
{
|
||||||
|
const auto bytes = ordered_json::to_cbor(original);
|
||||||
|
const auto restored = ordered_json::from_cbor(bytes);
|
||||||
|
CHECK(restored == original);
|
||||||
|
CHECK(collect_keys(restored) == original_keys);
|
||||||
|
CHECK(collect_keys(restored["mango"]) == original_mango_keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("MessagePack")
|
||||||
|
{
|
||||||
|
const auto bytes = ordered_json::to_msgpack(original);
|
||||||
|
const auto restored = ordered_json::from_msgpack(bytes);
|
||||||
|
CHECK(restored == original);
|
||||||
|
CHECK(collect_keys(restored) == original_keys);
|
||||||
|
CHECK(collect_keys(restored["mango"]) == original_mango_keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("UBJSON")
|
||||||
|
{
|
||||||
|
const auto bytes = ordered_json::to_ubjson(original);
|
||||||
|
const auto restored = ordered_json::from_ubjson(bytes);
|
||||||
|
CHECK(restored == original);
|
||||||
|
CHECK(collect_keys(restored) == original_keys);
|
||||||
|
CHECK(collect_keys(restored["mango"]) == original_mango_keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("BSON")
|
||||||
|
{
|
||||||
|
const auto bytes = ordered_json::to_bson(original);
|
||||||
|
const auto restored = ordered_json::from_bson(bytes);
|
||||||
|
CHECK(restored == original);
|
||||||
|
CHECK(collect_keys(restored) == original_keys);
|
||||||
|
CHECK(collect_keys(restored["mango"]) == original_mango_keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("BJData")
|
||||||
|
{
|
||||||
|
const auto bytes = ordered_json::to_bjdata(original);
|
||||||
|
const auto restored = ordered_json::from_bjdata(bytes);
|
||||||
|
CHECK(restored == original);
|
||||||
|
CHECK(collect_keys(restored) == original_keys);
|
||||||
|
CHECK(collect_keys(restored["mango"]) == original_mango_keys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("alt_json (custom string_t) across binary formats")
|
||||||
|
{
|
||||||
|
const alt_json original = make_rich_alt_json();
|
||||||
|
|
||||||
|
SECTION("CBOR")
|
||||||
|
{
|
||||||
|
const auto bytes = alt_json::to_cbor(original);
|
||||||
|
const auto restored = alt_json::from_cbor(bytes);
|
||||||
|
CHECK(restored == original);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("MessagePack")
|
||||||
|
{
|
||||||
|
const auto bytes = alt_json::to_msgpack(original);
|
||||||
|
const auto restored = alt_json::from_msgpack(bytes);
|
||||||
|
CHECK(restored == original);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("UBJSON")
|
||||||
|
{
|
||||||
|
const auto bytes = alt_json::to_ubjson(original);
|
||||||
|
const auto restored = alt_json::from_ubjson(bytes);
|
||||||
|
CHECK(restored == original);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("BSON")
|
||||||
|
{
|
||||||
|
const auto bytes = alt_json::to_bson(original);
|
||||||
|
const auto restored = alt_json::from_bson(bytes);
|
||||||
|
CHECK(restored == original);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("BJData")
|
||||||
|
{
|
||||||
|
const auto bytes = alt_json::to_bjdata(original);
|
||||||
|
const auto restored = alt_json::from_bjdata(bytes);
|
||||||
|
CHECK(restored == original);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("ordered_json operator== is sensitive to key order")
|
||||||
|
{
|
||||||
|
// Unlike nlohmann::json (whose object_t is a std::map, so equality never
|
||||||
|
// depends on insertion order), ordered_json's object_t (ordered_map) is a
|
||||||
|
// std::vector<std::pair<Key, T>> under the hood, and does not define its
|
||||||
|
// own operator==: it inherits std::vector's element-wise comparison. As a
|
||||||
|
// result, two ordered_json objects holding the very same key/value pairs
|
||||||
|
// in different insertion order compare *unequal*. This is the property
|
||||||
|
// that makes the round-trip `CHECK(restored == original)` checks above a
|
||||||
|
// meaningful order-preservation check by themselves (the explicit
|
||||||
|
// collect_keys() comparisons make that check explicit/readable, and
|
||||||
|
// guard against this operator== behavior ever changing).
|
||||||
|
ordered_json a;
|
||||||
|
a["x"] = 1;
|
||||||
|
a["y"] = 2;
|
||||||
|
|
||||||
|
ordered_json b;
|
||||||
|
b["y"] = 2;
|
||||||
|
b["x"] = 1;
|
||||||
|
|
||||||
|
CHECK(a.size() == b.size());
|
||||||
|
CHECK(a["x"] == b["x"]);
|
||||||
|
CHECK(a["y"] == b["y"]);
|
||||||
|
CHECK_FALSE(a == b);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("duplicate keys in a binary-encoded object")
|
||||||
|
{
|
||||||
|
// CBOR encoding of a map with two entries under the same key "a": {"a": 1, "a": 2}
|
||||||
|
const std::vector<std::uint8_t> cbor_bytes
|
||||||
|
{
|
||||||
|
0xA2, 0x61, 'a', 0x01, 0x61, 'a', 0x02
|
||||||
|
};
|
||||||
|
|
||||||
|
// Both json (std::map, via operator[]) and ordered_json (ordered_map, via
|
||||||
|
// operator[]) build binary-decoded objects by looking up/creating the
|
||||||
|
// entry for each incoming key and then assigning the value into it. This
|
||||||
|
// means a repeated key does *not* produce two entries in either case;
|
||||||
|
// instead, the *first* occurrence's position is kept (relevant only for
|
||||||
|
// ordered_json) while the *last* occurrence's value wins (for both) --
|
||||||
|
// this matches operator[]'s "assign the referenced slot" semantics, and
|
||||||
|
// is worth noting because it differs from the initializer-list
|
||||||
|
// construction path (`ordered_json{{"a",1},{"a",2}}`), which builds
|
||||||
|
// through insert()/emplace() and therefore keeps the *first* value, not
|
||||||
|
// the last (see the "There are no dup keys..." case in
|
||||||
|
// unit-ordered_json.cpp).
|
||||||
|
const auto j = json::from_cbor(cbor_bytes);
|
||||||
|
const auto oj = ordered_json::from_cbor(cbor_bytes);
|
||||||
|
|
||||||
|
CHECK(j.size() == 1);
|
||||||
|
CHECK(oj.size() == 1);
|
||||||
|
CHECK(j["a"] == 2);
|
||||||
|
CHECK(oj["a"] == 2);
|
||||||
|
CHECK(j == json(oj));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("ordered_json through flatten/unflatten")
|
||||||
|
{
|
||||||
|
const ordered_json original = make_rich_ordered_json();
|
||||||
|
const std::vector<std::string> original_keys = collect_keys(original);
|
||||||
|
const std::vector<std::string> original_mango_keys = collect_keys(original["mango"]);
|
||||||
|
|
||||||
|
const ordered_json flat = original.flatten();
|
||||||
|
const ordered_json unflattened = flat.unflatten();
|
||||||
|
|
||||||
|
CHECK(unflattened == original);
|
||||||
|
// flatten() walks the value depth-first in iteration order and
|
||||||
|
// unflatten() re-inserts each flattened key via operator[] in the flat
|
||||||
|
// object's iteration order, so for ordered_json the original key order
|
||||||
|
// (both top-level and nested) is preserved end-to-end.
|
||||||
|
CHECK(collect_keys(unflattened) == original_keys);
|
||||||
|
CHECK(collect_keys(unflattened["mango"]) == original_mango_keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("ordered_json through diff/patch/patch_inplace")
|
||||||
|
{
|
||||||
|
ordered_json original;
|
||||||
|
original["one"] = 1;
|
||||||
|
original["two"] = 2;
|
||||||
|
original["three"] = 3;
|
||||||
|
|
||||||
|
ordered_json target = original;
|
||||||
|
target["one"] = 100; // replace
|
||||||
|
target.erase("two"); // remove
|
||||||
|
target["four"] = 4; // add
|
||||||
|
|
||||||
|
const ordered_json patch = ordered_json::diff(original, target);
|
||||||
|
|
||||||
|
SECTION("patch")
|
||||||
|
{
|
||||||
|
const ordered_json patched = original.patch(patch);
|
||||||
|
CHECK(patched == target);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("patch_inplace")
|
||||||
|
{
|
||||||
|
ordered_json copy = original;
|
||||||
|
copy.patch_inplace(patch);
|
||||||
|
CHECK(copy == target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("ordered_json through merge_patch")
|
||||||
|
{
|
||||||
|
ordered_json original;
|
||||||
|
original["a"] = 1;
|
||||||
|
original["b"] = 2;
|
||||||
|
|
||||||
|
const ordered_json patch = {{"b", nullptr}, {"c", 3}};
|
||||||
|
|
||||||
|
original.merge_patch(patch);
|
||||||
|
|
||||||
|
ordered_json expected;
|
||||||
|
expected["a"] = 1;
|
||||||
|
expected["c"] = 3;
|
||||||
|
|
||||||
|
CHECK(original == expected);
|
||||||
|
CHECK(collect_keys(original) == collect_keys(expected));
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
using ordered_json = nlohmann::ordered_json;
|
||||||
|
|
||||||
// JSON_HAS_CPP_20 (do not remove; see note at top of file)
|
// JSON_HAS_CPP_20 (do not remove; see note at top of file)
|
||||||
#if JSON_HAS_STD_FORMAT
|
#if JSON_HAS_STD_FORMAT
|
||||||
@@ -93,4 +94,16 @@ TEST_CASE("std::formatter<nlohmann::json>")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("std::formatter<nlohmann::ordered_json>")
|
||||||
|
{
|
||||||
|
// spot-check a non-default basic_json instantiation, since the formatter
|
||||||
|
// is written against the generic NLOHMANN_BASIC_JSON_TPL_DECLARATION
|
||||||
|
// template and must actually instantiate (and behave correctly) for
|
||||||
|
// template arguments other than nlohmann::json
|
||||||
|
const ordered_json j = {{"foo", 1}, {"bar", {1, 2, 3}}};
|
||||||
|
CHECK(std::format("{}", j) == j.dump());
|
||||||
|
CHECK(std::format("{:#}", j) == j.dump(4));
|
||||||
|
CHECK(std::format("{:2}", j) == j.dump(2));
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -2149,61 +2149,6 @@ TEST_CASE("UBJSON")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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'
|
|
||||||
})
|
|
||||||
{
|
|
||||||
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")
|
|
||||||
{
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("a type with a payload is unaffected")
|
|
||||||
{
|
|
||||||
// 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&);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("Universal Binary JSON Specification Examples 1")
|
TEST_CASE("Universal Binary JSON Specification Examples 1")
|
||||||
{
|
{
|
||||||
SECTION("Null Value")
|
SECTION("Null Value")
|
||||||
|
|||||||
Reference in New Issue
Block a user