From 58679a7134c3f4de367aff54f92c1154bcc2b5c9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 30 Sep 2026 00:00:57 +0200 Subject: [PATCH] Fix CI: image tests with GCC on Linux, without exceptions, and on clang 3.6 - ci_test_gcc (Linux x86-64): static_cast(header_field(...)) was a useless cast (std::uint64_t is std::size_t there), and returning std::mt19937::result_type (std::uint_fast32_t, unsigned long there) as std::uint32_t failed -Werror=conversion. Cast named variables instead. - ci_test_noexceptions: the error, check, and damaged-image tests test the exceptions of load() and save() and catch outside a CHECK_THROWS, which aborts with JSON_NOEXCEPTION; compile them and their helpers only with exceptions. - clang 3.6: value-initialize a const json_document (no user-provided default constructor, CWG 253). - Format the image fuzzer with the pinned astyle, which the "check" job runs over tests/. Signed-off-by: Niels Lohmann --- tests/src/fuzzer-json_view_image.cpp | 10 ++++++++-- tests/src/unit-json_view_image.cpp | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/tests/src/fuzzer-json_view_image.cpp b/tests/src/fuzzer-json_view_image.cpp index 39c340d2e..8495468a4 100644 --- a/tests/src/fuzzer-json_view_image.cpp +++ b/tests/src/fuzzer-json_view_image.cpp @@ -41,7 +41,10 @@ using image_check = json_document::image_check; extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { // the input as an image - for (const image_check check : {image_check::full, image_check::bounds}) + for (const image_check check : + { + image_check::full, image_check::bounds + }) { json_document d; try @@ -79,7 +82,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) if (!parsed.is_discarded()) { const std::vector image = parsed.save(); - for (const image_check check : {image_check::full, image_check::bounds, image_check::none}) + for (const image_check check : + { + image_check::full, image_check::bounds, image_check::none + }) { const json_document loaded = json_document::load(image, check); assert(loaded.root().dump() == parsed.root().dump()); diff --git a/tests/src/unit-json_view_image.cpp b/tests/src/unit-json_view_image.cpp index b2fb05a4b..1133febc7 100644 --- a/tests/src/unit-json_view_image.cpp +++ b/tests/src/unit-json_view_image.cpp @@ -36,6 +36,7 @@ using nlohmann::detail::view::node; namespace { +#if !defined(JSON_NOEXCEPTION) std::string exception_of(const std::function& f) { try @@ -50,6 +51,7 @@ std::string exception_of(const std::function& f) } const char* const check_failed = "[json.exception.parse_error.116] parse error: invalid json_document image: the check failed"; +#endif std::string read_file(const std::string& name) { @@ -76,7 +78,8 @@ void set_header_field(std::vector& image, std::size_t offset, std: std::size_t node_count(const std::vector& image) { - return static_cast(header_field(image, 8)); + const std::uint64_t count = header_field(image, 8); + return static_cast(count); } std::size_t text_at(const std::vector& image) @@ -96,6 +99,7 @@ void set_node(std::vector& image, std::size_t i, const node& n) std::memcpy(image.data() + header_size + (i * sizeof(node)), &n, sizeof(node)); } +#if !defined(JSON_NOEXCEPTION) /// the result of loading an image with a check: "" or the exception message std::string load_result(const std::vector& image, image_check check) { @@ -116,6 +120,7 @@ std::vector corrupted(const std::vector& image, std: set_node(b, i, n); return b; } +#endif /// a document and the documents loaded from its image must be equal template @@ -142,7 +147,9 @@ void check_round_trip(const Document& d) std::uint32_t rng() { static std::mt19937 generator(5295); // NOLINT(cert-msc32-c,cert-msc51-cpp,bugprone-random-generator-seed): reproducible - return generator(); + // result_type is std::uint_fast32_t, which may be wider than 32 bits + const std::mt19937::result_type value = generator(); + return static_cast(value); } } // namespace @@ -353,6 +360,8 @@ TEST_CASE("json_view images: ownership") } } +// the remaining tests are about the exceptions of load() and save() +#if !defined(JSON_NOEXCEPTION) TEST_CASE("json_view images: errors") { SECTION("a literal as the root: dump() after loading") @@ -373,7 +382,7 @@ TEST_CASE("json_view images: errors") SECTION("saving a discarded document") { - const json_document empty; + const json_document empty{}; CHECK(exception_of([&] { static_cast(empty.save()); }) == "[json.exception.type_error.320] cannot save a discarded json_document"); const json_document failed = json_document::parse("[1,", false); CHECK(exception_of([&] { static_cast(failed.save()); }) == "[json.exception.type_error.320] cannot save a discarded json_document"); @@ -779,6 +788,7 @@ TEST_CASE("json_view images: damaged images") } } } +#endif #else