...
Different actions must be taken depending on whether or not you desire signal handlers to be persistent.
Persistent handlers
By default, *nix systems leave the handler in place after a signal is generated, whereas Windows system do not.
Non-Compliant Code Example (Windows)
This non-complaint code example fails to persist the signal handler on Windows platforms.
| Code Block | ||
|---|---|---|
| ||
void handler(int signum) {
/* handling code */
}
|
Compliant Solution (Windows)
A C99-compliant solution to persist the handler on a Windows system is to rebind the signal to the handler in the first line of the handler itself.
| Code Block | ||
|---|---|---|
| ||
void handler(int signum) {
#ifdef WINDOWS
signal(signum, handler);
#endif
/* rest of handling code */
}
|
Non-persistent handlers
By default, Windows systems reset the signal handler to its default action after a signal is generated, whereas *nix system do not.
Non-Compliant Code Example (*nix)
This non-complaint code example fails to reset the signal handler to its default behavior on *nix systems.
| Code Block | ||
|---|---|---|
| ||
void handler(int signum) {
/* handling code */
}
|
Compliant Solution (*nix)
A C99-compliant solution to reset the handler on a *nix system is to rebind the signal to the implementation-defined default handler in the first line of the handler itself.
| Code Block | ||
|---|---|---|
| ||
void handler(int signum) {
#ifdef WINDOWS
/* windows automatically resets handlers to default */
#else
signal(signum, SIG_DFL);
#endif
/* rest of handling code */
}
|
Risk Analysis
Failure to re-establish a persistent signal handler on Windows platforms understand implementation-specific details regarding signal handler persistence can lead to unexpected behavior.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
SIG01-A | 1 (high) | 1 (likely) | 3 (low) | P3 | L3 |
References
| Wiki Markup |
|---|
\[[ISO/IEC 9899-1999TR2|AA. C References#ISO/IEC 9899-1999]\] "The {{signal}} function" |