mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Rangeanalysis: Minor refactor for bound steps.
This commit is contained in:
@@ -682,12 +682,15 @@ module RangeStage<
|
||||
* - `upper = false` : `e2 >= e1 + delta`
|
||||
*/
|
||||
private predicate boundFlowStep(Sem::Expr e2, Sem::Expr e1, D::Delta delta, boolean upper) {
|
||||
// Constants have easy, base-case bounds, so let's not infer any recursive bounds.
|
||||
not e2 instanceof Sem::ConstantIntegerExpr and
|
||||
(
|
||||
valueFlowStep(e2, e1, delta) and
|
||||
(upper = true or upper = false)
|
||||
upper = [true, false]
|
||||
or
|
||||
e2.(SafeCastExpr).getOperand() = e1 and
|
||||
delta = D::fromInt(0) and
|
||||
(upper = true or upper = false)
|
||||
upper = [true, false]
|
||||
or
|
||||
javaCompatibility() and
|
||||
exists(Sem::Expr x, Sem::SubExpr sub |
|
||||
@@ -732,10 +735,12 @@ module RangeStage<
|
||||
upper = false
|
||||
or
|
||||
additionalBoundFlowStep(e2, e1, delta, upper)
|
||||
)
|
||||
}
|
||||
|
||||
/** Holds if `e2 = e1 * factor` and `factor > 0`. */
|
||||
private predicate boundFlowStepMul(Sem::Expr e2, Sem::Expr e1, D::Delta factor) {
|
||||
not e2 instanceof Sem::ConstantIntegerExpr and
|
||||
exists(Sem::ConstantIntegerExpr c, int k | k = c.getIntValue() and k > 0 |
|
||||
e2.(Sem::MulExpr).hasOperands(e1, c) and factor = D::fromInt(k)
|
||||
or
|
||||
@@ -755,6 +760,7 @@ module RangeStage<
|
||||
* therefore only valid for non-negative numbers.
|
||||
*/
|
||||
private predicate boundFlowStepDiv(Sem::Expr e2, Sem::Expr e1, D::Delta factor) {
|
||||
not e2 instanceof Sem::ConstantIntegerExpr and
|
||||
Sem::getExprType(e2) instanceof Sem::IntegerType and
|
||||
exists(Sem::ConstantIntegerExpr c, D::Delta k |
|
||||
k = D::fromInt(c.getIntValue()) and D::toFloat(k) > 0
|
||||
@@ -1149,8 +1155,6 @@ module RangeStage<
|
||||
or
|
||||
exists(Sem::Expr mid, D::Delta d1, D::Delta d2 |
|
||||
boundFlowStep(e, mid, d1, upper) and
|
||||
// Constants have easy, base-case bounds, so let's not infer any recursive bounds.
|
||||
not e instanceof Sem::ConstantIntegerExpr and
|
||||
bounded(mid, b, d2, upper, fromBackEdge, origdelta, reason) and
|
||||
// upper = true: e <= mid + d1 <= b + d1 + d2 = b + delta
|
||||
// upper = false: e >= mid + d1 >= b + d1 + d2 = b + delta
|
||||
@@ -1164,7 +1168,6 @@ module RangeStage<
|
||||
or
|
||||
exists(Sem::Expr mid, D::Delta factor, D::Delta d |
|
||||
boundFlowStepMul(e, mid, factor) and
|
||||
not e instanceof Sem::ConstantIntegerExpr and
|
||||
bounded(mid, b, d, upper, fromBackEdge, origdelta, reason) and
|
||||
b instanceof SemZeroBound and
|
||||
delta = D::fromFloat(D::toFloat(d) * D::toFloat(factor))
|
||||
@@ -1172,7 +1175,6 @@ module RangeStage<
|
||||
or
|
||||
exists(Sem::Expr mid, D::Delta factor, D::Delta d |
|
||||
boundFlowStepDiv(e, mid, factor) and
|
||||
not e instanceof Sem::ConstantIntegerExpr and
|
||||
bounded(mid, b, d, upper, fromBackEdge, origdelta, reason) and
|
||||
b instanceof SemZeroBound and
|
||||
D::toFloat(d) >= 0 and
|
||||
|
||||
Reference in New Issue
Block a user