https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97720
Bug ID: 97720 Summary: Sometimes std::current_exception does not work properly in the terminate handler Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: m101010a at gmail dot com Target Milestone: --- $ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-libunwind-exceptions --disable-werror gdc_include_dir=/usr/include/dlang/gdc Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 10.2.0 (GCC) $ cat x.cpp #include <iostream> struct has_destructor { ~has_destructor(); }; void do_nothing(); inline int throwing_function() { #ifdef DESTRUCTOR has_destructor hd; #endif throw ""; } class C { public: C() noexcept; ~C(); }; C::C() noexcept {throwing_function();} inline C::~C() { do_nothing(); } static void term_handler() { const char *f = "Died without exception\n"; auto e = std::current_exception(); if (e) f = "Died with exception\n"; std::cout << f << std::flush; quick_exit(0); } int main() { std::set_terminate(&term_handler); C{}; } $ cat y.cpp struct has_destructor { ~has_destructor(); }; void do_nothing(); has_destructor::~has_destructor() = default; void do_nothing() {} $ g++ x.cpp y.cpp -O1 $ ./a.out Died with exception $ g++ -DDESTRUCTOR x.cpp y.cpp -O1 $ ./a.out Died without exception $ This bug is very sensitive to small changes. For example, it will correctly get the exception_ptr when compiling with -O0 or -O2, or if C's constructor is inline, or if C's destructor is not inline. This is probably the underlying cause of bug 97339.