Fix CI for the parse() bad_alloc sweep test (#5985)

- sweep_allocator: provide max_size(), construct() and destroy(), which
  GCC 4.8's std::map calls on the allocator directly.
- Declare the non-throwing test lambdas noexcept (-Werror=noexcept on
  recent GCC).
- Give the sax_parse lambda an explicit return type; Clang 3.4 crashes on
  the deduced one in C++11 mode.
- Skip the parser callback sweep with iterator debugging (MSVC debug
  builds, clang-cl), where containers allocate debug proxies inside
  noexcept move constructors and terminate instead of throwing, as in the
  other allocator tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N5NasAgbDGG28MqXjQvLqp
Signed-off-by: Claude <noreply@anthropic.com>
This commit is contained in:
Claude committed 2026-11-02 05:51:08 +00:00
1 parent c82bd8b39a
commit 8aff27a0f9
1 file changed
+30 -3
+30 -3
View File
@@ -12,6 +12,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using nlohmann::json; using nlohmann::json;
#include <limits>
#include <valarray> #include <valarray>
#if JSON_HAS_RANGES #if JSON_HAS_RANGES
#include <ranges> #include <ranges>
@@ -790,7 +791,7 @@ TEST_CASE("a failed allocation leaves the value unchanged")
#if JSON_HAS_RANGES && !defined(__MINGW32__) #if JSON_HAS_RANGES && !defined(__MINGW32__)
const std::vector<int> numbers = {1, 2}; const std::vector<int> numbers = {1, 2};
next_construct_fails = true; next_construct_fails = true;
CHECK_THROWS_AS(nlohmann::to_json(j, numbers | std::views::filter([](int /*unused*/) CHECK_THROWS_AS(nlohmann::to_json(j, numbers | std::views::filter([](int /*unused*/) noexcept
{ {
return true; return true;
})), std::bad_alloc&); })), std::bad_alloc&);
@@ -837,6 +838,26 @@ struct sweep_allocator
std::allocator<T>().deallocate(p, n); std::allocator<T>().deallocate(p, n);
} }
// GCC 4.8's std::map calls these on the allocator directly instead of
// going through std::allocator_traits
std::size_t max_size() const noexcept
{
return (std::numeric_limits<std::size_t>::max)() / sizeof(T);
}
template<class U, class... Args>
void construct(U* p, Args&& ... args) noexcept(noexcept(::new (static_cast<void*>(p)) U(std::forward<Args>(args)...)))
{
::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
}
template<class U>
void destroy(U* p) noexcept
{
static_cast<void>(p); // unused for trivially destructible types
p->~U();
}
template <class U> template <class U>
struct rebind struct rebind
{ {
@@ -912,9 +933,14 @@ TEST_CASE("parse() survives a failing allocation at every point (#5985)")
}); });
} }
// With iterator debugging (MSVC STL debug builds, also used by clang-cl),
// containers allocate a debug proxy through the allocator inside their
// noexcept move constructors, so failing that allocation terminates the
// program instead of throwing std::bad_alloc. Nothing to check there.
#if !(defined(_ITERATOR_DEBUG_LEVEL) && _ITERATOR_DEBUG_LEVEL > 0)
SECTION("parser callback") SECTION("parser callback")
{ {
const auto keep_all = [](int /*depth*/, sweep_json::parse_event_t /*event*/, sweep_json& /*parsed*/) const auto keep_all = [](int /*depth*/, sweep_json::parse_event_t /*event*/, sweep_json& /*parsed*/) noexcept
{ {
return true; return true;
}; };
@@ -932,12 +958,13 @@ TEST_CASE("parse() survives a failing allocation at every point (#5985)")
return sweep_json::parse(text, drop_d); return sweep_json::parse(text, drop_d);
}); });
} }
#endif
SECTION("sax_parse into an existing value") SECTION("sax_parse into an existing value")
{ {
// a failed parse leaves the target in a state that is safe to destroy // a failed parse leaves the target in a state that is safe to destroy
using sax_t = nlohmann::detail::json_sax_dom_parser<sweep_json, nlohmann::detail::iterator_input_adapter<const char*>>; using sax_t = nlohmann::detail::json_sax_dom_parser<sweep_json, nlohmann::detail::iterator_input_adapter<const char*>>;
sweep_allocations_of<sweep_json>([&] sweep_allocations_of<sweep_json>([&]() -> sweep_json
{ {
sweep_json value; sweep_json value;
sax_t sax(value, false); sax_t sax(value, false);