...
The C99 exit()
function is used for normal program termination (see ERR04-AC. Choose an appropriate termination strategy). Nested calls to exit()
result in undefined behavior. This can only occur when exit()
is invoked from a function registered with atexit()
, or when exit()
is called from within a signal handler (see SIG30-C. Call only asynchronous-safe functions within signal handlers).
If a call to the longjmp
function is made that would terminate the call to a function registered with atexit()
, the behavior is undefined.
...
Noncompliant Code Example
In this non-compliant noncompliant code example, the exit1()
and exit2()
functions are registered by atexit()
to perform required cleanup upon program termination. However, if condition
evaluates to true, exit()
is called a second time, resulting in undefined behavior.
...
Code Block | ||
---|---|---|
| ||
#include <stdio.h> #include <stdlib.h> void exit1(void) { /* ...cleanup code... */ return; } void exit2(void) { if (/* condition */) { /* ...more cleanup code... */ } return; } int main(void) { if (atexit(exit1) != 0) { /* Handle Error */ } if (atexit(exit2) != 0) { /* Handle Error */ } /* ...program code... */ exit(0); } |
...
Noncompliant Code Example
The function exit1()
is registered by atexit()
, so upon program termination, exit1()
is called. Execution will jump back to main()
and return, with undefined results.
...