Compare commits

..
Author SHA1 Message Date
Niels Lohmann a05b22ddf3 Let to_json(std::optional<T>) propagate exceptions from T's to_json
The overload was marked noexcept even though its body assigns *opt to
the JSON value, which calls T's to_json (or allocates for std::string,
std::vector, or json). Any exception from there -- a user-defined
to_json reporting an error, or std::bad_alloc -- called std::terminate()
instead of propagating. The noexcept also made basic_json's converting
constructor noexcept(true) for std::optional<T>, so json j = opt; could
not report the error either.

Fixes #5642.
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-09-29 23:17:33 +02:00
6 changed files with 26 additions and 43 deletions
@@ -70,9 +70,6 @@ Strong exception safety: if an exception occurs, the original value stays intact
1. The function can throw the following exceptions:
- Throws [`type_error.305`](../../home/exceptions.md#jsonexceptiontype_error305) if the JSON value is not an array
or null; in that case, using the `[]` operator with an index makes no sense.
- Throws `#!cpp std::length_error` if `idx` equals the maximum value of `size_type`; the array is left unchanged.
(This is the one index for which growing the array to hold it cannot be expressed as a `size_type` size, the same
way an oversized [`resize`](https://en.cppreference.com/w/cpp/container/vector/resize) throws.)
2. The function can throw the following exceptions:
- Throws [`type_error.305`](../../home/exceptions.md#jsonexceptiontype_error305) if the JSON value is not an object
or null; in that case, using the `[]` operator with a key makes no sense.
@@ -260,8 +257,7 @@ Strong exception safety: if an exception occurs, the original value stays intact
## Version history
1. Added in version 1.0.0. Fixed in version 3.13.0 to throw `#!cpp std::length_error` instead of emptying the array and
accessing it out of bounds when `idx` equals the maximum value of `size_type`.
1. Added in version 1.0.0.
2. Added in version 1.0.0. Added overloads for `T* key` in version 1.1.0. Removed overloads for `T* key` (replaced by 3)
in version 3.11.0.
3. Added in version 3.11.0. Fixed in version 3.13.0 to consistently accept `std::string_view`-convertible keys, as
@@ -287,7 +287,7 @@ struct external_constructor<value_t::object>
#ifdef JSON_HAS_CPP_17
template<typename BasicJsonType, typename T,
enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
void to_json(BasicJsonType& j, const std::optional<T>& opt)
{
if (opt.has_value())
{
-9
View File
@@ -36,9 +36,7 @@
#include <iosfwd> // istream, ostream
#endif // JSON_NO_IO
#include <iterator> // make_move_iterator, random_access_iterator_tag
#include <limits> // numeric_limits
#include <memory> // unique_ptr
#include <stdexcept> // length_error
#include <string> // string, stoi, to_string
#include <utility> // declval, forward, move, pair, swap
#include <vector> // vector
@@ -2832,13 +2830,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// fill up the array with null values if given idx is outside the range
if (idx >= m_data.m_value.array->size())
{
// idx + 1 would overflow size_type and wrap to 0, which would empty
// the array instead of growing it; reject such an idx the same way
// resize() rejects other indices that are too large to represent
if (JSON_HEDLEY_UNLIKELY(idx == (std::numeric_limits<size_type>::max)()))
{
JSON_THROW(std::length_error(detail::concat("array index ", std::to_string(idx), " exceeds size_type")));
}
#if JSON_DIAGNOSTICS
// remember array size & capacity before resizing
const auto old_size = m_data.m_value.array->size();
+1 -10
View File
@@ -36,9 +36,7 @@
#include <iosfwd> // istream, ostream
#endif // JSON_NO_IO
#include <iterator> // make_move_iterator, random_access_iterator_tag
#include <limits> // numeric_limits
#include <memory> // unique_ptr
#include <stdexcept> // length_error
#include <string> // string, stoi, to_string
#include <utility> // declval, forward, move, pair, swap
#include <vector> // vector
@@ -6818,7 +6816,7 @@ struct external_constructor<value_t::object>
#ifdef JSON_HAS_CPP_17
template<typename BasicJsonType, typename T,
enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
void to_json(BasicJsonType& j, const std::optional<T>& opt)
{
if (opt.has_value())
{
@@ -28913,13 +28911,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// fill up the array with null values if given idx is outside the range
if (idx >= m_data.m_value.array->size())
{
// idx + 1 would overflow size_type and wrap to 0, which would empty
// the array instead of growing it; reject such an idx the same way
// resize() rejects other indices that are too large to represent
if (JSON_HEDLEY_UNLIKELY(idx == (std::numeric_limits<size_type>::max)()))
{
JSON_THROW(std::length_error(detail::concat("array index ", std::to_string(idx), " exceeds size_type")));
}
#if JSON_DIAGNOSTICS
// remember array size & capacity before resizing
const auto old_size = m_data.m_value.array->size();
+23
View File
@@ -1826,6 +1826,16 @@ TEST_CASE("std::u8string")
#endif
#endif
// a type whose to_json reports an error by throwing, used below to check that
// converting a std::optional<T> to JSON propagates an exception thrown while
// converting its contained value instead of calling std::terminate (#5642)
struct throwing_to_json_type {};
void to_json(json&, const throwing_to_json_type&)
{
throw std::runtime_error("cannot serialize throwing_to_json_type");
}
TEST_CASE("std::optional")
{
SECTION("null")
@@ -1908,6 +1918,19 @@ TEST_CASE("std::optional")
CHECK(json(opt_object) == j_object);
CHECK(std::map<std::string, std::optional<int>>(j_object) == opt_object);
}
#if !defined(JSON_NOEXCEPTION)
SECTION("exception from contained value's to_json propagates (#5642)")
{
// to_json(BasicJsonType&, const std::optional<T>&) must not be
// noexcept: it calls T's to_json, which may throw (a user-defined
// to_json that reports an error, or std::bad_alloc for T =
// std::string/vector/json). Before the fix, this called
// std::terminate() instead of letting the exception propagate.
const std::optional<throwing_to_json_type> opt = throwing_to_json_type{};
CHECK_THROWS_WITH_AS(json(opt), "cannot serialize throwing_to_json_type", std::runtime_error&);
}
#endif
}
#endif
-18
View File
@@ -147,24 +147,6 @@ TEST_CASE("element access 1")
CHECK(j_const[7] == json({1, 2, 3}));
}
SECTION("SIZE_MAX index (#5647)")
{
// idx + 1 must not be computed for idx == SIZE_MAX: it wraps to 0,
// which would empty the array and then write out of bounds instead
// of growing it; reject it like an oversized resize() would and
// leave the array unchanged
const auto max_idx = (std::numeric_limits<json::size_type>::max)();
const std::string expected = "array index " + std::to_string(max_idx) + " exceeds size_type";
const json j_before = j; // NOLINT(performance-unnecessary-copy-initialization)
CHECK_THROWS_WITH_AS(j[max_idx] = 1, expected.c_str(), std::length_error&);
CHECK(j == j_before);
json j_empty = json::array();
CHECK_THROWS_WITH_AS(j_empty[max_idx] = 1, expected.c_str(), std::length_error&);
CHECK(j_empty == json::array());
}
SECTION("access on non-array type")
{
SECTION("null")