LIB-return-error
Synopsis
The return value for a library function that might return an error value is not used.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
The return value for a library function that might return an error value is not used. Because this function might fail, the programmer should inspect the return value to find any error values, to avoid a crash or unexpected behavior. These functions are isnpected: malloc(), calloc(), realloc(), and mktime(). This check is identical to MISRAC2004-16.10, MISRAC++2008-0-3-2.
Coding standards
- CWE 252
Unchecked Return Value
- CWE 394
Unexpected Status Code or Return Value
- MISRA C:2004 16.10
(Required) If a function returns error information, then that error information shall be tested.
- MISRA C++ 2008 0-3-2
(Required) If a function generates error information, then that error information shall be tested.
Code examples
The following code example fails the check and will give a warning:
void example(void) {
malloc(sizeof(int)); // This function could fail,
// and the return value is
// not checked
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
void example(void) {
int *x = malloc(sizeof(int)); // OK - return value
// is stored
}