mirror of
https://github.com/nlohmann/json.git
synced 2026-10-03 13:10:33 +00:00
Fix CI: clang-tidy and JSON_NOEXCEPTION in the #3989 changes
ci_clang_tidy flagged nested conditional operators in the BON8 skip code and in remove_incomplete_utf8_sequence() (readability-avoid-nested-conditional-operator), and two branches with the same body in recover_string() (bugprone-branch-clone). Use if chains instead of the nested conditionals and merge the two branches into one condition; the short-circuit order is unchanged. ci_test_noexceptions aborted in the #3989 regression test: it compares the first recovered error with the message of the exception that from_cbor() and friends throw, and under JSON_NOEXCEPTION that call aborts instead of throwing. Guard binary_error_message() and its uses with #if !defined(JSON_NOEXCEPTION), as other tests in the suite do. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
@@ -2519,7 +2519,19 @@ scan_number_done:
|
||||
}
|
||||
|
||||
const auto lead_byte = static_cast<unsigned char>(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();
|
||||
}
|
||||
|
||||
@@ -11685,7 +11685,19 @@ scan_number_done:
|
||||
}
|
||||
|
||||
const auto lead_byte = static_cast<unsigned char>(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);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
@@ -1026,6 +1026,7 @@ BinaryParseResult parse_binary_recovering(const std::vector<std::uint8_t>& 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<std::uint8_t>& input, const json::input_format_t format)
|
||||
@@ -1064,6 +1065,7 @@ std::string binary_error_message(const std::vector<std::uint8_t>& input, const j
|
||||
}
|
||||
return "";
|
||||
}
|
||||
#endif
|
||||
|
||||
/// a BSON element: its type, its name, and its value
|
||||
std::vector<std::uint8_t> bson_element(const std::uint8_t type, const std::string& name, const std::vector<std::uint8_t>& 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user