scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 3ddba6b6ed93c63e246397e08acdc39a2e1744a9
parent e35b235d743259f6fbacd4037aab735d3e39dca3
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue,  7 Mar 2017 20:31:56 +0100

[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.

Diffstat:
libc/src/exit.c | 9+++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/libc/src/exit.c b/libc/src/exit.c @@ -9,8 +9,13 @@ void 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); }