...
To eliminate double-free vulnerabilities, it is necessary to guarantee that dynamic memory is freed exactly one time. Programmers should be wary when freeing memory in a loop or conditional statement; if coded incorrectly, these constructs can lead to double-free vulnerabilities. It is also a common error to misuse the realloc()
function in a manner that results in double-free vulnerabilities (see MEM04-C. Do not perform zero length allocations).
...
Noncompliant Code Example
In this example, the memory referred to by x
may be freed twice: once if error_condition
is true and again at the end of the code.
...