BSON reader: recover from the document size checks made on reading the size

Since #5930, a size below 5, a size that exceeds the enclosing document, and
a size too small for the document's content are reported before the document
is read. When recovering, such a document is now read up to its terminator,
within the enclosing document, and its size is not reported a second time.
BJData now checks an ndarray's type before the object that would hold it is
begun, so that test expects null.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-02 13:47:31 +01:00
1 parent 02d2fa8605
commit 405b8dcb13
3 files changed
+142 -37

No files matched your search

+18 -5
View File
@@ -483,6 +483,17 @@ TEST_CASE("regression test - #3989 SAX parse_error() returning true")
{{"s", "a"}, {"inner", {{"a", 1}}}},
2
},
// BSON: so is a document whose size is too small for its content,
// which is read up to its terminator
{
json::input_format_t::bson, bson_document(
{
bson_element(0x03, "inner", bson_document({bson_element(0x10, "a", bson_int32(1)), bson_element(0x10, "b", bson_int32(2))}, -7)),
bson_element(0x10, "after", bson_int32(3)),
}),
{{"inner", {{"a", 1}, {"b", 2}}}, {"after", 3}},
1
},
};
for (const auto& repair : repairs)
@@ -548,12 +559,12 @@ TEST_CASE("regression test - #3989 SAX parse_error() returning true")
CHECK(nested.errors == 1);
CHECK(nested.value == json({1}));
// a BJData ndarray whose element type has no name: the object that
// holds the ndarray was already begun
// a BJData ndarray whose element type is not valid: the type is
// checked before the object that would hold the ndarray is begun
const auto ndarray = parse_binary_recovering({'[', '$', 0x01, '#', '[', '$', 'i', '#', 'i', 2, 2, 3}, json::input_format_t::bjdata);
CHECK(ndarray.errors == 1);
CHECK(ndarray.balanced);
CHECK(ndarray.value == json::object());
CHECK(ndarray.value == nullptr);
// a skipped member that the input ends in
const auto truncated = parse_binary_recovering({0xA2, 0x01, 0x82, 0x01}, json::input_format_t::cbor);
@@ -561,9 +572,11 @@ TEST_CASE("regression test - #3989 SAX parse_error() returning true")
CHECK(truncated.balanced);
CHECK(truncated.value == json::object());
// a BSON element of an unknown type in a document whose size cannot be right
// a BSON element of an unknown type in a document whose size cannot be
// right: the size is reported when it is read, and the element ends
// the document, as its end is not known
const auto bson = parse_binary_recovering(bson_document({bson_element(0x10, "a", bson_int32(1)), bson_element(0x42, "x", bytes(3))}, -10), json::input_format_t::bson);
CHECK(bson.errors == 1);
CHECK(bson.errors == 2);
CHECK(bson.value == json({{"a", 1}, {"x", nullptr}}));
}