From 373005f7accebb2eff11d2837c468094e3116b38 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 27 Sep 2026 20:57:16 +0200 Subject: [PATCH] Fix MSVC: avoid reserving by faked size in MessagePack size tests (#5604) The "Size above uint32" tests for arrays and objects fake a container size of 2^32 and expect to_msgpack() to throw out_of_range.412. But to_msgpack(j) first reserves binary_reserve_hint(j) bytes, which is size + 1 for arrays and 2 * size + 1 for objects, i.e. 4 or 8 GiB. Linux and macOS overcommit, so the reservation succeeds; on Windows it throws std::bad_alloc before the size check is reached (seen with msvc-vs2026 Debug x64 on the object test). Write into a caller-owned vector instead, so nothing is reserved. Signed-off-by: Niels Lohmann --- tests/src/unit-msgpack.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/src/unit-msgpack.cpp b/tests/src/unit-msgpack.cpp index 077073052..de4255b4a 100644 --- a/tests/src/unit-msgpack.cpp +++ b/tests/src/unit-msgpack.cpp @@ -2208,8 +2208,11 @@ TEST_CASE("MessagePack Size above uint32 for array") auto& array = j.get_ref(); array.fake_size = true; + // write into a caller-owned vector: to_msgpack(j) reserves space based on + // the (faked) element count, which fails with bad_alloc on Windows + std::vector result; CHECK_THROWS_WITH_AS( - huge_array_json::to_msgpack(j), + huge_array_json::to_msgpack(j, result), "[json.exception.out_of_range.412] MessagePack length 4294967296 exceeds maximum of 4294967295", json::out_of_range&); @@ -2261,8 +2264,11 @@ TEST_CASE("MessagePack Size above uint32 for object") auto& object = j.get_ref(); object.fake_size = true; + // write into a caller-owned vector: to_msgpack(j) reserves space based on + // the (faked) element count, which fails with bad_alloc on Windows + std::vector result; CHECK_THROWS_WITH_AS( - huge_object_json::to_msgpack(j), + huge_object_json::to_msgpack(j, result), "[json.exception.out_of_range.412] MessagePack length 4294967296 exceeds maximum of 4294967295", json::out_of_range&);