]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix std::exception_ptr regressions [PR103630]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 9 Dec 2021 13:54:39 +0000 (13:54 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 26 Apr 2022 11:05:36 +0000 (12:05 +0100)
This restores support for std::make_exception_ptr<E&> 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)

libstdc++-v3/libsupc++/exception_ptr.h
libstdc++-v3/testsuite/18_support/exception_ptr/103630.cc [new file with mode: 0644]

index f1592c84afef09a3757cb78ba480c5adfcfc3361..e73e1a6fcf460f1b438af43eafb3c172c980a03d 100644 (file)
 #include <typeinfo>
 #include <new>
 
+#if __cplusplus >= 201103L
+# include <bits/move.h>
+#endif
+
 extern "C++" {
 
 namespace std 
@@ -185,14 +189,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<std::type_info*>(&typeid(__ex)),
-         __exception_ptr::__dest_thunk<_Ex>);
+         __e, const_cast<std::type_info*>(&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 (file)
index 0000000..58fb2ab
--- /dev/null
@@ -0,0 +1,39 @@
+// { dg-do run }
+
+#include <exception>
+#if __cplusplus < 201103L
+// std::make_exception_ptr is defined for C++98 as a GNU extension
+# include <bits/exception_ptr.h>
+#endif
+
+#include <testsuite_hooks.h>
+
+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<B&>(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
+}