Skip to content

Commit 575b66a

Browse files
committed
C++: Clarify the recommendation and example.
1 parent dd95a2a commit 575b66a

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

cpp/ql/src/Critical/DoubleFree.qhelp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@ the program, or security vulnerabilities, by allowing an attacker to overwrite a
1414
</overview>
1515
<recommendation>
1616
<p>
17-
Ensure that all execution paths deallocate the allocated memory at most once. If possible, reassign
18-
the pointer to a null value after deallocating it. This will prevent double-free vulnerabilities since
19-
most deallocation functions will perform a null-pointer check before attempting to deallocate the memory.
17+
Ensure that all execution paths deallocate the allocated memory at most once. In complex cases it may
18+
help to reassign a pointer to a null value after deallocating it. This will prevent double-free vulnerabilities
19+
since most deallocation functions will perform a null-pointer check before attempting to deallocate memory.
2020
</p>
2121

2222
</recommendation>
23-
<example><sample src="DoubleFreeBad.cpp" />
23+
<example>
24+
<p>
25+
In the following example, <code>buff</code> is allocated and then freed twice:
26+
</p>
27+
<sample src="DoubleFreeBad.cpp" />
28+
<p>
29+
Reviewing the code above, the issue can be fixed by simply deleting the additonal call to
30+
<code>free(buff)</code>. Another buffer <code>new_buffer</code> is allocated, but we can see the intent was
31+
not to free <code>new_buffer</code> as this pointer is returned by the function.
32+
</p>
33+
<sample src="DoubleFreeGood.cpp" />
2434
</example>
2535
<references>
2636

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int* f() {
2+
int *buff = malloc(SIZE*sizeof(int));
3+
do_stuff(buff);
4+
free(buff); // GOOD: buff is only freed once.
5+
int *new_buffer = malloc(SIZE*sizeof(int));
6+
return new_buffer;
7+
}

0 commit comments

Comments
 (0)