public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Cc: Iain Sandoe <iain@sandoe.co.uk>
Subject: [committed] libstdc++: Remove reliance on unspecified behaviour in std::rethrow_if_nested test
Date: Mon, 21 Aug 2023 10:46:11 +0100	[thread overview]
Message-ID: <20230821094701.1548195-1-jwakely@redhat.com> (raw)

This is the patch resolving the non-portable test that Iain raised in:
https://gcc.gnu.org/pipermail/libstdc++/2023-August/056534.html

Tested x86_64-linux. Pushed to trunk.

Backports would be OK, but I don't think they are needed.

-- >8 --

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.
---
 .../nested_exception/rethrow_if_nested-term.cc     | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/libstdc++-v3/testsuite/18_support/nested_exception/rethrow_if_nested-term.cc b/libstdc++-v3/testsuite/18_support/nested_exception/rethrow_if_nested-term.cc
index 3bfc7ab9943..b221eea3178 100644
--- a/libstdc++-v3/testsuite/18_support/nested_exception/rethrow_if_nested-term.cc
+++ b/libstdc++-v3/testsuite/18_support/nested_exception/rethrow_if_nested-term.cc
@@ -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)
-- 
2.41.0


                 reply	other threads:[~2023-08-21  9:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230821094701.1548195-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=iain@sandoe.co.uk \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).