From 7d4f2f4ec0dbcb9dc4d11fdae4b1ed0dfa1f5569 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 21 Jul 2022 14:50:48 +0200 Subject: [PATCH] :rotating_light: suppress UBSAN warning --- tests/src/unit-deserialization.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/src/unit-deserialization.cpp b/tests/src/unit-deserialization.cpp index 06ae503e1..f716b1dcd 100644 --- a/tests/src/unit-deserialization.cpp +++ b/tests/src/unit-deserialization.cpp @@ -166,6 +166,21 @@ struct SaxEventLoggerExitAfterStartArray : public SaxEventLogger }; } // namespace +// Passing a NULL pointer to the input adapter violates its NON_NULL attribute which is detected by UBSAN. +// To still test whether exceptions are thrown, we need to exclude these tests from UBSAN which can only +// be done with a function attribute. See +// https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#disabling-instrumentation-with-attribute-no-sanitize-undefined +#if defined(__clang__) +__attribute__((no_sanitize("undefined"))) +#endif +void test_file_exception() +{ + std::FILE* f = std::fopen("nonexisting_file", "r"); // NOLINT(cppcoreguidelines-owning-memory) + json _; + CHECK_THROWS_WITH_AS(_ = json::parse(f), "[json.exception.parse_error.116] parse error: input file is invalid", json::parse_error&); + CHECK_THROWS_WITH_AS(_ = json::accept(f), "[json.exception.parse_error.116] parse error: input file is invalid", json::parse_error&); +} + TEST_CASE("deserialization") { SECTION("successful deserialization") @@ -335,10 +350,7 @@ TEST_CASE("deserialization") SECTION("FILE*") { - std::FILE* f = std::fopen("nonexisting_file", "r"); // NOLINT(cppcoreguidelines-owning-memory) - json _; - CHECK_THROWS_WITH_AS(_ = json::parse(f), "[json.exception.parse_error.116] parse error: input file is invalid", json::parse_error&); - CHECK_THROWS_WITH_AS(_ = json::accept(f), "[json.exception.parse_error.116] parse error: input file is invalid", json::parse_error&); + test_file_exception(); } }