mirror of
https://github.com/github/codeql.git
synced 2026-04-27 17:55:19 +02:00
C++: Add a second example.
This commit is contained in:
@@ -31,6 +31,15 @@ Reviewing the code above, the issue can be fixed by simply deleting the additona
|
||||
not to free <code>new_buffer</code> as this pointer is returned by the function.
|
||||
</p>
|
||||
<sample src="DoubleFreeGood.cpp" />
|
||||
In the next example, <code>task</code> may be deleted twice, if an exception occurs inside the <code>try</code>
|
||||
block after the first <code>delete</code>:
|
||||
</p>
|
||||
<sample src="DoubleFreeBad2.cpp" />
|
||||
<p>
|
||||
The problem can be solved by assigning a null value to the pointer after the first <code>delete</code>, as
|
||||
calling <code>delete</code> a second time on the null pointer is harmless.
|
||||
</p>
|
||||
<sample src="DoubleFreeGood2.cpp" />
|
||||
</example>
|
||||
<references>
|
||||
|
||||
|
||||
16
cpp/ql/src/Critical/DoubleFreeBad2.cpp
Normal file
16
cpp/ql/src/Critical/DoubleFreeBad2.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
void g() {
|
||||
MyTask *task = NULL;
|
||||
|
||||
try
|
||||
{
|
||||
task = new MyTask;
|
||||
|
||||
...
|
||||
|
||||
delete task;
|
||||
|
||||
...
|
||||
} catch (...) {
|
||||
delete task; // BAD: potential double-free
|
||||
}
|
||||
}
|
||||
17
cpp/ql/src/Critical/DoubleFreeGood2.cpp
Normal file
17
cpp/ql/src/Critical/DoubleFreeGood2.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
void g() {
|
||||
MyTask *task = NULL;
|
||||
|
||||
try
|
||||
{
|
||||
task = new MyTask;
|
||||
|
||||
...
|
||||
|
||||
delete task;
|
||||
task = NULL;
|
||||
|
||||
...
|
||||
} catch (...) {
|
||||
delete task; // GOOD: harmless if task is NULL
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user