Some errors, such as a value out of range, might be the result of erroneous user input. If the input is interactive, the program can prompt the user for an acceptable value. With other errors, such as a resource allocation failure, the system may have little choice other than to shutdownshut down.
Wiki Markup |
---|
\[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] Section 6.47, "REU Termination strategy," says: |
When a fault is detected, there are many ways in which a system can react. The quickest and most noticeable way is to fail hard, also known as fail fast or fail stop. The reaction to a detected fault is to immediately halt the system. Alternatively, the reaction to a detected fault could be to fail soft. The system would keep working with the faults present, but the performance of the system would be degraded. Systems used in a high availability environment such as telephone switching centers, e-commerce, etc. would likely use a fail soft approach. What is actually done in a fail soft approach can vary depending on whether the system is used for safety critical or security critical purposes. For fail safe systems, such as flight controllers, traffic signals, or medical monitoring systems, there would be no effort to meet normal operational requirements, but rather to limit the damage or danger caused by the fault. A system that fails securely, such as cryptologic systems, would maintain maximum security when a fault is detected, possibly through a denial of service.
...
C99 provides several options for program termination, including exit()
, returning from main()
, _Exit()
, and abort()
.
...
The C standard exit()
function is typically used to end a program. It takes one argument, which should be either EXIT_SUCCESS
or EXIT_FAILURE
indicating normal or abnormal termination. Zero is equally portable and well understood. C99 , Section 7.20.4.3 says "If the value of status is zero or EXIT_SUCCESS
, an implementation-defined form of the status successful termination is returned." The exit()
function never returns.
...
The C standard atexit()
function can be used to customize exit()
to perform additional actions at program termination.
For example, calling:
Code Block | ||
---|---|---|
| ||
atexit(turn_gizmo_off); |
...
Consequently, returning from main()
is equivalent to calling exit()
. Many compilers implement this behavior with something analogous to:
Code Block |
---|
void _start() { /* ... */ exit(main(argc, argv)); } |
...
A more abrupt function, _Exit()
also takes one argument and never returns. The standard specifies that _Exit()
also closes open file descriptors , but does not specify if whether _Exit()
flushes file buffers or deletes temporary files. Functions registered by atexit()
are not executed.
...
The quickest and most abrupt way to terminate a program, abort()
takes no arguments , and always signifies abnormal termination to the operating system.
...
The abort()
function should not be called if it is important to perform application-specific cleanup before exiting. In this non-compliant code example, abort()
is called after data is sent to an open file descriptor. The data may or may not actually get written to the file.
...
Using abort()
or _Exit()
in place of exit()
may leave written files in an inconsistent state, and may also leave sensitive temporary files on the filesystemfile system.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ERR04-A | medium | unlikely | low | P6 | L2 |
...
Wiki Markup |
---|
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 5.1.2.2.3, "Program termination,", and Section 7.20.4, "Communication with the environment" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "REU Termination strategy" |
...