fgets: avoid arithmetic overflow when n==INT_MIN is passed
[musl] / src / exit / at_quick_exit.c
1 #include <stdlib.h>
2 #include "libc.h"
3 #include "lock.h"
4 #include "fork_impl.h"
5
6 #define COUNT 32
7
8 static void (*funcs[COUNT])(void);
9 static int count;
10 static volatile int lock[1];
11 volatile int *const __at_quick_exit_lockptr = lock;
12
13 void __funcs_on_quick_exit()
14 {
15         void (*func)(void);
16         LOCK(lock);
17         while (count > 0) {
18                 func = funcs[--count];
19                 UNLOCK(lock);
20                 func();
21                 LOCK(lock);
22         }
23 }
24
25 int at_quick_exit(void (*func)(void))
26 {
27         int r = 0;
28         LOCK(lock);
29         if (count == 32) r = -1;
30         else funcs[count++] = func;
31         UNLOCK(lock);
32         return r;
33 }