Simplify query.

This commit is contained in:
Max Schaefer
2024-05-15 12:49:45 +01:00
parent d406646414
commit 3c47c1137d

View File

@@ -16,43 +16,20 @@
import java
import semmle.code.java.arithmetic.Overflow
int leftWidth(ComparisonExpr e) { result = e.getLeftOperand().getType().(NumType).getWidthRank() }
int widthRank(Expr e) { result = e.getType().(NumType).getWidthRank() }
int rightWidth(ComparisonExpr e) { result = e.getRightOperand().getType().(NumType).getWidthRank() }
abstract class WideningComparison extends BinaryExpr instanceof ComparisonExpr {
abstract Expr getNarrower();
abstract Expr getWider();
predicate wideningComparison(ComparisonExpr c, Expr lesserOperand, Expr greaterOperand) {
lesserOperand = c.getLesserOperand() and
greaterOperand = c.getGreaterOperand() and
widthRank(lesserOperand) < widthRank(greaterOperand)
}
class LTWideningComparison extends WideningComparison {
LTWideningComparison() {
(this instanceof LEExpr or this instanceof LTExpr) and
leftWidth(this) < rightWidth(this)
}
override Expr getNarrower() { result = this.getLeftOperand() }
override Expr getWider() { result = this.getRightOperand() }
}
class GTWideningComparison extends WideningComparison {
GTWideningComparison() {
(this instanceof GEExpr or this instanceof GTExpr) and
leftWidth(this) > rightWidth(this)
}
override Expr getNarrower() { result = this.getRightOperand() }
override Expr getWider() { result = this.getLeftOperand() }
}
from WideningComparison c, LoopStmt l
from ComparisonExpr c, LoopStmt l, Expr lesserOperand, Expr greaterOperand
where
wideningComparison(c, lesserOperand, greaterOperand) and
not c.getAnOperand().isCompileTimeConstant() and
l.getCondition().getAChildExpr*() = c
select c,
"Comparison between $@ of type " + c.getNarrower().getType().getName() + " and $@ of wider type " +
c.getWider().getType().getName() + ".", c.getNarrower(), "expression", c.getWider(),
"Comparison between $@ of type " + lesserOperand.getType().getName() + " and $@ of wider type " +
greaterOperand.getType().getName() + ".", lesserOperand, "expression", greaterOperand,
"expression"