CERT-SIG31-C
In this section:
Synopsis
Shared objects in a signal handler are accessed or modified.
Enabled by default
Yes
Severity/Certainty
High/Low

Full description
Accessing or modifying shared objects (not of the type volatile sig_atomic_t) in a signal handler might result in race conditions that can leave data in an inconsistent state.
Coding standards
- CERT SIG31-C
Do not access or modify shared objects in signal handlers
- CWE 662
Improper Synchronization
Code examples
The following code example fails the check and will give a warning:
#include <signal.h>
#include <stdlib.h>
#include <string.h>
enum { MAX_MSG_SIZE = 24 };
char *err_msg;
void handler(int signum) {
strcpy(err_msg, "SIGINT encountered.");
}
int main(void) {
signal(SIGINT, handler);
err_msg = (char *)malloc(MAX_MSG_SIZE);
if (err_msg == NULL) {
/* Handle error */
}
strcpy(err_msg, "No errors yet.");
/* Main code loop */
return 0;
}
The following code example passes the check and will not give a warning about this issue:
#include <signal.h>
#include <stdlib.h>
#include <string.h>
enum { MAX_MSG_SIZE = 24 };
volatile sig_atomic_t e_flag = 0;
void handler(int signum) {
e_flag = 1;
}
int main(void) {
char *err_msg = (char *)malloc(MAX_MSG_SIZE);
if (err_msg == NULL) {
/* Handle error */
}
signal(SIGINT, handler);
strcpy(err_msg, "No errors yet.");
/* Main code loop */
if (e_flag) {
strcpy(err_msg, "SIGINT received.");
}
return 0;
}