メインコンテンツ

CERT C: Rec. SIG02-C

Avoid using signals to implement normal functionality

Since R2026a

Description

Avoid using signals to implement normal functionality.1

Polyspace Implementation

The rule checker checks for Use of standard header file signal.h.

Examples

expand all

Issue

This issue occurs if you use the functionalities from the standard header signal.h. If the signal function is a macro and the macro is expanded in the code, Polyspace® reports a violation.

Risk

Using signal handling functions can cause implementation-defined and undefined behavior.

Fix

Use alternatives such as the concurrency libraries from POSIX or C11.

Example

In this example, Polyspace reports violations on the use of signals for synchronizing two threads.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>  //Noncompliant
#include <unistd.h>

volatile sig_atomic_t signal_received = 0;

void signal_handler(int signum) {
    signal_received = 1;
}

void* thread_func_1(void* arg) {
    printf("Thread 1: Doing some work...\n");
    // Work....
    printf("Thread 1: Sending signal to thread 2.\n");
    pthread_kill(*(pthread_t*)arg, SIGUSR1);
    return NULL;
}

void* thread_func_2(void* arg) {
    signal(SIGUSR1, signal_handler);  //Noncompliant
    printf("Thread 2: Waiting for signal...\n");
    while (!signal_received) {
        pause();  // Wait for signal
    }
    printf("Thread 2: Signal received, proceeding with work.\n");
    return NULL;
}

Correction

Use alternatives, such as mutexes and condition variables from the C11 concurrency library, for synchronizing threads.

#include <stdio.h>
#include <stdlib.h>
#include <threads.h>  //Compliant

mtx_t mutex;
cnd_t condition;
int signal_received = 0;

int thread_func_1(void* arg) {
    printf("Thread 1: Doing some work...\n");
    thrd_sleep(&(struct timespec){.tv_sec = 2}, NULL);  
    printf("Thread 1: Signaling thread 2.\n");

    mtx_lock(&mutex);
    signal_received = 1;
    cnd_signal(&condition);
    mtx_unlock(&mutex);

    return 0;
}

int thread_func_2(void* arg) {
    mtx_lock(&mutex);
    while (!signal_received) {
        printf("Thread 2: Waiting for signal...\n");
        cnd_wait(&condition, &mutex);
    }
    printf("Thread 2: Signal received, proceeding with work.\n");
    mtx_unlock(&mutex);

    return 0;
}

Check Information

Group: Rule 11. Signals (SIG)
PQL Name: std.cert.SIG02_C

Version History

Introduced in R2026a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.