From 30226c2f0959ef4a74f67de4ed762cdc4dffb82f Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 14 Nov 2023 14:58:44 -0500 Subject: enable multiple-exit() detection --- src/stdlib/_stdlib.h | 2 ++ src/stdlib/atexit.c | 5 ----- src/stdlib/exit.c | 13 ++++++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/stdlib/_stdlib.h b/src/stdlib/_stdlib.h index 4d456594..82b7943d 100644 --- a/src/stdlib/_stdlib.h +++ b/src/stdlib/_stdlib.h @@ -19,6 +19,8 @@ struct __stdlib { struct atexit *next; struct atexit *prev; } atexit; + int exit_called; + int quick_exit_called; unsigned int rand; char **environ; constraint_handler_t constraint_handler; diff --git a/src/stdlib/atexit.c b/src/stdlib/atexit.c index d48b9d0e..79b486d0 100644 --- a/src/stdlib/atexit.c +++ b/src/stdlib/atexit.c @@ -1,5 +1,3 @@ -#if 0 - #include #include #include "_stdlib.h" @@ -38,6 +36,3 @@ RETURN_FAILURE(NONZERO) RETURN_SUCCESS(0) STDC(1) */ - - -#endif diff --git a/src/stdlib/exit.c b/src/stdlib/exit.c index 55800ace..60a933b8 100644 --- a/src/stdlib/exit.c +++ b/src/stdlib/exit.c @@ -1,5 +1,3 @@ -#if 0 - #include #include #include "_stdlib.h" @@ -11,6 +9,14 @@ _Noreturn void exit(int status) long scno = __syscall_lookup(exit); struct atexit *ae = &(__stdlib.atexit); + if (__stdlib.quick_exit_called) { + __stdlib.constraint_handler("Undefined behavior: exit() called after quick_exit()", NULL, 0); + } + if (__stdlib.exit_called) { + __stdlib.constraint_handler("Undefined behavior: exit() called twice", NULL, 0); + } + __stdlib.exit_called = 1; + /* execute all atexit() registered functions in reverse order */ while (ae) { int i = ae->nfns; @@ -47,6 +53,3 @@ IMPLEMENTATION(The unsuccessful termination value returned to the host environme /* STDC(1) */ - - -#endif -- cgit v1.2.1