Skip to content

Commit 60d07cf

Browse files
committed
Java: Clean up IntegerGuards.qll
1 parent 03321ff commit 60d07cf

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

java/ql/lib/semmle/code/java/dataflow/IntegerGuards.qll

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private import RangeUtils
1010
private import RangeAnalysis
1111

1212
/** Gets an expression that might have the value `i`. */
13-
private Expr exprWithIntValue(int i) {
13+
deprecated private Expr exprWithIntValue(int i) {
1414
result.(ConstantIntegerExpr).getIntValue() = i or
1515
result.(ChooseExpr).getAResultExpr() = exprWithIntValue(i)
1616
}
@@ -19,11 +19,11 @@ private Expr exprWithIntValue(int i) {
1919
* An expression for which the predicate `integerGuard` is relevant.
2020
* This includes `VarRead` and `MethodCall`.
2121
*/
22-
class IntComparableExpr extends Expr {
22+
deprecated class IntComparableExpr extends Expr {
2323
IntComparableExpr() { this instanceof VarRead or this instanceof MethodCall }
2424

2525
/** Gets an integer that is directly assigned to the expression in case of a variable; or zero. */
26-
int relevantInt() {
26+
deprecated int relevantInt() {
2727
exists(SsaExplicitUpdate ssa, SsaSourceVariable v |
2828
this = v.getAnAccess() and
2929
ssa.getSourceVariable() = v and
@@ -55,14 +55,18 @@ private predicate comparison(ComparisonExpr comp, boolean branch, Expr e1, Expr
5555
* Holds if `guard` evaluating to `branch` ensures that:
5656
* `e <= k` when `upper = true`
5757
* `e >= k` when `upper = false`
58+
*
59+
* Does _not_ include the constant comparison case where the guard directly
60+
* ensures `e == k`.
5861
*/
5962
pragma[nomagic]
6063
predicate rangeGuard(Expr guard, boolean branch, Expr e, int k, boolean upper) {
6164
exists(EqualityTest eqtest, Expr c |
6265
eqtest = guard and
6366
eqtest.hasOperands(e, c) and
6467
bounded(c, any(ZeroBound zb), k, upper, _) and
65-
branch = eqtest.polarity()
68+
branch = eqtest.polarity() and
69+
not c instanceof ConstantIntegerExpr
6670
)
6771
or
6872
exists(Expr c, int val, boolean strict, int d |
@@ -87,6 +91,30 @@ predicate rangeGuard(Expr guard, boolean branch, Expr e, int k, boolean upper) {
8791
}
8892

8993
/**
94+
* Gets an expression that directly tests whether a given expression, `e`, is
95+
* non-zero.
96+
*/
97+
Expr nonZeroGuard(Expr e, boolean branch) {
98+
exists(EqualityTest eqtest, boolean polarity, int k |
99+
eqtest = result and
100+
eqtest.hasOperands(e, any(ConstantIntegerExpr c | c.getIntValue() = k)) and
101+
polarity = eqtest.polarity()
102+
|
103+
k = 0 and branch = polarity.booleanNot()
104+
or
105+
k != 0 and branch = polarity
106+
)
107+
or
108+
exists(int val, boolean upper | rangeGuard(result, branch, e, val, upper) |
109+
upper = true and val < 0 // e <= val < 0 ==> e != 0
110+
or
111+
upper = false and val > 0 // e >= val > 0 ==> e != 0
112+
)
113+
}
114+
115+
/**
116+
* DEPRECATED.
117+
*
90118
* An expression that directly tests whether a given expression is equal to `k` or not.
91119
* The set of `k`s is restricted to those that are relevant for the expression or
92120
* have a direct comparison with the expression.
@@ -95,7 +123,7 @@ predicate rangeGuard(Expr guard, boolean branch, Expr e, int k, boolean upper) {
95123
* is true, and different from `k` if `is_k` is false.
96124
*/
97125
pragma[nomagic]
98-
Expr integerGuard(IntComparableExpr e, boolean branch, int k, boolean is_k) {
126+
deprecated Expr integerGuard(IntComparableExpr e, boolean branch, int k, boolean is_k) {
99127
exists(EqualityTest eqtest, boolean polarity |
100128
eqtest = result and
101129
eqtest.hasOperands(e, any(ConstantIntegerExpr c | c.getIntValue() = k)) and
@@ -119,13 +147,15 @@ Expr integerGuard(IntComparableExpr e, boolean branch, int k, boolean is_k) {
119147
}
120148

121149
/**
150+
* DEPRECATED: Use `rangeGuard` instead.
151+
*
122152
* A guard that splits the values of a variable into one range with an upper bound of `k-1`
123153
* and one with a lower bound of `k`.
124154
*
125155
* If `branch_with_lower_bound_k` is true then `result` is equivalent to `k <= x`
126156
* and if it is false then `result` is equivalent to `k > x`.
127157
*/
128-
Expr intBoundGuard(VarRead x, boolean branch_with_lower_bound_k, int k) {
158+
deprecated Expr intBoundGuard(VarRead x, boolean branch_with_lower_bound_k, int k) {
129159
exists(ComparisonExpr comp, ConstantIntegerExpr c, int val |
130160
comp = result and
131161
comp.hasOperands(x, c) and

java/ql/lib/semmle/code/java/dataflow/Nullness.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private Expr nonEmptyExpr() {
211211
// ...or it is guarded by a condition proving its length to be non-zero.
212212
exists(ConditionBlock cond, boolean branch, FieldAccess length |
213213
cond.controls(result.getBasicBlock(), branch) and
214-
cond.getCondition() = integerGuard(length, branch, 0, false) and
214+
cond.getCondition() = nonZeroGuard(length, branch) and
215215
length.getField().hasName("length") and
216216
length.getQualifier() = v.getAUse()
217217
)
@@ -241,7 +241,7 @@ private Expr nonEmptyExpr() {
241241
or
242242
// ...or a check on its `size`.
243243
exists(MethodCall size |
244-
c = integerGuard(size, branch, 0, false) and
244+
c = nonZeroGuard(size, branch) and
245245
size.getMethod().hasName("size") and
246246
size.getQualifier() = v.getAUse()
247247
)

0 commit comments

Comments
 (0)