...
| Code Block | ||
|---|---|---|
| ||
if (message_type == value_1) {
/* Process message type 1 */
free(message);
message = NULL;
}
/* ...*/
if (message_type == value_2) {
/* Process message type 2 */
free(message);
message = NULL;
}
|
Exceptions
MEM01-EX1: If the variable goes out of scope immediately following the free(), it is not necessary to clear its value since it will no longer be accessible.
| Code Block | ||
|---|---|---|
| ||
void foo() {
char *str;
/* ... */
free(str);
return;
}
|
Risk Assessment
Setting pointers to NULL or to another valid value after memory has been freed is a simple and easily implemented solution for reducing dangling pointers. Dangling pointers can result in freeing memory multiple times or in writing to memory that has already been freed. Both of these problems can lead to an attacker executing arbitrary code with the permissions of the vulnerable process.
...