[Bug c++/97720] Sometimes std::current_exception does not work properly in the terminate handler

m101010a at gmail dot com gcc-bugzilla@gcc.gnu.org
Thu Nov 5 02:02:49 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97720

--- Comment #2 from m101010a at gmail dot com ---
> when the compiler can see there is no matching handler for the exception, 
> it doesn't perform stack unwinding

This is fine, it's implementation-defined whether the stack is unwound.

> it just calls std::terminate without throwing anything.

This is not fine.  According to N4868 there is an implicit exception handler
active when std::terminate is called due to a throw (14.4/7), and
std::current_exception returns the currently handled exception (17.9.7/8).  So
even if the compiler is going to optimize the throw to a call to terminate it
still needs to behave as if something is being thrown.  This interpretation is
corroborated by comments from MSVC devs in a similar bug: see
https://developercommunity.visualstudio.com/comments/305900/view.html and
https://developercommunity.visualstudio.com/comments/579784/view.html

Also I just looked at the assembly, and it still calls __cxa_throw, even in
cases where it warns that the throw will always terminate:
https://godbolt.org/z/9hja8r .  Between that and how very small changes cause
the test program to report the exception in the terminate handler, this doesn't
look like intentional behavior.


More information about the Gcc-bugs mailing list