...
Exceptions
SIG31-EX1: The consensus formed at the April 2008 meeting of ISO/IEC WG14 was that it is acceptable to read a variable of type volatile sig_atomic_t as well as write it, and that doing so was not a problem on any known implementation. In this compliant solution, a variable of type volatile sig_atomic_t is read and then written before returning from the signal handler.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <signal.h>
#include <stdlib.h>
#include <string.h>
volatile sig_atomic_t e_flag = 0;
void handler(int signum) {
if (0 == e_flag) {
e_flag = 1;
}
}
int main(void) {
enum { MAX_MSG_SIZE = 24 };
char *err_msg = (char *)malloc(MAX_MSG_SIZE);
if (err_msg == NULL) {
/* Handle error condition */
}
signal(SIGINT, handler);
strcpy(err_msg, "No errors yet.");
/* Main code loop */
if (e_flag) {
strcpy(err_msg, "SIGINT received.");
}
return 0;
}
|
SIG31-EX2: The C Standard in subclause 7.14.1.1 paragraph 5 makes a special exception for errno when a valid call to the signal() function results in a SIG_ERR return, allowing errno to take an indeterminate value. See ERR32-C. Do not rely on indeterminate values of errno.
...