Skip to content

Commit 5632dd5

Browse files
authored
Merge pull request #14275 from alexet/fix-use-after-free-fp
CPP: Fix some use after free FPs.
2 parents 2684a22 + 6b0ae0f commit 5632dd5

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

cpp/ql/src/Critical/UseAfterFree.ql

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ private predicate externalCallNeverDereferences(FormattingFunctionCall call, int
2929
)
3030
}
3131

32-
predicate isUse0(DataFlow::Node n, Expr e) {
33-
e = n.asExpr() and
32+
predicate isUse0(Expr e) {
3433
not isFree(_, e, _) and
3534
(
3635
e = any(PointerDereferenceExpr pde).getOperand()
@@ -43,7 +42,7 @@ predicate isUse0(DataFlow::Node n, Expr e) {
4342
or
4443
// Assume any function without a body will dereference the pointer
4544
exists(int i, Call call, Function f |
46-
n.asExpr() = call.getArgument(i) and
45+
e = call.getArgument(i) and
4746
f = call.getTarget() and
4847
not f.hasEntryPoint() and
4948
// Exclude known functions we know won't dereference the pointer.
@@ -57,7 +56,7 @@ module ParameterSinks {
5756
import semmle.code.cpp.ir.ValueNumbering
5857

5958
predicate flowsToUse(DataFlow::Node n) {
60-
isUse0(n, _)
59+
isUse0(n.asExpr())
6160
or
6261
exists(DataFlow::Node succ |
6362
flowsToUse(succ) and
@@ -90,7 +89,7 @@ module ParameterSinks {
9089
) {
9190
pragma[only_bind_out](source.asParameter()) = pragma[only_bind_out](init.getParameter()) and
9291
paramToUse(source, sink) and
93-
isUse0(sink, _)
92+
isUse0(sink.asExpr())
9493
}
9594

9695
private InitializeParameterInstruction getAnAlwaysDereferencedParameter0() {
@@ -139,7 +138,7 @@ module IsUse {
139138
private import semmle.code.cpp.ir.dataflow.internal.DataFlowImplCommon
140139

141140
predicate isUse(DataFlow::Node n, Expr e) {
142-
isUse0(n, e)
141+
isUse0(e) and n.asExpr() = e
143142
or
144143
exists(CallInstruction call, InitializeParameterInstruction init |
145144
n.asOperand().getDef().getUnconvertedResultExpression() = e and

cpp/ql/test/query-tests/Critical/MemoryFreed/MemoryFreed.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
| test_free.cpp:255:10:255:10 | p |
9797
| test_free.cpp:260:9:260:9 | p |
9898
| test_free.cpp:263:12:263:12 | p |
99+
| test_free.cpp:269:7:269:11 | ... = ... |
99100
| virtual.cpp:18:10:18:10 | a |
100101
| virtual.cpp:19:10:19:10 | c |
101102
| virtual.cpp:38:10:38:10 | b |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
| test_free.cpp:36:22:36:35 | ... = ... | This memory allocation may not be released at $@. | test_free.cpp:38:1:38:1 | return ... | this exit point |
2+
| test_free.cpp:267:12:267:17 | call to malloc | This memory allocation may not be released at $@. | test_free.cpp:270:1:270:1 | return ... | this exit point |

cpp/ql/test/query-tests/Critical/MemoryFreed/test_free.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,10 @@ void test_ref_delete(int *&p) {
261261
p = new int;
262262
use(p); // GOOD
263263
delete p; // GOOD
264+
}
265+
266+
void test_free_assign() {
267+
void *a = malloc(10);
268+
void *b;
269+
free(b = a); // GOOD
264270
}

0 commit comments

Comments
 (0)