From db9da74aacb4b92409198e7e7d2b02687d22ad35 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Tue, 10 Nov 2026 16:05:49 +0100 Subject: [PATCH 1/2] Return an error when an rfl::Skip field's default value throws ParserSkip::read is noexcept. For a field that is skipped when reading, it constructs the default value of the wrapped type, which throws for a Validator whose default value is invalid, such as rfl::Skip>>. Reading a struct with such a field in the input called std::terminate. Catch the exception and return it as the field's error. Co-Authored-By: Claude Fable 5.2 --- include/rfl/parsing/ParserSkip.hpp | 11 +++++++++-- tests/json/test_skip_throwing_default.cpp | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/json/test_skip_throwing_default.cpp diff --git a/include/rfl/parsing/ParserSkip.hpp b/include/rfl/parsing/ParserSkip.hpp index 6d748ba04..5b31767f6 100644 --- a/include/rfl/parsing/ParserSkip.hpp +++ b/include/rfl/parsing/ParserSkip.hpp @@ -1,6 +1,7 @@ #ifndef RFL_PARSING_PARSER_SKIP_HPP_ #define RFL_PARSING_PARSER_SKIP_HPP_ +#include #include #include @@ -39,8 +40,14 @@ struct ParserSkip { static Result> read(const R& _r, const InputVarType& _var) noexcept { if constexpr (_skip_deserialization) { - return internal::Skip( - std::remove_cvref_t()); + // The default value can throw, for instance a Validator whose default + // value is invalid. + try { + return internal::Skip( + std::remove_cvref_t()); + } catch (std::exception& e) { + return error(e.what()); + } } else { const auto to_skip = [&](auto&& _t) { return internal::Skip( diff --git a/tests/json/test_skip_throwing_default.cpp b/tests/json/test_skip_throwing_default.cpp new file mode 100644 index 000000000..a8d530060 --- /dev/null +++ b/tests/json/test_skip_throwing_default.cpp @@ -0,0 +1,23 @@ +#include + +#include +#include + +namespace test_skip_throwing_default { + +using Positive = rfl::Validator>; + +struct WithSkip { + rfl::Skip s; + int y; +}; + +TEST(json, test_skip_throwing_default) { + const auto res = rfl::json::read(R"({"y": 1, "s": 5})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Failed to parse field 's': Value expected to be greater than or " + "equal to 1, but got 0."); +} + +} // namespace test_skip_throwing_default From 3b35bb8fd3485d84790c8d0f4033861645b12267 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Wed, 11 Nov 2026 08:15:14 +0100 Subject: [PATCH 2/2] Return an error when the default value of a missing field throws NamedTupleParser::handle_one_missing_field and TupleReader::handle_missing_fields_impl are noexcept. For a field that is not required and missing from the input, they construct the default value in place, which throws for a type such as rfl::Skip>>. Reading a struct, a named tuple or a tuple without that field called std::terminate. Catch the exception and report it as the field's error. The field is not marked as set, so its destructor is not run. Types that are nothrow default constructible are constructed as before. Co-Authored-By: Claude Fable 5.2 --- include/rfl/parsing/NamedTupleParser.hpp | 24 +++++-- include/rfl/parsing/TupleReader.hpp | 19 ++++- tests/json/test_skip_throwing_default.cpp | 84 +++++++++++++++++++++++ 3 files changed, 122 insertions(+), 5 deletions(-) diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index c4e2fd312..835e7abc7 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -2,6 +2,7 @@ #define RFL_PARSING_NAMEDTUPLEPARSER_HPP_ #include +#include #include #include #include @@ -353,11 +354,26 @@ struct NamedTupleParser { make_nested_error(std::string(current_name), stream.str())); } else if constexpr (!internal::has_default_val_v) { - if constexpr (!std::is_const_v) { - ::new (rfl::get<_i>(_view)) ValueType(); + using NonConstT = std::remove_const_t; + const auto ptr = const_cast(rfl::get<_i>(_view)); + if constexpr (std::is_nothrow_default_constructible_v) { + ::new (ptr) NonConstT(); } else { - using NonConstT = std::remove_const_t; - ::new (const_cast(rfl::get<_i>(_view))) NonConstT(); + // The default value can throw, for instance a Validator whose + // default value is invalid. + try { + ::new (ptr) NonConstT(); + } catch (std::exception& e) { + constexpr auto current_name = + internal::nth_element_t<_i, FieldTypes...>::name(); + _errors->emplace_back(make_nested_error( + std::string(current_name), + "Field named '" + std::string(current_name) + + "' not found, and its default value could not be " + "constructed: " + + e.what())); + return; + } } std::get<_i>(*_set) = true; } diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index bc48a8700..dab0c7fdf 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -1,6 +1,7 @@ #ifndef RFL_PARSING_TUPLEREADER_HPP_ #define RFL_PARSING_TUPLEREADER_HPP_ +#include #include #include #include @@ -80,9 +81,25 @@ class TupleReader { static_cast(_i), "Field " + std::to_string(_i) + " was required, but missing."); return; - } else { + } else if constexpr (std::is_nothrow_default_constructible_v< + CurrentType>) { ::new (&(rfl::get<_i>(*tuple_))) CurrentType(); ++num_set_; + } else { + // The default value can throw, for instance a Validator whose + // default value is invalid. + try { + ::new (&(rfl::get<_i>(*tuple_))) CurrentType(); + ++num_set_; + } catch (std::exception& e) { + *_err = make_nested_error( + static_cast(_i), + "Field " + std::to_string(_i) + + " was missing, and its default value could not be " + "constructed: " + + e.what()); + return; + } } } handle_missing_fields_impl<_i + 1>(_err); diff --git a/tests/json/test_skip_throwing_default.cpp b/tests/json/test_skip_throwing_default.cpp index a8d530060..652eff917 100644 --- a/tests/json/test_skip_throwing_default.cpp +++ b/tests/json/test_skip_throwing_default.cpp @@ -1,17 +1,48 @@ #include +#include #include #include +#include +#include +#include namespace test_skip_throwing_default { using Positive = rfl::Validator>; +using Path = std::vector; + struct WithSkip { rfl::Skip s; int y; }; +struct SkipLast { + int y; + rfl::Skip s; +}; + +/// Not required, because its ReflectionType is a std::optional, and counts +/// the instances that are alive. +struct ThrowingDefault { + using ReflectionType = std::optional; + + static inline int alive = 0; + + ThrowingDefault() { throw std::runtime_error("no default"); } + ThrowingDefault(const ReflectionType&) { ++alive; } + ThrowingDefault(const ThrowingDefault&) { ++alive; } + ~ThrowingDefault() { --alive; } + + ReflectionType reflection() const { return std::nullopt; } +}; + +struct WithThrowingDefault { + int y; + ThrowingDefault t; +}; + TEST(json, test_skip_throwing_default) { const auto res = rfl::json::read(R"({"y": 1, "s": 5})"); ASSERT_FALSE(res); @@ -20,4 +51,57 @@ TEST(json, test_skip_throwing_default) { "equal to 1, but got 0."); } +TEST(json, test_skip_throwing_default_field_absent) { + const auto res = rfl::json::read(R"({"y": 1})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field named 's' not found, and its default value could not be " + "constructed: Value expected to be greater than or equal to 1, " + "but got 0."); + const auto details = res.error().details(); + ASSERT_EQ(details.size(), 1); + EXPECT_EQ(details[0].path, (Path{"s"})); + EXPECT_EQ(details[0].message, res.error().what()); +} + +TEST(json, test_skip_throwing_default_field_absent_no_field_names) { + const auto res = rfl::json::read(R"([1])"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field named 's' not found, and its default value could not be " + "constructed: Value expected to be greater than or equal to 1, " + "but got 0."); +} + +TEST(json, test_skip_throwing_default_tuple_element_absent) { + const auto res = + rfl::json::read>>(R"([1])"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field 1 was missing, and its default value could not be " + "constructed: Value expected to be greater than or equal to 1, " + "but got 0."); + const auto details = res.error().details(); + ASSERT_EQ(details.size(), 1); + EXPECT_EQ(details[0].path, (Path{size_t{1}})); +} + +// No destructor may run for the field that could not be constructed. +TEST(json, test_skip_throwing_default_does_not_destroy_the_field) { + const auto res = rfl::json::read(R"({"y": 1})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field named 't' not found, and its default value could not be " + "constructed: no default"); + EXPECT_EQ(ThrowingDefault::alive, 0); + + const auto tup = + rfl::json::read>(R"([1])"); + ASSERT_FALSE(tup); + EXPECT_EQ(ThrowingDefault::alive, 0); + + ASSERT_TRUE(rfl::json::read(R"({"y": 1, "t": 2})")); + EXPECT_EQ(ThrowingDefault::alive, 0); +} + } // namespace test_skip_throwing_default