Skip to content

Commit 3d88f08

Browse files
committed
C++: Include more expressions in the base case.
1 parent 983acf2 commit 3d88f08

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

cpp/ql/src/Security/CWE/CWE-191/UnsignedDifferenceExpressionComparedZero.ql

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,62 @@ predicate isGuarded(SubExpr sub, Expr left, Expr right) {
4141
Expr exprIsLeftOrLessBase(SubExpr sub) {
4242
interestingSubExpr(sub, _) and // Manual magic
4343
exists(Expr e | globalValueNumber(e).getAnExpr() = sub.getLeftOperand() |
44-
// result = sub.getLeftOperand() so result <= sub.getLeftOperand()
44+
// sub = e - x
45+
// result = e
46+
// so:
47+
// result <= e
4548
result = e
49+
or
50+
// sub = e - x
51+
// result = e & y
52+
// so:
53+
// result = e & y <= e
54+
result.(BitwiseAndExpr).getAnOperand() = e
55+
or
56+
exists(SubExpr s |
57+
// sub = e - x
58+
// result = s
59+
// s = e - y
60+
// y >= 0
61+
// so:
62+
// result = e - y <= e
63+
result = s and
64+
s.getLeftOperand() = e and
65+
lowerBound(s.getRightOperand().getFullyConverted()) >= 0
66+
)
67+
or
68+
exists(Expr other |
69+
// sub = e - x
70+
// result = a
71+
// a = e + y
72+
// y <= 0
73+
// so:
74+
// result = e + y <= e + 0 = e
75+
result.(AddExpr).hasOperands(e, other) and
76+
upperBound(other.getFullyConverted()) <= 0
77+
)
78+
or
79+
exists(DivExpr d |
80+
// sub = e - x
81+
// result = d
82+
// d = e / y
83+
// y >= 1
84+
// so:
85+
// result = e / y <= e / 1 = e
86+
result = d and
87+
d.getLeftOperand() = e and
88+
lowerBound(d.getRightOperand().getFullyConverted()) >= 1
89+
)
90+
or
91+
exists(RShiftExpr rs |
92+
// sub = e - x
93+
// result = rs
94+
// rs = e >> y
95+
// so:
96+
// result = e >> y <= e
97+
result = rs and
98+
rs.getLeftOperand() = e
99+
)
46100
)
47101
}
48102

0 commit comments

Comments
 (0)