From 613a9976941103bae8cac251f00d28ca3e5e9853 Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Mon, 6 Jun 2022 15:40:57 -0700 Subject: [PATCH] Fix `-Wmaybe-unitialized` warning in gcc-12 by laundering the result of reinterpret_cast<> in `functions` helper --- .../boost/unordered/detail/implementation.hpp | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/include/boost/unordered/detail/implementation.hpp b/include/boost/unordered/detail/implementation.hpp index 6d25b029..544c2d3e 100644 --- a/include/boost/unordered/detail/implementation.hpp +++ b/include/boost/unordered/detail/implementation.hpp @@ -1548,6 +1548,21 @@ namespace boost { // If an exception is thrown between these two calls, use // 'cleanup_spare_functions' to destroy the unused constructed functions. +#if defined(_GLIBCXX_HAVE_BUILTIN_LAUNDER) + // gcc-12 warns when accessing the `current_functions` of our `functions` + // class below with `-Wmaybe-unitialized`. By laundering the pointer, we + // silence the warning and assure the compiler that a valid object exists + // in that region of storage. This warning is also generated in C++03 + // which does not have `std::launder`. The compiler builtin is always + // available, regardless of the C++ standard used when compiling. + template T* launder(T* p) BOOST_NOEXCEPT + { + return __builtin_launder(p); + } +#else + template T* launder(T* p) BOOST_NOEXCEPT { return p; } +#endif + template class functions { public: @@ -1604,14 +1619,16 @@ namespace boost { function_pair const& current_functions() const { - return *static_cast( - static_cast(funcs_[current_ & 1].address())); + return *::boost::unordered::detail::launder( + static_cast( + static_cast(funcs_[current_ & 1].address()))); } function_pair& current_functions() { - return *static_cast( - static_cast(funcs_[current_ & 1].address())); + return *::boost::unordered::detail::launder( + static_cast( + static_cast(funcs_[current_ & 1].address()))); } void construct_spare_functions(function_pair const& f)