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.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)