CERT-ERR30-C_d
In this section:
Synopsis
Check return of errno setting functions for values indicating failure.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
It is meaningful to inspect the value of errno only after establishing that the errno-setting function has returned an error. The return value of these functions must be inspected.
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(char *c) {
long a = strtol(c, NULL, 8);
return;
}
The following code example passes the check and will not give a warning about this issue:
#include <limits.h>
#include <stdlib.h>
void example(char *c) {
long a = strtol(c, NULL, 8);
if (a == ULONG_MAX) {
//handle error
}
return;
}