Skip to content

Commit 1b2b595

Browse files
committed
Move the new binary-subtype regression test out of unit-regression2.cpp
unit-regression2.cpp is already at the edge of what the MinGW linker can relocate; adding this test's ~26 lines tips test-regression2_cpp20 (clang, Windows) over into "relocation truncated to fit: IMAGE_REL_AMD64_REL32 against `.rdata'" (see 8ce64b9 / b82717c for the same failure mode). Split the test along format lines instead: MessagePack assertions move to unit-msgpack.cpp, BSON assertions to unit-bson.cpp. The CBOR round-trip guard is dropped as redundant -- unit-cbor.cpp's "Tagged values" section already round-trips subtypes up to 8589934590, far past the 70000 checked here. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
1 parent 9382236 commit 1b2b595

3 files changed

Lines changed: 24 additions & 26 deletions

File tree

tests/src/unit-bson.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,15 @@ TEST_CASE("BSON")
791791
}
792792
}
793793

794+
TEST_CASE("regression test - BSON binary subtype rejects a value that doesn't fit a single byte")
795+
{
796+
json const doc255 = {{"b", json::binary({1, 2}, 255)}};
797+
CHECK(json::from_bson(json::to_bson(doc255))["b"].get_binary().subtype() == 255);
798+
799+
CHECK_THROWS_AS(json::to_bson(json{{"b", json::binary({1, 2}, 256)}}), json::out_of_range);
800+
CHECK_THROWS_WITH_AS(json::to_bson(json{{"b", json::binary({1, 2}, 300)}}), "[json.exception.out_of_range.415] subtype 300 is too large for the BSON binary subtype (max 255)", json::out_of_range);
801+
}
802+
794803
TEST_CASE("BSON input/output_adapters")
795804
{
796805
const json json_representation =

tests/src/unit-msgpack.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,21 @@ TEST_CASE("MessagePack")
15971597
}
15981598
}
15991599

1600+
TEST_CASE("regression test - MessagePack ext type rejects a subtype that doesn't fit a single byte")
1601+
{
1602+
// subtype 0-255 must still round-trip correctly (regression guard, pre-existing behavior)
1603+
CHECK(json::from_msgpack(json::to_msgpack(json::binary({1, 2}, 0))).get_binary().subtype() == 0);
1604+
CHECK(json::from_msgpack(json::to_msgpack(json::binary({1, 2}, 200))).get_binary().subtype() == 200);
1605+
CHECK(json::from_msgpack(json::to_msgpack(json::binary({1, 2}, 255))).get_binary().subtype() == 255);
1606+
1607+
// a subtype > 255 must throw instead of silently truncating
1608+
CHECK_THROWS_AS(json::to_msgpack(json::binary({1, 2}, 256)), json::out_of_range);
1609+
CHECK_THROWS_WITH_AS(json::to_msgpack(json::binary({1, 2}, 70000)), "[json.exception.out_of_range.415] subtype 70000 is too large for the MessagePack ext type (max 255)", json::out_of_range);
1610+
1611+
// a binary value with no subtype at all must be unaffected
1612+
CHECK(json::from_msgpack(json::to_msgpack(json::binary({1, 2}))).get_binary().has_subtype() == false);
1613+
}
1614+
16001615
// use this testcase outside [hide] to run it with Valgrind
16011616
TEST_CASE("single MessagePack roundtrip")
16021617
{

tests/src/unit-regression2.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,30 +1673,4 @@ TEST_CASE("regression test - excessive binary container size honors allow_except
16731673
CHECK(json::from_cbor(std::vector<std::uint8_t> {0x9b, 0, 0, 0, 0, 0, 0, 0, 0x02}, true, false).is_discarded());
16741674
}
16751675

1676-
TEST_CASE("regression test - MessagePack/BSON writers reject binary subtypes that don't fit their wire format")
1677-
{
1678-
// MessagePack: subtype 0-255 must still round-trip correctly (regression guard, pre-existing behavior)
1679-
CHECK(json::from_msgpack(json::to_msgpack(json::binary({1, 2}, 0))).get_binary().subtype() == 0);
1680-
CHECK(json::from_msgpack(json::to_msgpack(json::binary({1, 2}, 200))).get_binary().subtype() == 200);
1681-
CHECK(json::from_msgpack(json::to_msgpack(json::binary({1, 2}, 255))).get_binary().subtype() == 255);
1682-
1683-
// MessagePack: subtype > 255 must now throw instead of silently truncating
1684-
CHECK_THROWS_AS(json::to_msgpack(json::binary({1, 2}, 256)), json::out_of_range);
1685-
CHECK_THROWS_AS(json::to_msgpack(json::binary({1, 2}, 70000)), json::out_of_range);
1686-
CHECK_THROWS_WITH_AS(json::to_msgpack(json::binary({1, 2}, 70000)), "[json.exception.out_of_range.415] subtype 70000 is too large for the MessagePack ext type (max 255)", json::out_of_range);
1687-
1688-
// BSON: same pattern
1689-
json doc255 = {{"b", json::binary({1, 2}, 255)}};
1690-
CHECK(json::from_bson(json::to_bson(doc255))["b"].get_binary().subtype() == 255);
1691-
CHECK_THROWS_AS(json::to_bson(json{{"b", json::binary({1, 2}, 256)}}), json::out_of_range);
1692-
CHECK_THROWS_AS(json::to_bson(json{{"b", json::binary({1, 2}, 300)}}), json::out_of_range);
1693-
CHECK_THROWS_WITH_AS(json::to_bson(json{{"b", json::binary({1, 2}, 300)}}), "[json.exception.out_of_range.415] subtype 300 is too large for the BSON binary subtype (max 255)", json::out_of_range);
1694-
1695-
// CBOR must remain unaffected (full 64-bit subtype range already supported correctly) - regression guard
1696-
CHECK(json::from_cbor(json::to_cbor(json::binary({1, 2}, 70000)), true, true, json::cbor_tag_handler_t::store).get_binary().subtype() == 70000);
1697-
1698-
// a binary value with NO subtype at all must be completely unaffected by this change
1699-
CHECK(json::from_msgpack(json::to_msgpack(json::binary({1, 2}))).get_binary().has_subtype() == false);
1700-
}
1701-
17021676
DOCTEST_CLANG_SUPPRESS_WARNING_POP

0 commit comments

Comments
 (0)