Fix fuzzer driver step lists to match the checks the code performs

The header comment of six of the seven binary-format fuzzer drivers
listed an invariant the code does not check: CBOR, MessagePack, BSON
and BON8 said "assert(j1 == j2)", but the code checks byte stability,
assert(to_X(j2) == vec). UBJSON and BJData still described the old
"assert(j1 == j2/j3/j4)" byte-exact check from before PR #5494 replaced
it with a use_size/use_type-aware round trip (UBJSON) and a
value-stability check (BJData); BJData's added paragraph already
explained the new check, but the step list above it did not.

Also remove a dead branch in fuzzer-parse_bson.cpp: from_bson() is
called with allow_exceptions = true, so it throws instead of returning
a discarded value, and the "if (j1.is_discarded()) return 0;" guard
could never trigger. Drop the unused <iostream> include from all seven
drivers and <sstream> from all but fuzzer-parse_bon8.cpp, which is the
only one that uses std::istringstream.

Overlaps #5601, which edits all seven drivers in the same hunks.

Closes #5714 item 4.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-09-30 18:08:00 +02:00
1 parent fbe05f2ed0
commit b2760f273d
7 files changed
+22 -40

No files matched your search

+9 -11
View File
@@ -11,15 +11,15 @@ This file implements a parser test suitable for fuzz testing. Given a byte
array data, it performs the following steps: array data, it performs the following steps:
- j1 = from_bjdata(data) - j1 = from_bjdata(data)
- vec = to_bjdata(j1) - vec2 = to_bjdata(j1, use_size = false, use_type = false)
- j2 = from_bjdata(vec) - vec3 = to_bjdata(j1, use_size = true, use_type = false)
- assert(j1 == j2) - vec4 = to_bjdata(j1, use_size = true, use_type = true)
- vec2 = to_bjdata(j1, use_size = true, use_type = false) - j2 = from_bjdata(vec2)
- j3 = from_bjdata(vec2) - j3 = from_bjdata(vec3)
- assert(j1 == j3) - j4 = from_bjdata(vec4)
- vec3 = to_bjdata(j1, use_size = true, use_type = true) - assert(from_bjdata(to_bjdata(j2, use_size = false, use_type = false)) is value-stable with j2)
- j4 = from_bjdata(vec3) - assert(from_bjdata(to_bjdata(j3, use_size = true, use_type = false)) is value-stable with j3)
- assert(j1 == j4) - assert(from_bjdata(to_bjdata(j4, use_size = true, use_type = true)) is value-stable with j4)
Re-serializing j2/j3/j4 with the same use_size/use_type settings is checked Re-serializing j2/j3/j4 with the same use_size/use_type settings is checked
for value-stability rather than byte-exact stability: from_bjdata(to_bjdata(j2)) for value-stability rather than byte-exact stability: from_bjdata(to_bjdata(j2))
@@ -50,8 +50,6 @@ drivers.
*/ */
#include <cassert> #include <cassert>
#include <iostream>
#include <sstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
// the round-trip checks below are assertions; NDEBUG would compile them away // the round-trip checks below are assertions; NDEBUG would compile them away
+1 -2
View File
@@ -13,7 +13,7 @@ array data, it performs the following steps:
- j1 = from_bon8(data) - j1 = from_bon8(data)
- vec = to_bon8(j1) - vec = to_bon8(j1)
- j2 = from_bon8(vec) - j2 = from_bon8(vec)
- assert(j1 == j2) - assert(to_bon8(j2) == vec)
It also checks that reading the data from a stream, which reads strings byte by It also checks that reading the data from a stream, which reads strings byte by
byte, gives the same value or error as reading it from contiguous memory, which byte, gives the same value or error as reading it from contiguous memory, which
@@ -24,7 +24,6 @@ drivers.
*/ */
#include <cassert> #include <cassert>
#include <iostream>
#include <sstream> #include <sstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
+1 -8
View File
@@ -13,15 +13,13 @@ array data, it performs the following steps:
- j1 = from_bson(data) - j1 = from_bson(data)
- vec = to_bson(j1) - vec = to_bson(j1)
- j2 = from_bson(vec) - j2 = from_bson(vec)
- assert(j1 == j2) - assert(to_bson(j2) == vec)
The provided function `LLVMFuzzerTestOneInput` can be used in different fuzzer The provided function `LLVMFuzzerTestOneInput` can be used in different fuzzer
drivers. drivers.
*/ */
#include <cassert> #include <cassert>
#include <iostream>
#include <sstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
// the round-trip checks below are assertions; NDEBUG would compile them away // the round-trip checks below are assertions; NDEBUG would compile them away
@@ -40,11 +38,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
std::vector<uint8_t> const vec1(data, data + size); std::vector<uint8_t> const vec1(data, data + size);
json const j1 = json::from_bson(vec1); json const j1 = json::from_bson(vec1);
if (j1.is_discarded())
{
return 0;
}
try try
{ {
// step 2: round trip // step 2: round trip
+1 -3
View File
@@ -13,15 +13,13 @@ array data, it performs the following steps:
- j1 = from_cbor(data) - j1 = from_cbor(data)
- vec = to_cbor(j1) - vec = to_cbor(j1)
- j2 = from_cbor(vec) - j2 = from_cbor(vec)
- assert(j1 == j2) - assert(to_cbor(j2) == vec)
The provided function `LLVMFuzzerTestOneInput` can be used in different fuzzer The provided function `LLVMFuzzerTestOneInput` can be used in different fuzzer
drivers. drivers.
*/ */
#include <cassert> #include <cassert>
#include <iostream>
#include <sstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
// the round-trip checks below are assertions; NDEBUG would compile them away // the round-trip checks below are assertions; NDEBUG would compile them away
-2
View File
@@ -21,8 +21,6 @@ drivers.
*/ */
#include <cassert> #include <cassert>
#include <iostream>
#include <sstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
// the round-trip checks below are assertions; NDEBUG would compile them away // the round-trip checks below are assertions; NDEBUG would compile them away
+1 -3
View File
@@ -13,15 +13,13 @@ array data, it performs the following steps:
- j1 = from_msgpack(data) - j1 = from_msgpack(data)
- vec = to_msgpack(j1) - vec = to_msgpack(j1)
- j2 = from_msgpack(vec) - j2 = from_msgpack(vec)
- assert(j1 == j2) - assert(to_msgpack(j2) == vec)
The provided function `LLVMFuzzerTestOneInput` can be used in different fuzzer The provided function `LLVMFuzzerTestOneInput` can be used in different fuzzer
drivers. drivers.
*/ */
#include <cassert> #include <cassert>
#include <iostream>
#include <sstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
// the round-trip checks below are assertions; NDEBUG would compile them away // the round-trip checks below are assertions; NDEBUG would compile them away
+9 -11
View File
@@ -11,15 +11,15 @@ This file implements a parser test suitable for fuzz testing. Given a byte
array data, it performs the following steps: array data, it performs the following steps:
- j1 = from_ubjson(data) - j1 = from_ubjson(data)
- vec = to_ubjson(j1) - vec2 = to_ubjson(j1, use_size = false, use_type = false)
- j2 = from_ubjson(vec) - vec3 = to_ubjson(j1, use_size = true, use_type = false)
- assert(j1 == j2) - vec4 = to_ubjson(j1, use_size = true, use_type = true)
- vec2 = to_ubjson(j1, use_size = true, use_type = false) - j2 = from_ubjson(vec2)
- j3 = from_ubjson(vec2) - j3 = from_ubjson(vec3)
- assert(j1 == j3) - j4 = from_ubjson(vec4)
- vec3 = to_ubjson(j1, use_size = true, use_type = true) - assert(to_ubjson(j2, use_size = false, use_type = false) == vec2)
- j4 = from_ubjson(vec3) - assert(to_ubjson(j3, use_size = true, use_type = false) == vec3)
- assert(j1 == j4) - assert(to_ubjson(j4, use_size = true, use_type = true) == vec4)
The unit tests run the same checks on a fixed corpus (see the "UBJSON round-trip The unit tests run the same checks on a fixed corpus (see the "UBJSON round-trip
invariants" test case), so keep both in sync. invariants" test case), so keep both in sync.
@@ -29,8 +29,6 @@ drivers.
*/ */
#include <cassert> #include <cassert>
#include <iostream>
#include <sstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
// the round-trip checks below are assertions; NDEBUG would compile them away // the round-trip checks below are assertions; NDEBUG would compile them away