Skip to content

Commit 4227dd7

Browse files
authored
Merge pull request #20400 from MathiasVP/cleanup-incorrect-scanf-query
C++: Cleanup `ScanfChecks.qll`
2 parents edec76a + 72d7223 commit 4227dd7

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

cpp/ql/src/Critical/ScanfChecks.qll

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ private import semmle.code.cpp.controlflow.IRGuards
44
private import semmle.code.cpp.ir.ValueNumbering
55

66
private predicate exprInBooleanContext(Expr e) {
7-
exists(IRGuardCondition gc |
8-
exists(Instruction i |
9-
i.getUnconvertedResultExpression() = e and
10-
gc.comparesEq(valueNumber(i).getAUse(), 0, _, _)
11-
)
12-
or
13-
gc.getUnconvertedResultExpression() = e
7+
exists(IRGuardCondition gc, Instruction i |
8+
i.getUnconvertedResultExpression() = e and
9+
gc.comparesEq(valueNumber(i).getAUse(), 0, _, _)
1410
)
1511
}
1612

@@ -36,20 +32,18 @@ private string getEofValue() {
3632
* Holds if the value of `call` has been checked to not equal `EOF`.
3733
*/
3834
private predicate checkedForEof(ScanfFunctionCall call) {
39-
exists(IRGuardCondition gc |
40-
exists(CallInstruction i | i.getUnconvertedResultExpression() = call |
41-
exists(int val | gc.comparesEq(valueNumber(i).getAUse(), val, _, _) |
42-
// call == EOF
43-
val = getEofValue().toInt()
44-
or
45-
// call == [any positive number]
46-
val > 0
47-
)
35+
exists(IRGuardCondition gc, CallInstruction i | i.getUnconvertedResultExpression() = call |
36+
exists(int val | gc.comparesEq(valueNumber(i).getAUse(), val, _, _) |
37+
// call == EOF
38+
val = getEofValue().toInt()
4839
or
49-
exists(int val | gc.comparesLt(valueNumber(i).getAUse(), val, true, _) |
50-
// call < [any non-negative number] (EOF is guaranteed to be negative)
51-
val >= 0
52-
)
40+
// call == [any positive number]
41+
val > 0
42+
)
43+
or
44+
exists(int val | gc.comparesLt(valueNumber(i).getAUse(), val, true, _) |
45+
// call < [any non-negative number] (EOF is guaranteed to be negative)
46+
val >= 0
5347
)
5448
)
5549
}

0 commit comments

Comments
 (0)