>From 63c2b2d58b2ec45df67927b8959ea8af6dd578ed Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Wed, 22 Jul 2015 23:05:57 +0000 Subject: [PATCH] fix atexit when it is called from an atexit handler The old code accepted atexit handlers after exit, but did not run them. C11 seems to explicitly allow atexit to fail in this case, but this situation can easily come up in C++ if a destructor has local static object with a destructor so it should be handled. Restart iterating the list of atexit handlers if new handlers are added during a call. Note that the memory usage can grow linearly with the overall number of registered atexit handlers instead of with the worst case list length. (This only matters if atexit handlers keep registering atexit handlers which should not happen in practice). --- src/exit/atexit.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/exit/atexit.c b/src/exit/atexit.c index be82718..75f7511 100644 --- a/src/exit/atexit.c +++ b/src/exit/atexit.c @@ -17,6 +17,7 @@ static volatile int lock[2]; void __funcs_on_exit() { int i; + struct fl *old; void (*func)(void *), *arg; LOCK(lock); for (; head; head=head->next) for (i=COUNT-1; i>=0; i--) { @@ -24,9 +25,12 @@ void __funcs_on_exit() func = head->f[i]; arg = head->a[i]; head->f[i] = 0; + old = head; UNLOCK(lock); func(arg); LOCK(lock); + if (head != old || head->f[i]) + i = COUNT; } } -- 2.4.1