You read errno after calling an
errno-setting function in a signal handler
This defect occurs when you call one of these functions in a signal handler:
signal: You call the signal function
in a signal handler and then read the value of
errno.
For instance, the signal handler function handler calls
signal and then calls perror,
which reads
errno.
typedef void (*pfv)(int);
void handler(int signum) {
pfv old_handler = signal(signum, SIG_DFL);
if (old_handler == SIG_ERR) {
perror("SIGINT handler");
}
}errno-setting POSIX® function: You call an errno-setting
POSIX function in a signal handler but do not restore
errno when returning from the signal handler.
For instance, the signal handler function handler calls
waitpid, which changes errno, but
does not restore errno before
returning.
#include <stddef.h>
#include <errno.h>
#include <sys/wait.h>
void handler(int signum) {
int rc = waitpid(-1, NULL, WNOHANG);
if (ECHILD != errno) {
}
}
In each case that the checker flags, you risk relying on an indeterminate value of
errno.
signal: If the call to signal in
a signal handler fails, the value of errno is
indeterminate (see C11 Standard, Sec. 7.14.1.1). If you rely on a
specific value of errno, you can see unexpected
results.
errno-setting POSIX function: An errno-setting function
sets errno on failure. If you read
errno after a signal handler is called and the
signal handler itself calls an errno-setting
function, you can see unexpected results.
Avoid situations where you risk relying on an indeterminate value of
errno.
signal: After calling the signal
function in a signal handler, do not read errno or
use a function that reads errno.
errno-setting POSIX function: Before calling an
errno-setting function in a signal handler, save
errno to a temporary variable. Restore
errno from this variable before returning from
the signal handler.
| Group: Programming |
| Language: C | C++ |
| Default: On for handwritten code, off for generated code |
Command-Line Syntax:
SIG_HANDLER_ERRNO_MISUSE |
| Impact: Medium |
Errno not checked | Errno not reset | Find defects (-checkers) | Function called
from signal handler not asynchronous-safe