From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 3C60D3858434 for ; Thu, 9 Dec 2021 23:30:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3C60D3858434 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-534-RTfoGittN2CjffJT-OTomA-1; Thu, 09 Dec 2021 18:30:40 -0500 X-MC-Unique: RTfoGittN2CjffJT-OTomA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C3F2D2F41; Thu, 9 Dec 2021 23:30:39 +0000 (UTC) Received: from localhost (unknown [10.33.36.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id 71D041017CF5; Thu, 9 Dec 2021 23:30:39 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::exception_ptr regressions [PR103630] Date: Thu, 9 Dec 2021 23:30:38 +0000 Message-Id: <20211209233038.1569530-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Dec 2021 23:30:45 -0000 Tested powerpc64le-linux, pushed to trunk. 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 (exception_ptr): Fix exception specifications on inline definitions. (make_exception_ptr): Decay the template parameter. Use typeid of the static type. * testsuite/18_support/exception_ptr/103630.cc: New test. --- libstdc++-v3/libsupc++/exception_ptr.h | 19 ++++++--- .../18_support/exception_ptr/103630.cc | 39 +++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 libstdc++-v3/testsuite/18_support/exception_ptr/103630.cc diff --git a/libstdc++-v3/libsupc++/exception_ptr.h b/libstdc++-v3/libsupc++/exception_ptr.h index f9dffd565bf..f59752478b1 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 + #ifdef _GLIBCXX_EH_PTR_RELOPS_COMPAT # define _GLIBCXX_EH_PTR_USED __attribute__((__used__)) #else @@ -175,13 +179,14 @@ namespace std _GLIBCXX_EH_PTR_USED inline - exception_ptr::exception_ptr() _GLIBCXX_NOEXCEPT + exception_ptr::exception_ptr() _GLIBCXX_USE_NOEXCEPT : _M_exception_object(0) { } _GLIBCXX_EH_PTR_USED inline - exception_ptr::exception_ptr(const exception_ptr& __other) _GLIBCXX_NOEXCEPT + exception_ptr::exception_ptr(const exception_ptr& __other) + _GLIBCXX_USE_NOEXCEPT : _M_exception_object(__other._M_exception_object) { if (_M_exception_object) @@ -232,14 +237,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 +} -- 2.31.1