Skip to content

Commit da5c2d9

Browse files
committed
CPP: Use guard libraries to find equalities with zero.
1 parent df32e95 commit da5c2d9

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

cpp/ql/src/Critical/ScanfChecks.qll

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
private import cpp
22
private import semmle.code.cpp.commons.Scanf
3+
private import semmle.code.cpp.controlflow.IRGuards
4+
private import semmle.code.cpp.ir.ValueNumbering
35

46
private predicate exprInBooleanContext(Expr e) {
5-
e.getParent() instanceof BinaryLogicalOperation
6-
or
7-
e.getParent() instanceof UnaryLogicalOperation
8-
or
9-
e = any(IfStmt ifStmt).getCondition()
10-
or
11-
e = any(WhileStmt whileStmt).getCondition()
12-
or
13-
exists(EqualityOperation eqOp, Expr other |
14-
eqOp.hasOperands(e, other) and
15-
other.getValue() = "0"
16-
)
17-
or
18-
exists(Variable v |
19-
v.getAnAssignedValue() = e and
20-
forex(Expr use | use = v.getAnAccess() | exprInBooleanContext(use))
7+
exists(IRGuardCondition gc |
8+
exists(Instruction i, ConstantInstruction zero |
9+
zero.getValue() = "0" and
10+
i.getUnconvertedResultExpression() = e and
11+
gc.comparesEq(valueNumber(i).getAUse(), zero.getAUse(), 0, _, _)
12+
)
13+
or
14+
gc.getUnconvertedResultExpression() = e
2115
)
2216
}
2317

0 commit comments

Comments
 (0)