shithub: scc

Download patch

ref: 020a6282c06e9f61517d8a37495d888ec7527808
parent: eba17d80d42e07d4eb9176c66d1bbb3ee39e8d81
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Mar 7 15:31:56 EST 2017

[libc] Fix exit()

The functions registered at atexit() must be called in reverse
order. This patch also protect against calling exit() from
some of the atexit handler.

--- a/libc/src/exit.c
+++ b/libc/src/exit.c
@@ -9,8 +9,13 @@
 exit(int status)
 {
 	void (**bp)(void);
+	int i;
 
-	for (bp = _atexitf; bp < &_atexitf[_ATEXIT_MAX] && *bp; ++bp)
-		(*bp)();
+	for (i = _ATEXIT_MAX-1; i >= 0; --i) {
+		if (bp = _atexit[i]) {
+			*_atexit[i] = NULL;
+			(*bp)();
+		}
+	}
 	_Exit(status);
 }