CERT-ERR30-C_c
In this section:
Synopsis
Check errno only after the function called is an errno-setting function.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
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. errno should only be checked where a function is documents its use.
Coding standards
This check does not correspond to any coding standard rules.
Code examples
The following code example fails the check and will give a warning:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
void example(char *c) {
long l = strtol(c, NULL, 10);
printf("%s\n", c);
if (l == 0 && errno == 0) {
}
}
The following code example passes the check and will not give a warning about this issue:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
void example(char *c) {
long l = strtol(c, NULL, 10);
if (l == 0 && errno == 0) {
return;
}
printf("%s\n", c);
}