@@ -72,6 +72,7 @@ private import ParityAnalysis
7272private import semmle.code.java.Reflection
7373private import semmle.code.java.Collections
7474private import semmle.code.java.Maps
75+ import Bound
7576
7677cached private module RangeAnalysisCache {
7778
@@ -409,61 +410,6 @@ private predicate boundFlowStepDiv(Expr e2, Expr e1, int factor) {
409410 )
410411}
411412
412- private newtype TBound =
413- TBoundZero ( ) or
414- TBoundSsa ( SsaVariable v ) { v .getSourceVariable ( ) .getType ( ) instanceof IntegralType } or
415- TBoundExpr ( Expr e ) { e .( FieldRead ) .getField ( ) instanceof ArrayLengthField and not exists ( SsaVariable v | e = v .getAUse ( ) ) }
416-
417- /**
418- * A bound that may be inferred for an expression plus/minus an integer delta.
419- */
420- abstract class Bound extends TBound {
421- abstract string toString ( ) ;
422- /** Gets an expression that equals this bound plus `delta`. */
423- abstract Expr getExpr ( int delta ) ;
424- /** Gets an expression that equals this bound. */
425- Expr getExpr ( ) {
426- result = getExpr ( 0 )
427- }
428- predicate hasLocationInfo ( string path , int sl , int sc , int el , int ec ) {
429- path = "" and sl = 0 and sc = 0 and el = 0 and ec = 0
430- }
431- }
432-
433- /**
434- * The bound that corresponds to the integer 0. This is used to represent all
435- * integer bounds as bounds are always accompanied by an added integer delta.
436- */
437- class ZeroBound extends Bound , TBoundZero {
438- override string toString ( ) { result = "0" }
439- override Expr getExpr ( int delta ) { result .( ConstantIntegerExpr ) .getIntValue ( ) = delta }
440- }
441-
442- /**
443- * A bound corresponding to the value of an SSA variable.
444- */
445- class SsaBound extends Bound , TBoundSsa {
446- /** Gets the SSA variable that equals this bound. */
447- SsaVariable getSsa ( ) { this = TBoundSsa ( result ) }
448- override string toString ( ) { result = getSsa ( ) .toString ( ) }
449- override Expr getExpr ( int delta ) { result = getSsa ( ) .getAUse ( ) and delta = 0 }
450- override predicate hasLocationInfo ( string path , int sl , int sc , int el , int ec ) {
451- getSsa ( ) .getLocation ( ) .hasLocationInfo ( path , sl , sc , el , ec )
452- }
453- }
454-
455- /**
456- * A bound that corresponds to the value of a specific expression that might be
457- * interesting, but isn't otherwise represented by the value of an SSA variable.
458- */
459- class ExprBound extends Bound , TBoundExpr {
460- override string toString ( ) { result = getExpr ( ) .toString ( ) }
461- override Expr getExpr ( int delta ) { this = TBoundExpr ( result ) and delta = 0 }
462- override predicate hasLocationInfo ( string path , int sl , int sc , int el , int ec ) {
463- getExpr ( ) .hasLocationInfo ( path , sl , sc , el , ec )
464- }
465- }
466-
467413/**
468414 * Holds if `b + delta` is a valid bound for `v` at `pos`.
469415 * - `upper = true` : `v <= b + delta`
@@ -632,7 +578,7 @@ private predicate baseBound(Expr e, int b, boolean upper) {
632578 */
633579private predicate safeNarrowingCast ( NarrowingCastExpr cast , boolean upper ) {
634580 exists ( int bound |
635- bounded ( cast .getExpr ( ) , TBoundZero ( ) , bound , upper , _, _, _)
581+ bounded ( cast .getExpr ( ) , any ( ZeroBound zb ) , bound , upper , _, _, _)
636582 |
637583 upper = true and bound <= cast .getUpperBound ( ) or
638584 upper = false and bound >= cast .getLowerBound ( )
0 commit comments