https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125705
Bug ID: 125705
Summary: [libstdc++] `std::exit` does not call `terminate` when
`atexit` handler exits via an exception
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: hstong at ca dot ibm.com
Target Milestone: ---
Since at least C++98, `std::exit` has been specified such that `std::terminate`
is called when an `atexit`-registered function exits via an exception (C++98
subclause 18.3 [lib.support.start.term] paragraph 5,
https://wg21.link/support.start.term#9.1). libstdc++ does not meet this
requirement as it allows both the search for a handler and stack unwinding
itself to continue past the body of `std::exit`.
https://godbolt.org/z/18cPzKnTo
### Source (`<stdin>`)
```cpp
#include <stdlib.h>
void cleanup() { throw 42; }
void f [[gnu::weak]]() { exit(EXIT_FAILURE); }
volatile auto fn = cleanup;
int main(void) {
atexit(fn);
try {
f();
} catch (...) { return 0; }
}
```
### Compiler invocation
```
g++ -std=c++26 -xc++ - -o prog
```
### Program invocation
```
./prog
```
### Actual program output
(rc=0)
### Expected program output
```
terminate called after throwing an instance of 'int'
```
### Compiler version info (g++ -v)
```
Using built-in specs.
COLLECT_GCC=/opt/wandbox/gcc-head/bin/g++
COLLECT_LTO_WRAPPER=/opt/wandbox/gcc-head/libexec/gcc/x86_64-pc-linux-gnu/17.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../source/configure --prefix=/opt/wandbox/gcc-head
--enable-languages=c,c++ --disable-multilib --without-ppl --without-cloog-ppl
--enable-checking=release --disable-nls --enable-lto
LDFLAGS=-Wl,-rpath,/opt/wandbox/gcc-head/lib,-rpath,/opt/wandbox/gcc-head/lib64,-rpath,/opt/wandbox/gcc-head/lib32
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 17.0.0 20260604 (experimental) (GCC)
```