From 97a3d550c23d48ca532ebd13606170f1c109e755 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 30 May 2022 14:30:36 -0700 Subject: [PATCH] Fix stack overflow in debug builds in intrusive list dtor When destroying large intrusive lists use iteration instead of recursion. --- include/cppast/detail/intrusive_list.hpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/include/cppast/detail/intrusive_list.hpp b/include/cppast/detail/intrusive_list.hpp index ac83239..a6ef5fb 100644 --- a/include/cppast/detail/intrusive_list.hpp +++ b/include/cppast/detail/intrusive_list.hpp @@ -20,13 +20,31 @@ namespace detail template class intrusive_list_node { - std::unique_ptr next_; + public: + intrusive_list_node() = default; + intrusive_list_node(intrusive_list_node&&) = default; + intrusive_list_node& operator=(intrusive_list_node&&) = default; + + ~intrusive_list_node() noexcept + { + // Free iteratively to avoid stack overflow in debug builds. + auto next = next_.release(); + while (next) + { + std::unique_ptr cur(next); + next = cur->next_.release(); + } + } + + private: void do_on_insert(const T& parent) noexcept { static_cast(*this).on_insert(parent); } + std::unique_ptr next_; + template friend struct intrusive_list_access; };