|
16 | 16 | import java
|
17 | 17 | import semmle.code.java.arithmetic.Overflow
|
18 | 18 |
|
19 |
| -int leftWidth(ComparisonExpr e) { result = e.getLeftOperand().getType().(NumType).getWidthRank() } |
| 19 | +int widthRank(Expr e) { result = e.getType().(NumType).getWidthRank() } |
20 | 20 |
|
21 |
| -int rightWidth(ComparisonExpr e) { result = e.getRightOperand().getType().(NumType).getWidthRank() } |
22 |
| - |
23 |
| -abstract class WideningComparison extends BinaryExpr instanceof ComparisonExpr { |
24 |
| - abstract Expr getNarrower(); |
25 |
| - |
26 |
| - abstract Expr getWider(); |
27 |
| -} |
28 |
| - |
29 |
| -class LTWideningComparison extends WideningComparison { |
30 |
| - LTWideningComparison() { |
31 |
| - (this instanceof LEExpr or this instanceof LTExpr) and |
32 |
| - leftWidth(this) < rightWidth(this) |
33 |
| - } |
34 |
| - |
35 |
| - override Expr getNarrower() { result = this.getLeftOperand() } |
36 |
| - |
37 |
| - override Expr getWider() { result = this.getRightOperand() } |
38 |
| -} |
39 |
| - |
40 |
| -class GTWideningComparison extends WideningComparison { |
41 |
| - GTWideningComparison() { |
42 |
| - (this instanceof GEExpr or this instanceof GTExpr) and |
43 |
| - leftWidth(this) > rightWidth(this) |
44 |
| - } |
45 |
| - |
46 |
| - override Expr getNarrower() { result = this.getRightOperand() } |
47 |
| - |
48 |
| - override Expr getWider() { result = this.getLeftOperand() } |
| 21 | +predicate wideningComparison(ComparisonExpr c, Expr lesserOperand, Expr greaterOperand) { |
| 22 | + lesserOperand = c.getLesserOperand() and |
| 23 | + greaterOperand = c.getGreaterOperand() and |
| 24 | + widthRank(lesserOperand) < widthRank(greaterOperand) |
49 | 25 | }
|
50 | 26 |
|
51 |
| -from WideningComparison c, LoopStmt l |
| 27 | +from ComparisonExpr c, LoopStmt l, Expr lesserOperand, Expr greaterOperand |
52 | 28 | where
|
| 29 | + wideningComparison(c, lesserOperand, greaterOperand) and |
53 | 30 | not c.getAnOperand().isCompileTimeConstant() and
|
54 | 31 | l.getCondition().getAChildExpr*() = c
|
55 | 32 | select c,
|
56 |
| - "Comparison between $@ of type " + c.getNarrower().getType().getName() + " and $@ of wider type " + |
57 |
| - c.getWider().getType().getName() + ".", c.getNarrower(), "expression", c.getWider(), |
| 33 | + "Comparison between $@ of type " + lesserOperand.getType().getName() + " and $@ of wider type " + |
| 34 | + greaterOperand.getType().getName() + ".", lesserOperand, "expression", greaterOperand, |
58 | 35 | "expression"
|
0 commit comments