From 4df91e65f5afab1f960326262661680fd41815b6 Mon Sep 17 00:00:00 2001 From: rosenrost <52100845+rosenrost@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:38:26 +0100 Subject: [PATCH 1/2] Call atexit() handlers in reverse order of registration The C standard requires the functions registered with atexit() to be called in the reverse order of their registration. exit() called them in registration order. This also affects the C++ destructors that are registered via atexit() in __main(). The handlers are now called by index, so a handler that registers another handler (which may realloc() the array) no longer breaks the loop. --- sources/exit.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sources/exit.c b/sources/exit.c index 5ddccb3..bdc78ad 100644 --- a/sources/exit.c +++ b/sources/exit.c @@ -15,9 +15,9 @@ int _num_at_exit = 0; void exit(int status) { - /* first: call all atexit-registered func's */ - for(; _num_at_exit; --_num_at_exit) - (*_at_exit++) (); + /* first: call all atexit-registered func's, in reverse order of registration */ + while (_num_at_exit > 0) + (*_at_exit[--_num_at_exit]) (); /* second: close all files */ /* unneeded here, since handles are unbuffered */ From 20ed5a29685a57860bf04987dd9a114239ee2f2c Mon Sep 17 00:00:00 2001 From: rosenrost <52100845+rosenrost@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:38:26 +0100 Subject: [PATCH 2/2] Don't call atexit() handlers from abort() abort() called exit() and therefore ran all atexit() handlers and C++ destructors, which must not happen on abnormal termination. Use _exit() instead. --- sources/abort.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sources/abort.c b/sources/abort.c index be1aa40..73b0b09 100644 --- a/sources/abort.c +++ b/sources/abort.c @@ -1,6 +1,8 @@ #include +#include void abort(void) { - exit(-1); + /* abnormal termination: unlike exit(), atexit() handlers must not be called */ + _exit(-1); }