Why is this an issue?
Stack allocated memory, like memory allocated with the functions alloca
, _alloca
, _malloca
,
__builtin_alloca
, is automatically released at the end of the function, and should not be released with free
. Explicitly
free
-ing such memory results in undefined behavior.
This rule raises issues when trying to release pointers to memory which is not owned, like stack allocated memory and function pointers.
Noncompliant code example
void fun() {
char *name = (char *) alloca(size);
// ...
free(name); // Noncompliant, memory allocated on the stack
char *name2 = "name";
// ...
free(name2); // Noncompliant, memory allocated on the stack
}
Compliant solution
void fun() {
char *name = (char *) alloca(size);
// ...
char *name2 = "name";
// ...
}