CWE Rule 432
Description
Dangerous Signal Handler not Disabled During Sensitive Operations
Polyspace Implementation
The rule checker checks for Signal handling not disabled in handler.
Examples
This issue occurs if the handler of a signal can be reentered by the same signal during its execution. Polyspace® reports a violation if the signal handler performs any action without performing one of these actions first:
Ignore the current signal — Invoke
signal()using the current signal as the first argument andSIG_IGNas the second argument.Set the handling of the current signal to default action — Invoke
signal()using the current signal as the first argument andSIG_DFLas the second argument.
If the signal handler does not stop listening for the current signal, the handler can be reentered if the current signal is received again during the execution of the handler. Consider this signal handler:
#include <signal.h>
int shared_state = 0;
void signal_handler(int signum) {
shared_state++;
}
int main() {
//...
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
}SIGINT or
SIGTERM signals, signal_handler() is invoked.
During the execution of signal_handler(), if either of the signals is
received again, the execution of the handler is interrupted and the value of
shared_state can be corrupted. In the signal handler function, before performing any action, set the handling of the
current signal to the default action. Alternatively, ignore the current signal during the
signal handling operations. Finally, before exiting the signal handler, assign the current
handler to the current signal again. The signal_handler function in the
preceding code can be fixed as
follows:
void signal_handler(int signum) {
signal(signum, SIG_DFL);
shared_state++;
signal(signum, signal_handler);
}In this example, the handlers increment_handler() and
decrement_handler() can be interrupted during their execution.
Polyspace reports
violations.
#include <stdio.h>
#include <signal.h>
volatile sig_atomic_t counter = 0;
void increment_handler(int signum) { //Noncompliant
counter++;
}
void decrement_handler(int signum) { //Noncompliant
counter--;
}
int main() {
signal(SIGUSR1, increment_handler);
signal(SIGUSR2, decrement_handler);
//...
return 0;
}To fix these violations, disable the handlers first before performing any action in the handlers.
#include <stdio.h>
#include <signal.h>
volatile sig_atomic_t counter = 0;
void increment_handler(int signum) { //Compliant
signal(signum, SIG_DFL);
counter++;
signal(signum, increment_handler);
}
void decrement_handler(int signum) { //Compliant
signal(signum, SIG_DFL);
counter--;
signal(signum, increment_handler);
}
int main() {
signal(SIGUSR1, increment_handler);
signal(SIGUSR2, decrement_handler);
//...
return 0;
}Check Information
| Category: Others |
PQL Name: std.cwe_native.R432 |
Version History
Introduced in R2026a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)