]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Remove reliance on unspecified behaviour in std::rethrow_if_nested test
authorJonathan Wakely <jwakely@redhat.com>
Mon, 21 Aug 2023 09:36:13 +0000 (10:36 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 21 Aug 2023 09:43:19 +0000 (10:43 +0100)
This test case calls std::set_terminate while there is an active
exception. Since LWG 2111 it is unspecified which terminate handler is
used when std::nested_exception::rethrow_nested() calls std::terminate.
With libsupc++ the global handler changed by std::set_terminate is used,
but libc++abi uses the active exception's handler (the one that was
current when the exception was first thrown).

Adjust the test case so that it works with either implementation choice.
So that the process doesn't exit cleanly if std::terminate happens
sooner than expected, use a global variable to control when the "clean
terminate" behaviour happens.

libstdc++-v3/ChangeLog:

* testsuite/18_support/nested_exception/rethrow_if_nested-term.cc:
Call std::set_terminate before throwing the nested exception.

libstdc++-v3/testsuite/18_support/nested_exception/rethrow_if_nested-term.cc

index 3bfc7ab994362fe235f617688594c6b7b34f2758..b221eea3178f762d15a7c7e1f24c32c97a17ea41 100644 (file)
@@ -4,25 +4,33 @@
 #include <exception>
 #include <cstdlib>
 
-[[noreturn]] void terminate_cleanly() noexcept { std::exit(0); }
+int exit_status = 1;
+[[noreturn]] void terminate_cleanly() noexcept { std::exit(exit_status); }
 
 struct A { virtual ~A() = default; };
 
 int main()
 {
+  std::set_terminate(terminate_cleanly);
   try
   {
     // At this point std::current_exception() == nullptr so the
     // std::nested_exception object is empty.
     std::throw_with_nested(A{});
+
+    // Should not reach this point.
+    std::abort();
   }
   catch (const A& a)
   {
-    std::set_terminate(terminate_cleanly);
+    // This means the expected std::terminate() call will exit cleanly,
+    // so this test will PASS.
+    exit_status = 0;
+
     std::rethrow_if_nested(a);
 #if __cpp_rtti
     // No nested exception, so trying to rethrow it calls std::terminate()
-    // which calls std::exit(0). Shoud not reach this point.
+    // which calls std::exit(0). Should not reach this point.
     std::abort();
 #else
     // Without RTTI we can't dynamic_cast<const std::nested_exception*>(&a)