Main Content

MISRA C++:2023 Rule 21.10.3

The facilities provided by the standard header file <csignal> shall not be used

Since R2024b

Description

Rule Definition

The facilities provided by the standard header file <csignal> shall not be used.

Rationale

Signal handling functions such as signal() and other function provided by <csignal> can lead to undefined and implementation-specific behavior. Signal handlers are limited in what they can safely do and the behavior of signals can vary between different operating systems.

Avoid using the facilities provided by the header <csignal> including the facilities provided by <signal.h>.

Polyspace Implementation

Polyspace® reports a violation on any use of the functions provided by <csignal>, such as signal().

Polyspace does not report a violation when you call signal() with a value of SIG_IGN as the second argument because this can be used to disable one or more signals.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <csignal>
#include <iostream>
#include <chrono>
#include <thread>

void handle_sigterm(int signal) {
    // ...
    std::exit(signal);
}

int main() {
    std::signal(SIGTERM, handle_sigterm);			// Noncompliant	

    std::signal(SIGTERM, SIG_IGN);                          // Compliant
	
    // Infinite loop to simulate a long-running process
    while (true) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        std::cout << "Processing..." << std::endl;
    }

    return 0;
}

In this example, the handle_sigterm() function is defined as a signal handler that is called when a SIGTERM signal is received. Use of the signal() function from <csignal> is noncompliant.

Because SIG_IGN is the second argument of the second signal() call, it is compliant by exception.

Check Information

Group: Language support library
Category: Required

Version History

Introduced in R2024b