According to the C Standard, subclause 7.14.1.1 [ISO/IEC 9899:2011], returning from a SIGFPE
, SIGILL
, or
or any other implementation-defined value corresponding to a computational exception signal handler not generated by a call to SIGSEGV
raise()
is undefined behavior 130.
The POSIX standard [IEEE Std 1003.1:2013] adds SIGBUS
to the list of computational exception signal handlers,
Do not return from SIGFPE
, SIGILL
, or
, or any other implementation-defined value corresponding to a computational exception such as SIGSEGV
SIGBUS
on POSIX systems, regardless of how the signal was generated.
Noncompliant Code Example
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <signal.h>
#include <stdlib.h>
volatile sig_atomic_t denom;
void sighandle(int s) {
/* Fix the offending volatile */
if (denom == 0) {
denom = 1;
}
}
int main(int argc, char *argv[]) {
int result = 0;
if (argc < 2) {
return 0;
}
denom = (sig_atomic_t) strtol(argv[1], NULL, 10);
signal(SIGFPE, (*sighandle));
result = 100 / (int)denom;
return 0;
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <signal.h> #include <stdlib.h> volatile sig_atomic_t denom; void sighandle(int s) { /* Recovery is impossible */ abort(); } int main(int argc, char *argv[]) { int result = 0; int denom; if if (argc < 2) { return 0; } denom = (sig_atomic_t) strtol(argv[1], NULL, 10); signal(SIGFPE, (*sighandle)); result = 100 / (int)denom; return 0; } |
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Bibliography
[IEEE Std 1003.1:2013] | 2.4.1 Signal Generation and Delivery |
[ISO/IEC 9899:2011] | Subclause 7.14.1.1, "The signal Function" |
...