From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 98C293858405; Mon, 9 May 2022 16:40:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 98C293858405 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r9-10063] libstdc++: Fix std::exception_ptr regressions [PR103630] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-9 X-Git-Oldrev: 740e1ae17770d8e4829714c689f5471d235184b7 X-Git-Newrev: 93a621991615e943704ed1a957d934736bf8a3d3 Message-Id: <20220509164036.98C293858405@sourceware.org> Date: Mon, 9 May 2022 16:40:36 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 09 May 2022 16:40:36 -0000 https://gcc.gnu.org/g:93a621991615e943704ed1a957d934736bf8a3d3 commit r9-10063-g93a621991615e943704ed1a957d934736bf8a3d3 Author: Jonathan Wakely Date: Thu Dec 9 13:54:39 2021 +0000 libstdc++: Fix std::exception_ptr regressions [PR103630] This restores support for std::make_exception_ptr and for using std::exception_ptr in C++98. Because the new non-throwing implementation needs to use std::decay to handle references the original throwing implementation is used for C++98. We also need to change the typeid expression so it doesn't yield the dynamic type when the function parameter is a reference to a polymorphic type. Otherwise the new exception object could be caught by any handler matching the dynamic type, even though the actual exception object is only a copy of the base class, sliced to the static type. libstdc++-v3/ChangeLog: PR libstdc++/103630 * libsupc++/exception_ptr.h (make_exception_ptr): Decay the template parameter. Use typeid of the static type. * testsuite/18_support/exception_ptr/103630.cc: New test. (cherry picked from commit a1ca039fc0fe934ef36c25d8284e6e116bcaffa7) Diff: --- libstdc++-v3/libsupc++/exception_ptr.h | 14 +++++--- .../testsuite/18_support/exception_ptr/103630.cc | 39 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/libsupc++/exception_ptr.h b/libstdc++-v3/libsupc++/exception_ptr.h index bd2178b3a1a..9d641103141 100644 --- a/libstdc++-v3/libsupc++/exception_ptr.h +++ b/libstdc++-v3/libsupc++/exception_ptr.h @@ -39,6 +39,10 @@ #include #include +#if __cplusplus >= 201103L +# include +#endif + extern "C++" { namespace std @@ -178,14 +182,16 @@ namespace std exception_ptr make_exception_ptr(_Ex __ex) _GLIBCXX_USE_NOEXCEPT { -#if __cpp_exceptions && __cpp_rtti && !_GLIBCXX_HAVE_CDTOR_CALLABI +#if __cpp_exceptions && __cpp_rtti && !_GLIBCXX_HAVE_CDTOR_CALLABI \ + && __cplusplus >= 201103L + using _Ex2 = typename remove_reference<_Ex>::type; void* __e = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ex)); (void) __cxxabiv1::__cxa_init_primary_exception( - __e, const_cast(&typeid(__ex)), - __exception_ptr::__dest_thunk<_Ex>); + __e, const_cast(&typeid(_Ex)), + __exception_ptr::__dest_thunk<_Ex2>); try { - ::new (__e) _Ex(__ex); + ::new (__e) _Ex2(std::forward<_Ex>(__ex)); return exception_ptr(__e); } catch(...) diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/103630.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/103630.cc new file mode 100644 index 00000000000..58fb2abe4d2 --- /dev/null +++ b/libstdc++-v3/testsuite/18_support/exception_ptr/103630.cc @@ -0,0 +1,39 @@ +// { dg-do run } + +#include +#if __cplusplus < 201103L +// std::make_exception_ptr is defined for C++98 as a GNU extension +# include +#endif + +#include + +struct B +{ + virtual bool derived() const { return false; } +}; + +struct D : B +{ + virtual bool derived() const { return true; } +}; + +int main() +{ + D d; + std::exception_ptr p = std::make_exception_ptr(d); // PR libstdc++/103630 +#if __cpp_exceptions + try + { + std::rethrow_exception(p); + } + catch (const D& d) + { + VERIFY(d.derived()); // PR libstdc++/103630 + } + catch (const B& b) + { + VERIFY(!b.derived()); + } +#endif +}