MEM-return-no-assign
In this section:
Synopsis
A function that allocates memory's return value is not stored.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
A function that allocates a memory's return value is not stored. Not storing the returned memory means that this memory cannot be tracked, and therefore deallocated. This will result in a memory leak.
Coding standards
- CWE 401
Improper Release of Memory Before Removing Last Reference ('Memory Leak')
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
int *allocating_fn(void) {
return malloc(sizeof(int));
}
void example(void) {
allocating_fn();
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
int *allocating_fn(void) {
return malloc(sizeof(int));
}
void example(void) {
int *p = allocating_fn();
}