diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 35cfb16b5..2f8adbc58 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -4537,7 +4537,11 @@ class binary_reader { return false; } - return skip_bon8_bytes((byte <= 0xDF) ? 0 : ((byte <= 0xEF) ? 1 : 2)); + if (byte <= 0xDF) + { + return skip_bon8_bytes(0); + } + return skip_bon8_bytes((byte <= 0xEF) ? 1 : 2); } /*! diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index ea9041b04..d9ab6b2a3 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -2519,7 +2519,19 @@ scan_number_done: } const auto lead_byte = static_cast(token_buffer[lead - 1]); - const std::size_t expected = (lead_byte >= 0xF0) ? 3 : (lead_byte >= 0xE0) ? 2 : (lead_byte >= 0xC0) ? 1 : 0; + std::size_t expected = 0; + if (lead_byte >= 0xF0) + { + expected = 3; + } + else if (lead_byte >= 0xE0) + { + expected = 2; + } + else if (lead_byte >= 0xC0) + { + expected = 1; + } if (continuation_bytes >= expected) { return false; @@ -2700,12 +2712,9 @@ scan_number_done: bool fetch = false; if (error_message_starts_with("invalid string: surrogate") - || error_message_starts_with("invalid string: '\\u'")) - { - add_replacement_character(); - } - else if (error_message_starts_with("invalid string: ill-formed UTF-8") - && remove_incomplete_utf8_sequence()) + || error_message_starts_with("invalid string: '\\u'") + || (error_message_starts_with("invalid string: ill-formed UTF-8") + && remove_incomplete_utf8_sequence())) { add_replacement_character(); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f0125217d..beece0a09 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -11685,7 +11685,19 @@ scan_number_done: } const auto lead_byte = static_cast(token_buffer[lead - 1]); - const std::size_t expected = (lead_byte >= 0xF0) ? 3 : (lead_byte >= 0xE0) ? 2 : (lead_byte >= 0xC0) ? 1 : 0; + std::size_t expected = 0; + if (lead_byte >= 0xF0) + { + expected = 3; + } + else if (lead_byte >= 0xE0) + { + expected = 2; + } + else if (lead_byte >= 0xC0) + { + expected = 1; + } if (continuation_bytes >= expected) { return false; @@ -11866,12 +11878,9 @@ scan_number_done: bool fetch = false; if (error_message_starts_with("invalid string: surrogate") - || error_message_starts_with("invalid string: '\\u'")) - { - add_replacement_character(); - } - else if (error_message_starts_with("invalid string: ill-formed UTF-8") - && remove_incomplete_utf8_sequence()) + || error_message_starts_with("invalid string: '\\u'") + || (error_message_starts_with("invalid string: ill-formed UTF-8") + && remove_incomplete_utf8_sequence())) { add_replacement_character(); } @@ -17963,7 +17972,11 @@ class binary_reader { return false; } - return skip_bon8_bytes((byte <= 0xDF) ? 0 : ((byte <= 0xEF) ? 1 : 2)); + if (byte <= 0xDF) + { + return skip_bon8_bytes(0); + } + return skip_bon8_bytes((byte <= 0xEF) ? 1 : 2); } /*! diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index 94d473418..4aa4b144b 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -1026,6 +1026,7 @@ BinaryParseResult parse_binary_recovering(const std::vector& input return {j, sax.errors, sax.messages, ok, sax.balanced()}; } +#if !defined(JSON_NOEXCEPTION) /// the message of the exception that reading @a input into a JSON value /// throws, or an empty string if reading succeeds std::string binary_error_message(const std::vector& input, const json::input_format_t format) @@ -1064,6 +1065,7 @@ std::string binary_error_message(const std::vector& input, const j } return ""; } +#endif /// a BSON element: its type, its name, and its value std::vector bson_element(const std::uint8_t type, const std::string& name, const std::vector& value) @@ -1315,9 +1317,13 @@ TEST_CASE("regression test - #3989 SAX parse_error() returning true") CHECK(result.balanced); CHECK(result.errors == repair.errors); CHECK(result.value == repair.expected); - // the first error is the one reported without recovering REQUIRE(!result.messages.empty()); +#if !defined(JSON_NOEXCEPTION) + // the first error is the one reported without recovering; under + // JSON_NOEXCEPTION, reading without recovering aborts instead of + // throwing, so there is no message to compare with CHECK(result.messages.front() == binary_error_message(repair.input, repair.format)); +#endif } } @@ -1410,14 +1416,17 @@ TEST_CASE("regression test - #3989 SAX parse_error() returning true") const auto result = parse_binary_recovering(input, format); CHECK(result.balanced); CHECK(result.errors <= input.size() + 1); +#if !defined(JSON_NOEXCEPTION) // an error is reported exactly if reading into a JSON value - // fails, and the first one is the same + // fails, and the first one is the same (under JSON_NOEXCEPTION, + // that reading aborts instead of throwing) const auto message = binary_error_message(input, format); CHECK(result.ok == message.empty()); if (!result.ok && result.errors < 100) { CHECK(result.messages.front() == message); } +#endif } } }