If a function generates error information, then that error information shall be tested
If a function generates error information, then that error information shall be tested.
If you do not check the return value of functions that indicate error information through their return values, your program can behave unexpectedly. Errors from these functions can propagate throughout the program causing incorrect output, security vulnerabilities, and possibly system failures.
For the errno-setting functions, to see if the function call
completed without errors, check errno for error values. The return values
of these errno-setting functions do not indicate errors. The return value
can be one of the following:
void
Even if an error occurs, the return value can be the same as the value from a
successful call. Such return values are called
in-band error indicators. For instance, strtol converts a string to
a long integer and returns the integer. If the result of conversion overflows, the
function returns LONG_MAX and sets errno to
ERANGE. However, the function can also return
LONG_MAX from a successful
conversion. Only by checking errno can you distinguish between an
error and a successful conversion.
For the errno-setting functions, you can determine if an
error occurred only by checking errno.
The checker raises a violation when:
You call sensitive standard functions that return information about possible errors and you do one of the following:
Ignore the return value.
You simply do not assign the return value to a variable, or explicitly cast
the return value to void.
Use an output from the function (return value or argument passed by reference) without testing the return value for errors.
The checker considers a function as sensitive if the function call is prone to failure because of reasons such as:
Exhausted system resources (for example, when allocating resources).
Changed privileges or permissions.
Tainted sources when reading, writing, or converting data from external sources.
Unsupported features despite an existing API.
Some of these functions can perform critical tasks such as:
Set privileges (for example, setuid)
Create a jail (for example, chroot)
Create a process (for example, fork)
Create a thread (for example, pthread_create)
Lock or unlock mutex (for example,
pthread_mutex_lock)
Lock or unlock memory segments (for example,
mlock)
For functions that are not critical, the checker allows casting the
function return value to void.
You call a function that sets errno to indicate error
conditions, but do not check errno after the call. For these
functions, checking errno is the only
reliable way to determine if an error
occurred.
Functions that set errno on errors include:
fgetwc, strtol, and
wcstol.
For a comprehensive list of functions, see documentation about errno.
POSIX®
errno-setting functions such as encrypt
and setkey.
If you expect a rule violation but do not see it, refer to Coding Standard Violations Not Displayed.
| Group: Language independent issues |
| Category: Required, Non-automated |