MEM-free-field
In this section:
Synopsis
A struct or a class field is possibly freed.
Enabled by default
Yes
Severity/Certainty
High/High

Full description
A struct or a class field is possibly freed. Fields are located in the middle of memory objects and thus cannot be freed. Additionally, erroneously using free() on fields might corrupt stdlib's memory bookkeeping, affecting heap memory. This check is identical to CERT-MEM34-C_b.
Coding standards
- CERT MEM34-C
Only free memory allocated dynamically
- CWE 590
Free of Memory not on the Heap
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
struct C{
int x;
};
int foo(struct C c) {
int *p = &c.x;
free(p);
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
struct C{
int *x;
};
int foo(struct C *c) {
int *p = (c->x);
free(p);
}