Some errors, such as out-of-range values, might be the result of erroneous user input. Interactive programs typically handle such errors by rejecting the input and prompting the user for an acceptable value. Servers reject invalid user input by indicating an error to the client while at the same time continuing to service other clients' valid requests. All robust programs must be prepared to gracefully handle resource exhaustion, such as low memory or disk space conditions, at a minimum by preventing the loss of user data kept in volatile storage. Interactive programs may give the user the option to save data on an alternate alternative medium, while whereas network servers may respond by reducing throughput or otherwise degrading the quality of service. However, when certain kinds of errors are detected, such as irrecoverable logic errors, rather than risk data corruption by continuing to execute in an indeterminate state, the appropriate strategy may be for the system to quickly shut down, allowing the operator to start it afresh in a determinate state.
[ISO/IEC TR 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.
And also
The reaction to a fault in a system can depend on the criticality of the part in which the fault originates. When a program consists of several tasks, the tasks each may be critical, or not. If a task is critical, it may or may not be restartable by the rest of the program. Ideally, a task which detects a fault within itself should be able to halt leaving its resources available for use by the rest of the program, halt clearing away its resources, or halt the entire program. The latency of any such communication, and whether other tasks can ignore such a communication, should be clearly specified. Having inconsistent reactions to a fault, such as the fault reaction to a crypto fault, can potentially be a vulnerability.
C99 provides C provides several options for program termination, including exit()
, returning from main()
, _Exit()
, and abort()
.
...
Calling exit()
causes normal program termination to occur. Other than returning from main()
, calling exit()
is the typical way to end a program. The function takes one argument of type int
, which should be either EXIT_SUCCESS
or EXIT_FAILURE
, indicating successful or unsuccessful termination , respectively. The value of EXIT_SUCCESS
is guaranteed to be zero. C99 Section The C Standard, Section 7.2022.4.3 4 [ISO/IEC 9899:2011], 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.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdlib.h>
/* ... */
if (/* something really bad happened */) {
exit(EXIT_FAILURE);
}
|
...
- Flushes unwritten buffered data.
- Closes all open files.
- Removes temporary files.
- Returns an integer exit status to the operating system.
The C standard Standard atexit()
function can be used to customize exit()
to perform additional actions at program termination.
...
Code Block | ||||
---|---|---|---|---|
| ||||
atexit(turn_gizmo_off);
|
registers the turn_gizmo_off()
function so that a subsequent call to exit()
will invoke turn_gizmo_off()
as it terminates the program. C99 C requires that atexit()
can register at least 32 functions.
...
Note that the behavior of a program that calls exit()
from an atexit
handler is undefined. (See undefined behavior 172 in Annex J of C99. See also rule ENV32-C. All atexit handlers must return normally.)
...
Code Block | ||||
---|---|---|---|---|
| ||||
int main(int argc, char **argv) {
/* ... */
if (/* something really bad happened */) {
return EXIT_FAILURE;
}
/* ... */
return EXIT_SUCCESS;
}
|
...
Consequently, returning from main()
is equivalent to calling exit()
. Many compilers implement this behavior with something analogous to
Code Block |
---|
void _start(void) {
/* ... */
exit(main(argc, argv));
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdlib.h>
/* ... */
if (/* something really bad happened */) {
_Exit(EXIT_FAILURE);
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdlib.h>
/* ... */
if (/* something really bad happened */) {
abort();
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdlib.h>
#include <stdio.h>
int write_data(void) {
const char *filename = "hello.txt";
FILE *f = fopen(filename, "w");
if (f == NULL) {
/* Handle error */
}
fprintf(f, "Hello, World\n");
/* ... */
abort(); /* oops! data might not be written! */
/* ... */
return 0;
}
int main(void) {
write_data();
return 0;
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdlib.h>
#include <stdio.h>
int write_data(void) {
const char *filename = "hello.txt";
FILE *f = fopen(filename, "w");
if (f == NULL) {
/* Handle error */
}
fprintf(f, "Hello, World\n");
/* ... */
exit(EXIT_FAILURE); /* writes data & closes f. */
/* ... */
return 0;
}
int main(void) {
write_data();
return 0;
}
|
...