MEM-double-free-some
In this section:
Synopsis
A memory location is freed more than once on some paths but not on others.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
There is a path through the code where a memory location is attempted to be freed after it has already been freed earlier. This will most likely cause an application crash on this path. This check is identical to MISRAC2012-Rule-22.2_b.
Coding standards
- CERT MEM31-C
Free dynamically allocated memory exactly once
- CWE 415
Double Free
- MISRA C:2012 Rule-22.2
(Mandatory) A block of memory shall only be freed if it was allocated by means of a Standard Library function
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
void example(void) {
int *ptr = (int*)malloc(sizeof(int));
free(ptr);
if(rand() % 2 == 0)
{
free(ptr);
}
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
void example(void) {
int *ptr = (int*)malloc(sizeof(int));
if(rand() % 2 == 0)
{
free(ptr);
}
else
{
free(ptr);
}
}