Compare commits

..
Author SHA1 Message Date
Niels Lohmann b44aeaac0a Fix the MSVC and GCC builds of develop (C4127, -Weffc++)
json_view number(): #6006 folded the check of the digit after a minus
sign into `else if (!negative || ...)`, which MSVC 19.44 reports as
C4127 (conditional expression is constant) and /WX turns into an
error in every MSVC and clang-cl job. The check now sits in the
`if (negative)` block, right after the sign is skipped; the error and
its position are unchanged.

unit-locale-cpp.cpp: the locale_switching_token added in #5997 held a
`const char*`, so GCC's -Weffc++ asked for a copy constructor and copy
assignment. The locale name is now a std::string.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-11-02 19:51:33 +01:00
Niels Lohmann 07876e2292 json_view: a value that is not found has no bytes in its exception context (#6015) 2026-11-02 19:11:07 +01:00
10 changed files with 56 additions and 140 deletions

No files matched your search

+12 -32
View File
@@ -1024,7 +1024,7 @@ class binary_reader
{ {
std::uint64_t len{}; std::uint64_t len{};
std::size_t size{}; std::size_t size{};
return get_cbor_argument(len) && get_container_size(len, size, "array") && enter_cbor_array(size); return get_cbor_argument(len) && get_cbor_container_size(len, size, "array") && enter_cbor_array(size);
} }
case 0x9F: // array (indefinite length) case 0x9F: // array (indefinite length)
@@ -1064,7 +1064,7 @@ class binary_reader
{ {
std::uint64_t len{}; std::uint64_t len{};
std::size_t size{}; std::size_t size{};
return get_cbor_argument(len) && get_container_size(len, size, "map") && enter_object(size); return get_cbor_argument(len) && get_cbor_container_size(len, size, "map") && enter_object(size);
} }
case 0xBF: // map (indefinite length) case 0xBF: // map (indefinite length)
@@ -1700,7 +1700,7 @@ class binary_reader
} }
/*! /*!
@brief narrow a definite CBOR or MessagePack array/map length to std::size_t @brief narrow a definite CBOR array/map length to std::size_t
A definite length is rejected if it does not fit in std::size_t or if it A definite length is rejected if it does not fit in std::size_t or if it
equals detail::unknown_size(), which is reserved to mark an indefinite- equals detail::unknown_size(), which is reserved to mark an indefinite-
@@ -1713,7 +1713,7 @@ class binary_reader
@param[in] context "array" or "map", for the error message @param[in] context "array" or "map", for the error message
@return whether the length is usable @return whether the length is usable
*/ */
bool get_container_size(const std::uint64_t len, std::size_t& result, const char* context) bool get_cbor_container_size(const std::uint64_t len, std::size_t& result, const char* context)
{ {
if (JSON_HEDLEY_UNLIKELY(!value_in_range_of<std::size_t>(len) || len == detail::unknown_size())) if (JSON_HEDLEY_UNLIKELY(!value_in_range_of<std::size_t>(len) || len == detail::unknown_size()))
{ {
@@ -2309,8 +2309,7 @@ class binary_reader
case 0xDD: // array 32 case 0xDD: // array 32
{ {
std::uint32_t len{}; std::uint32_t len{};
std::size_t size{}; return get_number(len) && enter_msgpack_array(conditional_static_cast<std::size_t>(len));
return get_number(len) && get_container_size(len, size, "array") && enter_msgpack_array(size);
} }
case 0xDE: // map 16 case 0xDE: // map 16
@@ -2322,8 +2321,7 @@ class binary_reader
case 0xDF: // map 32 case 0xDF: // map 32
{ {
std::uint32_t len{}; std::uint32_t len{};
std::size_t size{}; return get_number(len) && enter_object(conditional_static_cast<std::size_t>(len));
return get_number(len) && get_container_size(len, size, "map") && enter_object(size);
} }
// negative fixint // negative fixint
@@ -3133,27 +3131,6 @@ class binary_reader
return true; return true;
} }
/*!
@brief store the count of an optimized container (or the length of a string)
A count equal to npos is rejected: npos marks a container whose count is
not known, so a count of that value would be read as "no count" (compare
get_container_size()). No container can hold that many elements.
@param[in] count the count read from the input
@param[out] result set to @a count unless it is rejected
@return whether @a count is usable
*/
bool store_ubjson_count(const std::size_t count, std::size_t& result)
{
if (JSON_HEDLEY_UNLIKELY(count == npos))
{
return out_of_range_error(exception_id::container_too_large, "integer value overflow", "size");
}
result = count;
return true;
}
/*! /*!
@brief read a UBJSON/BJData optimized-container count of a signed marker @brief read a UBJSON/BJData optimized-container count of a signed marker
type ('i', 'I', 'l', 'L') and narrow it to std::size_t type ('i', 'I', 'l', 'L') and narrow it to std::size_t
@@ -3184,7 +3161,8 @@ class binary_reader
{ {
return out_of_range_error(exception_id::container_too_large, "integer value overflow", "size"); return out_of_range_error(exception_id::container_too_large, "integer value overflow", "size");
} }
return store_ubjson_count(static_cast<std::size_t>(number), result); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
return true;
} }
/*! /*!
@@ -3261,7 +3239,8 @@ class binary_reader
{ {
return false; return false;
} }
return store_ubjson_count(conditional_static_cast<std::size_t>(number), result); result = conditional_static_cast<std::size_t>(number);
return true;
} }
case 'M': case 'M':
@@ -3279,7 +3258,8 @@ class binary_reader
{ {
return out_of_range_error(exception_id::container_too_large, "integer value overflow", "size"); return out_of_range_error(exception_id::container_too_large, "integer value overflow", "size");
} }
return store_ubjson_count(detail::conditional_static_cast<std::size_t>(number), result); result = detail::conditional_static_cast<std::size_t>(number);
return true;
} }
case '[': case '[':
+6 -6
View File
@@ -1168,20 +1168,20 @@ indent_done:
if (negative) if (negative)
{ {
++p; ++p;
if (NLOHMANN_VIEW_UNLIKELY(cur() < '0' || cur() > '9'))
{
return fail(error_code::number_after_minus);
}
} }
const unsigned char* const int_start = p; const unsigned char* const int_start = p;
if (cur() == '0') if (cur() == '0')
{ {
++p; ++p;
} }
else if (!negative || NLOHMANN_VIEW_LIKELY(cur() >= '1' && cur() <= '9'))
{
// (without a sign, the dispatch saw a digit)
p = skip_digits(p + 1, e);
}
else else
{ {
return fail(error_code::number_after_minus); // (a digit: the dispatch or the check after the sign saw it)
p = skip_digits(p + 1, e);
} }
const auto int_digits = static_cast<std::size_t>(p - int_start); const auto int_digits = static_cast<std::size_t>(p - int_start);
std::size_t frac_digits = 0; std::size_t frac_digits = 0;
+6 -2
View File
@@ -251,8 +251,12 @@ std::string diagnostics(const error_site site)
std::string context; std::string context;
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
std::vector<std::string> tokens; std::vector<std::string> tokens;
// a value that is not found leaves no tokens, like the root const bool found = edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens);
static_cast<void>(edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens)); if (!found)
{
// a value that is not found has no path, and with it no bytes
return context;
}
if (!tokens.empty()) if (!tokens.empty())
{ {
// the root value has no path, but still has a byte range // the root value has no path, but still has a byte range
+12 -32
View File
@@ -19039,7 +19039,7 @@ class binary_reader
{ {
std::uint64_t len{}; std::uint64_t len{};
std::size_t size{}; std::size_t size{};
return get_cbor_argument(len) && get_container_size(len, size, "array") && enter_cbor_array(size); return get_cbor_argument(len) && get_cbor_container_size(len, size, "array") && enter_cbor_array(size);
} }
case 0x9F: // array (indefinite length) case 0x9F: // array (indefinite length)
@@ -19079,7 +19079,7 @@ class binary_reader
{ {
std::uint64_t len{}; std::uint64_t len{};
std::size_t size{}; std::size_t size{};
return get_cbor_argument(len) && get_container_size(len, size, "map") && enter_object(size); return get_cbor_argument(len) && get_cbor_container_size(len, size, "map") && enter_object(size);
} }
case 0xBF: // map (indefinite length) case 0xBF: // map (indefinite length)
@@ -19715,7 +19715,7 @@ class binary_reader
} }
/*! /*!
@brief narrow a definite CBOR or MessagePack array/map length to std::size_t @brief narrow a definite CBOR array/map length to std::size_t
A definite length is rejected if it does not fit in std::size_t or if it A definite length is rejected if it does not fit in std::size_t or if it
equals detail::unknown_size(), which is reserved to mark an indefinite- equals detail::unknown_size(), which is reserved to mark an indefinite-
@@ -19728,7 +19728,7 @@ class binary_reader
@param[in] context "array" or "map", for the error message @param[in] context "array" or "map", for the error message
@return whether the length is usable @return whether the length is usable
*/ */
bool get_container_size(const std::uint64_t len, std::size_t& result, const char* context) bool get_cbor_container_size(const std::uint64_t len, std::size_t& result, const char* context)
{ {
if (JSON_HEDLEY_UNLIKELY(!value_in_range_of<std::size_t>(len) || len == detail::unknown_size())) if (JSON_HEDLEY_UNLIKELY(!value_in_range_of<std::size_t>(len) || len == detail::unknown_size()))
{ {
@@ -20324,8 +20324,7 @@ class binary_reader
case 0xDD: // array 32 case 0xDD: // array 32
{ {
std::uint32_t len{}; std::uint32_t len{};
std::size_t size{}; return get_number(len) && enter_msgpack_array(conditional_static_cast<std::size_t>(len));
return get_number(len) && get_container_size(len, size, "array") && enter_msgpack_array(size);
} }
case 0xDE: // map 16 case 0xDE: // map 16
@@ -20337,8 +20336,7 @@ class binary_reader
case 0xDF: // map 32 case 0xDF: // map 32
{ {
std::uint32_t len{}; std::uint32_t len{};
std::size_t size{}; return get_number(len) && enter_object(conditional_static_cast<std::size_t>(len));
return get_number(len) && get_container_size(len, size, "map") && enter_object(size);
} }
// negative fixint // negative fixint
@@ -21148,27 +21146,6 @@ class binary_reader
return true; return true;
} }
/*!
@brief store the count of an optimized container (or the length of a string)
A count equal to npos is rejected: npos marks a container whose count is
not known, so a count of that value would be read as "no count" (compare
get_container_size()). No container can hold that many elements.
@param[in] count the count read from the input
@param[out] result set to @a count unless it is rejected
@return whether @a count is usable
*/
bool store_ubjson_count(const std::size_t count, std::size_t& result)
{
if (JSON_HEDLEY_UNLIKELY(count == npos))
{
return out_of_range_error(exception_id::container_too_large, "integer value overflow", "size");
}
result = count;
return true;
}
/*! /*!
@brief read a UBJSON/BJData optimized-container count of a signed marker @brief read a UBJSON/BJData optimized-container count of a signed marker
type ('i', 'I', 'l', 'L') and narrow it to std::size_t type ('i', 'I', 'l', 'L') and narrow it to std::size_t
@@ -21199,7 +21176,8 @@ class binary_reader
{ {
return out_of_range_error(exception_id::container_too_large, "integer value overflow", "size"); return out_of_range_error(exception_id::container_too_large, "integer value overflow", "size");
} }
return store_ubjson_count(static_cast<std::size_t>(number), result); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
return true;
} }
/*! /*!
@@ -21276,7 +21254,8 @@ class binary_reader
{ {
return false; return false;
} }
return store_ubjson_count(conditional_static_cast<std::size_t>(number), result); result = conditional_static_cast<std::size_t>(number);
return true;
} }
case 'M': case 'M':
@@ -21294,7 +21273,8 @@ class binary_reader
{ {
return out_of_range_error(exception_id::container_too_large, "integer value overflow", "size"); return out_of_range_error(exception_id::container_too_large, "integer value overflow", "size");
} }
return store_ubjson_count(detail::conditional_static_cast<std::size_t>(number), result); result = detail::conditional_static_cast<std::size_t>(number);
return true;
} }
case '[': case '[':
+12 -8
View File
@@ -2473,20 +2473,20 @@ indent_done:
if (negative) if (negative)
{ {
++p; ++p;
if (NLOHMANN_VIEW_UNLIKELY(cur() < '0' || cur() > '9'))
{
return fail(error_code::number_after_minus);
}
} }
const unsigned char* const int_start = p; const unsigned char* const int_start = p;
if (cur() == '0') if (cur() == '0')
{ {
++p; ++p;
} }
else if (!negative || NLOHMANN_VIEW_LIKELY(cur() >= '1' && cur() <= '9'))
{
// (without a sign, the dispatch saw a digit)
p = skip_digits(p + 1, e);
}
else else
{ {
return fail(error_code::number_after_minus); // (a digit: the dispatch or the check after the sign saw it)
p = skip_digits(p + 1, e);
} }
const auto int_digits = static_cast<std::size_t>(p - int_start); const auto int_digits = static_cast<std::size_t>(p - int_start);
std::size_t frac_digits = 0; std::size_t frac_digits = 0;
@@ -5080,8 +5080,12 @@ std::string diagnostics(const error_site site)
std::string context; std::string context;
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
std::vector<std::string> tokens; std::vector<std::string> tokens;
// a value that is not found leaves no tokens, like the root const bool found = edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens);
static_cast<void>(edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens)); if (!found)
{
// a value that is not found has no path, and with it no bytes
return context;
}
if (!tokens.empty()) if (!tokens.empty())
{ {
// the root value has no path, but still has a byte range // the root value has no path, but still has a byte range
-20
View File
@@ -97,23 +97,3 @@ TEST_CASE("CBOR")
} }
} }
} }
TEST_CASE("MessagePack")
{
SECTION("parse errors")
{
SECTION("array/map size equal to the unknown-size sentinel")
{
// on 32-bit platforms an array 32/map 32 length of 0xFFFFFFFF aliases unknown_size()
std::vector<uint8_t> const varr = {0xDD, 0xFF, 0xFF, 0xFF, 0xFF};
std::vector<uint8_t> const vmap = {0xDF, 0xFF, 0xFF, 0xFF, 0xFF};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(varr), "[json.exception.out_of_range.408] syntax error while parsing MessagePack size: excessive array size", json::out_of_range&);
CHECK(json::from_msgpack(varr, true, false).is_discarded());
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vmap), "[json.exception.out_of_range.408] syntax error while parsing MessagePack size: excessive map size", json::out_of_range&);
CHECK(json::from_msgpack(vmap, true, false).is_discarded());
}
}
}
+3 -23
View File
@@ -3237,32 +3237,12 @@ TEST_CASE("BJData")
#endif #endif
CHECK(json::from_bjdata(vM, true, false).is_discarded()); CHECK(json::from_bjdata(vM, true, false).is_discarded());
// the first dimension is 2^64-1, which is rejected as a count (see below)
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vMX), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
CHECK(json::from_bjdata(vMX, true, false).is_discarded());
}
SECTION("optimized array: count equal to SIZE_MAX")
{
// the reader marks a container without a count by SIZE_MAX, so
// a count of that value must not be read as "no count"
// (it used to parse as [1])
json _;
#if SIZE_MAX != 0xffffffff #if SIZE_MAX != 0xffffffff
std::vector<uint8_t> const vM = {'[', '#', 'M', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 'i', 0x01, ']'}; CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vMX), "[json.exception.out_of_range.408] syntax error while parsing BJData size: excessive ndarray size caused overflow", json::out_of_range&);
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
CHECK(json::from_bjdata(vM, true, false).is_discarded());
// one less is an ordinary excessive count
std::vector<uint8_t> const vM1 = {'[', '#', 'M', 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 'i', 0x01, ']'};
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM1), "[json.exception.out_of_range.408] excessive array size: 18446744073709551614", json::out_of_range&);
#else #else
std::vector<uint8_t> const vm = {'[', '#', 'm', 0xFF, 0xFF, 0xFF, 0xFF, 'i', 0x01, ']'}; CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vMX), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vm), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
CHECK(json::from_bjdata(vm, true, false).is_discarded());
std::vector<uint8_t> const vL = {'[', '#', 'L', 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 'i', 0x01, ']'};
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vL), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
CHECK(json::from_bjdata(vL, true, false).is_discarded());
#endif #endif
CHECK(json::from_bjdata(vMX, true, false).is_discarded());
} }
SECTION("optimized array: integer value overflow") SECTION("optimized array: integer value overflow")
+4 -4
View File
@@ -505,8 +505,8 @@ struct locale_switching_token
{ {
using value_type = char; using value_type = char;
locale_switching_token(std::string text_, const char* switch_to_) locale_switching_token(std::string text_, std::string switch_to_)
: text(std::move(text_)), switch_to(switch_to_) : text(std::move(text_)), switch_to(std::move(switch_to_))
{} {}
std::size_t size() const std::size_t size() const
@@ -523,13 +523,13 @@ struct locale_switching_token
{ {
if (!switched) if (!switched)
{ {
switched = std::setlocale(LC_NUMERIC, switch_to) != nullptr; switched = std::setlocale(LC_NUMERIC, switch_to.c_str()) != nullptr;
} }
return &text[0]; return &text[0];
} }
std::string text; std::string text;
const char* switch_to; std::string switch_to;
bool switched = false; bool switched = false;
}; };
+1 -1
View File
@@ -1009,7 +1009,7 @@ TEST_CASE("regression test - excessive binary container size honors allow_except
// allow_exceptions=true (the default) must still throw exactly as before. // allow_exceptions=true (the default) must still throw exactly as before.
// The exact message text is not checked here: on platforms where // The exact message text is not checked here: on platforms where
// std::size_t is 32-bit, the CBOR reader's own length-narrowing check // std::size_t is 32-bit, the CBOR reader's own length-narrowing check
// (get_container_size(), unrelated to this fix) intercepts a // (get_cbor_container_size(), unrelated to this fix) intercepts a
// declared length of 2^63 before it ever reaches the check this test // declared length of 2^63 before it ever reaches the check this test
// targets, with different (but equally valid, and already correct) // targets, with different (but equally valid, and already correct)
// wording -- see unit-cbor.cpp for coverage of that message. // wording -- see unit-cbor.cpp for coverage of that message.
-12
View File
@@ -2385,18 +2385,6 @@ TEST_CASE("UBJSON optimized arrays of a valueless type are bounded")
} }
} }
SECTION("a count equal to SIZE_MAX is rejected")
{
// the reader marks a container without a count by SIZE_MAX, so a
// count of that value must not be read as "no count"; UBJSON's
// largest count type (int64) can only reach it where size_t is 32 bits
#if SIZE_MAX == 0xffffffff
const std::vector<uint8_t> input = {'[', '#', 'L', 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 'i', 0x01, ']'};
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(input), "[json.exception.out_of_range.408] syntax error while parsing UBJSON size: integer value overflow", json::out_of_range&);
CHECK(json::from_ubjson(input, true, false).is_discarded());
#endif
}
SECTION("ordinary counts are unaffected") SECTION("ordinary counts are unaffected")
{ {
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'Z', '#', 'i', 3})) == json({nullptr, nullptr, nullptr})); CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'Z', '#', 'i', 3})) == json({nullptr, nullptr, nullptr}));