...
Many UNIX (and UNIX-like) systems automatically reinstall signal handlers upon handler execution, meaning that the signal handler defined by the user is left in place until it is explicitly removed. For example, when this code is compiled with GCC 3.4.4 and executed under Red Hat Linux, SIGINT
is captured both times by handler
.:
Code Block |
---|
% ./test ^C Escaped from first while () ^C Escaped from second while () % |
When a signal handler is installed with the signal()
function in Windows and some UNIX systems, the default action is restored for that signal after the signal is triggered. This means that signal handlers are not automatically reinstalled. For example, when this code is compiled with Microsoft Visual Studio 2005, version 8.0, only the first SIGINT
is captured by handler
.:
Code Block |
---|
> test.exe ^C Escaped from first while () ^C > |
...
This noncompliant code example fails to persist the signal handler on Windows platforms and on those UNIX systems where handlers are not persistent by default.:
Code Block | ||||
---|---|---|---|---|
| ||||
void handler(int signum) { /* Handle signal */ } |
...
A common approach to create persistent signal handlers is to call signal()
from within the handler itself, consequently, unresetting the reset signal.:
Code Block | ||||
---|---|---|---|---|
| ||||
void handler(int signum) { if (signal(signum, handler) == SIG_ERR) { /* Handle error */ } /* Handle signal */ } |
...
This noncompliant code example fails to reset the signal handler to its default behavior on systems where handlers are persistent by default.:
Code Block | ||||
---|---|---|---|---|
| ||||
void handler(int signum) { /* Handle signal */ } |
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Compass/ROSE |
|
| Could detect possible violations by flagging any signal handler that calls | ||||||
PRQA QA-C |
| warncall for signal | Partially implemented. |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...
CERT C++ Secure Coding Standard | SIG01-CPP. Understand implementation-specific details regarding signal handler persistence |
...