Skip to content

Commit c480431

Browse files
committed
C++: simplify cpp/guarded-free
This new form is more declarative by use of the `GuardCondition`. Thanks to the tireless effort of @MathiasVP!
1 parent 3195f0c commit c480431

File tree

1 file changed

+5
-28
lines changed

1 file changed

+5
-28
lines changed

cpp/ql/src/experimental/Best Practices/GuardedFree.ql

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,9 @@ class FreeCall extends FunctionCall {
1717
FreeCall() { this.getTarget().hasGlobalName("free") }
1818
}
1919

20-
from IfStmt stmt, FreeCall fc, Variable v
20+
from GuardCondition gc, FreeCall fc, Variable v, BasicBlock bb
2121
where
22-
stmt.getThen() = fc.getEnclosingStmt() and
23-
(
24-
stmt.getCondition() = v.getAnAccess() and
25-
fc.getArgument(0) = v.getAnAccess()
26-
or
27-
exists(PointerDereferenceExpr cond, PointerDereferenceExpr arg |
28-
fc.getArgument(0) = arg and
29-
stmt.getCondition() = cond and
30-
cond.getOperand+() = v.getAnAccess() and
31-
arg.getOperand+() = v.getAnAccess()
32-
)
33-
or
34-
exists(ArrayExpr cond, ArrayExpr arg |
35-
fc.getArgument(0) = arg and
36-
stmt.getCondition() = cond and
37-
cond.getArrayBase+() = v.getAnAccess() and
38-
arg.getArrayBase+() = v.getAnAccess()
39-
)
40-
or
41-
exists(NEExpr eq |
42-
fc.getArgument(0) = v.getAnAccess() and
43-
stmt.getCondition() = eq and
44-
eq.getAnOperand() = v.getAnAccess() and
45-
eq.getAnOperand().getValue() = "0"
46-
)
47-
)
48-
select stmt, "unnecessary NULL check before call to $@", fc, "free"
22+
gc.ensuresEq(v.getAnAccess(), 0, bb, false) and
23+
fc.getArgument(0) = v.getAnAccess() and
24+
bb = fc.getEnclosingStmt()
25+
select gc, "unnecessary NULL check before call to $@", fc, "free"

0 commit comments

Comments
 (0)