Skip to content

Commit 810a046

Browse files
authored
Merge pull request #2346 from aschackmull/java/rangeanalysis-integral-fix2
Java: Fix range analysis bug where int was assumed.
2 parents f5598db + 81a9094 commit 810a046

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

java/ql/src/semmle/code/java/dataflow/RangeAnalysis.qll

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,16 @@ private class NarrowingCastExpr extends CastExpr {
375375
int getUpperBound() { typeBound(getType(), _, result) }
376376
}
377377

378+
/** Holds if `e >= 1` as determined by sign analysis. */
379+
private predicate strictlyPositiveIntegralExpr(Expr e) {
380+
strictlyPositive(e) and e.getType() instanceof IntegralType
381+
}
382+
383+
/** Holds if `e <= -1` as determined by sign analysis. */
384+
private predicate strictlyNegativeIntegralExpr(Expr e) {
385+
strictlyNegative(e) and e.getType() instanceof IntegralType
386+
}
387+
378388
/**
379389
* Holds if `e1 + delta` is a valid bound for `e2`.
380390
* - `upper = true` : `e2 <= e1 + delta`
@@ -400,13 +410,13 @@ private predicate boundFlowStep(Expr e2, Expr e1, int delta, boolean upper) {
400410
// `x instanceof ConstantIntegerExpr` is covered by valueFlowStep
401411
not x instanceof ConstantIntegerExpr and
402412
not e1 instanceof ConstantIntegerExpr and
403-
if strictlyPositive(x)
413+
if strictlyPositiveIntegralExpr(x)
404414
then upper = false and delta = 1
405415
else
406416
if positive(x)
407417
then upper = false and delta = 0
408418
else
409-
if strictlyNegative(x)
419+
if strictlyNegativeIntegralExpr(x)
410420
then upper = true and delta = -1
411421
else
412422
if negative(x)
@@ -429,13 +439,13 @@ private predicate boundFlowStep(Expr e2, Expr e1, int delta, boolean upper) {
429439
|
430440
// `x instanceof ConstantIntegerExpr` is covered by valueFlowStep
431441
not x instanceof ConstantIntegerExpr and
432-
if strictlyPositive(x)
442+
if strictlyPositiveIntegralExpr(x)
433443
then upper = true and delta = -1
434444
else
435445
if positive(x)
436446
then upper = true and delta = 0
437447
else
438-
if strictlyNegative(x)
448+
if strictlyNegativeIntegralExpr(x)
439449
then upper = false and delta = 1
440450
else
441451
if negative(x)

java/ql/test/query-tests/UselessComparisonTest/C.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,12 @@ public class C {
33
return (x < 0 || x > 1 || Double.isNaN(x)) ? Double.NaN :
44
x == 0 ? 0 : x == 1 ? 1 : 0.5;
55
}
6+
7+
void m2(double x) {
8+
if (x > 0) {
9+
double y = 1 - x;
10+
if (y > 0) { // OK
11+
}
12+
}
13+
}
614
}

0 commit comments

Comments
 (0)