In this code, x is freed twice. What is the risk of this code?
x = malloc(200);
/* do something with x */
free(x);
/* do something else */
free(x);
-
This is a double free vulnerability and must be fixed.
-
The second call to free() will return an error.
-
There might be compiler warnings, but the program will run fine.
-
This is not a security issue.
Double free is a memory corruption vulnerability where free() is called twice on the same pointer. This can corrupt heap metadata, leading to crashes or arbitrary code execution. It is absolutely a security issue (D is wrong). The second free() does NOT necessarily return an error (B is wrong). The program will NOT run fine (C is wrong). Double free must be prevented by setting pointers to NULL after freeing or using memory-safe practices.
Calling free() twice on the same pointer is a classic double-free vulnerability: it corrupts heap metadata and can be exploited by attackers to achieve arbitrary code execution or crash the program, so it must be fixed. The second free() call does not reliably return an error — behavior is undefined and often silently corrupts memory rather than raising a clean error. It's not merely a compiler-warning-level cosmetic issue; real memory corruption can occur at runtime. And it is very much a recognized security issue (CWE-415), not a non-issue.