From c6c60dcde23e59633126da25fd820275f39b8e96 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 17 Oct 2026 11:36:04 +0200 Subject: [PATCH] Convert explicitly between bytes and characters in the guard-page tests The clang sanitizer job (-fsanitize=integer, x86-64 where char is signed) reported the implicit char -> unsigned char conversions of std::copy() into the guarded buffer and of the std::string built from the binary inputs. Signed-off-by: Niels Lohmann --- tests/src/unit-guard-page.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/src/unit-guard-page.cpp b/tests/src/unit-guard-page.cpp index bf268449d..a4b12bb1a 100644 --- a/tests/src/unit-guard-page.cpp +++ b/tests/src/unit-guard-page.cpp @@ -87,7 +87,10 @@ class guarded_buffer { REQUIRE(input.size() <= page); unsigned char* const first = base + (2 * page) - input.size(); - std::copy(input.begin(), input.end(), first); + std::transform(input.begin(), input.end(), first, [](const char c) + { + return static_cast(c); + }); return first; } @@ -96,7 +99,10 @@ class guarded_buffer { REQUIRE(input.size() <= page); unsigned char* const first = base + page; - std::copy(input.begin(), input.end(), first); + std::transform(input.begin(), input.end(), first, [](const char c) + { + return static_cast(c); + }); return first; } @@ -288,12 +294,25 @@ JSON_TEST_GUARD_PAGE_READER(read_ubjson, from_ubjson) JSON_TEST_GUARD_PAGE_READER(read_bjdata, from_bjdata) #undef JSON_TEST_GUARD_PAGE_READER +// @a bytes as characters (explicitly converted: the iterator-range constructor +// would convert implicitly, which -fsanitize=integer reports) +std::string as_chars(const std::vector& bytes) +{ + std::string result; + result.reserve(bytes.size()); + for (const std::uint8_t b : bytes) + { + result.push_back(static_cast(b)); + } + return result; +} + // reads every prefix of @a bytes at both ends of the guarded page and from a // std::string, which must give the same outcome template void check_binary(guarded_buffer& guard, const std::vector& bytes, Reader parse) { - for (const auto& input : all_prefixes({std::string(bytes.begin(), bytes.end())})) + for (const auto& input : all_prefixes({as_chars(bytes)})) { CAPTURE(input) const std::string expected = outcome([&](bool allow)