Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7619275

Browse files
committed
Java: Fix range analysis bug in integral inequality bounds.
1 parent 39a45ce commit 7619275

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,11 @@ private predicate boundFlowStepSsa(
298298
)
299299
}
300300

301-
/** Holds if `v != e + delta` at `pos`. */
302-
private predicate unequalFlowStepSsa(
301+
/** Holds if `v != e + delta` at `pos` and `v` is of integral type. */
302+
private predicate unequalFlowStepIntegralSsa(
303303
SsaVariable v, SsaReadPosition pos, Expr e, int delta, Reason reason
304304
) {
305+
v.getSourceVariable().getType() instanceof IntegralType and
305306
exists(Guard guard, boolean testIsTrue |
306307
pos.hasReadOfVar(v) and
307308
guard = eqFlowCond(v, e, delta, false, testIsTrue) and
@@ -555,7 +556,7 @@ private predicate boundedSsa(
555556
boundedSsa(v, pos, b, d, upper, fromBackEdge, origdelta, r2) or
556557
boundedPhi(v, b, d, upper, fromBackEdge, origdelta, r2)
557558
|
558-
unequalSsa(v, pos, b, d, r1) and
559+
unequalIntegralSsa(v, pos, b, d, r1) and
559560
(
560561
upper = true and delta = d - 1
561562
or
@@ -570,11 +571,13 @@ private predicate boundedSsa(
570571
}
571572

572573
/**
573-
* Holds if `v != b + delta` at `pos`.
574+
* Holds if `v != b + delta` at `pos` and `v` is of integral type.
574575
*/
575-
private predicate unequalSsa(SsaVariable v, SsaReadPosition pos, Bound b, int delta, Reason reason) {
576+
private predicate unequalIntegralSsa(
577+
SsaVariable v, SsaReadPosition pos, Bound b, int delta, Reason reason
578+
) {
576579
exists(Expr e, int d1, int d2 |
577-
unequalFlowStepSsa(v, pos, e, d1, reason) and
580+
unequalFlowStepIntegralSsa(v, pos, e, d1, reason) and
578581
bounded(e, b, d2, true, _, _, _) and
579582
bounded(e, b, d2, false, _, _, _) and
580583
delta = d2 + d1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class C {
2+
double m1(double x) {
3+
return (x < 0 || x > 1 || Double.isNaN(x)) ? Double.NaN :
4+
x == 0 ? 0 : x == 1 ? 1 : 0.5;
5+
}
6+
}

0 commit comments

Comments
 (0)