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.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)