mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] fix atexit when it is called from an atexit handler
@ 2015-07-22 23:44 Szabolcs Nagy
  2015-07-23  1:18 ` Rich Felker
  0 siblings, 1 reply; 12+ messages in thread
From: Szabolcs Nagy @ 2015-07-22 23:44 UTC (permalink / raw)
  To: musl

When running atexit handlers always restart iterating the list of
handlers so if new ones are registered they are called in the right
order.

Note that the iteration changes the head pointer in an irreversible
way, this is not a problem, but may cause more allocations than
optimal when new handlers are registered during exit.

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.
---
 src/exit/atexit.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/exit/atexit.c b/src/exit/atexit.c
index be82718..a0fd8d7 100644
--- a/src/exit/atexit.c
+++ b/src/exit/atexit.c
@@ -14,13 +14,24 @@ static struct fl
 
 static volatile int lock[2];
 
+static int next()
+{
+	int i;
+	for (; head; head=head->next)
+		for (i=COUNT-1; i>=0; i--)
+			if (head->f[i])
+				return i;
+	return -1;
+}
+
 void __funcs_on_exit()
 {
 	int i;
 	void (*func)(void *), *arg;
 	LOCK(lock);
-	for (; head; head=head->next) for (i=COUNT-1; i>=0; i--) {
-		if (!head->f[i]) continue;
+	for (;;) {
+		i = next();
+		if (i<0) break;
 		func = head->f[i];
 		arg = head->a[i];
 		head->f[i] = 0;
-- 
2.4.1



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2015-07-24 21:25 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-22 23:44 [PATCH] fix atexit when it is called from an atexit handler Szabolcs Nagy
2015-07-23  1:18 ` Rich Felker
2015-07-23  3:07   ` Szabolcs Nagy
2015-07-23  5:54     ` Jens Gustedt
2015-07-23  7:19       ` Jens Gustedt
2015-07-23 19:58         ` Rich Felker
2015-07-23 21:51           ` Jens Gustedt
2015-07-24  0:16             ` Szabolcs Nagy
2015-07-24  6:52               ` Jens Gustedt
2015-07-24 10:53                 ` Szabolcs Nagy
2015-07-24 16:01                 ` Rich Felker
2015-07-24 21:25     ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).