mirror of
https://github.com/nlohmann/json.git
synced 2026-10-04 21:50:33 +00:00
Add deprecated from_bon8/from_bjdata(ptr, len) overloads
from_bon8(ptr, len) and from_bjdata(ptr, len) had no overload for a pointer and a length, unlike from_cbor/from_msgpack/from_ubjson/ from_bson. The call instead bound to from_*(InputType&&, bool strict), which read ptr as a NUL-terminated C string via strlen and silently converted len to the strict flag. Data containing a 0x00 byte was cut off there; data without one was read past the end of the buffer. Add a deprecated (ptr, len, strict, allow_exceptions) overload for each function that forwards to (ptr, ptr + len, ...), matching the existing deprecated overloads of the other four binary readers. Since neither function ever had this overload, the deprecation is declared as of version 3.13.0, the next unreleased version, rather than the version each function was originally added in. Fixes #5648. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -4572,3 +4572,31 @@ TEST_CASE("BJData roundtrips" * doctest::skip())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("issue #5648 - from_bjdata(ptr, len) must read len bytes, not treat ptr as a C string")
|
||||
{
|
||||
// to_bjdata() encodes the integer 0 as the two bytes 'i' 0x00 (a BJData
|
||||
// type marker followed by the value byte 0x00), so the packed data
|
||||
// below contains a 0x00 byte before its end.
|
||||
const json j = {{"a", 0}};
|
||||
const std::vector<std::uint8_t> packed = json::to_bjdata(j);
|
||||
bool contains_nul = false;
|
||||
for (const auto byte : packed)
|
||||
{
|
||||
contains_nul |= (byte == 0x00);
|
||||
}
|
||||
REQUIRE(contains_nul);
|
||||
|
||||
// before the fix, from_bjdata had no (ptr, len) overload, so this call
|
||||
// bound to from_bjdata(InputType&&, bool strict) instead: ptr was read
|
||||
// as a NUL-terminated C string (stopping at the embedded 0x00 byte), and
|
||||
// len was silently converted to the strict flag. The deprecated
|
||||
// overload added for this issue forwards to from_bjdata(ptr, ptr + len,
|
||||
// ...) instead, like from_ubjson's deprecated (ptr, len) overload does.
|
||||
json result;
|
||||
CHECK_NOTHROW(result = json::from_bjdata(packed.data(), packed.size()));
|
||||
CHECK(result == j);
|
||||
|
||||
// len must not collapse into the strict flag either
|
||||
CHECK(json::from_bjdata(packed.data(), packed.size(), false) == j);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user