CERT-ERR30-C_a
Synopsis
Set errno to zero before calling a library function known to set errno.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
The value of errno is initialized to zero at program startup, but it is never subsequently set to zero by any C standard library function. The value of errno may be set to nonzero by a C standard library function call whether or not there is an error, provided the use of errno is not documented in the description of the function. Therefore, errno should be set to zero before calling an errno-setting function.
Coding standards
- CERT ERR30-C
Set errno to zero before calling a library function known to set errno, and check errno only after the function returns a value indicating failure
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
void example(const char *c) {
strtol(c, NULL, 10);
}
The following code example passes the check and will not give a warning about this issue:
#include <errno.h>
#include <stdlib.h>
void example(const char *c) {
errno = 0;
long a = strtol(c, NULL, 10);
}