mirror of
https://github.com/github/codeql.git
synced 2026-04-26 17:25:19 +02:00
Simplify query.
This commit is contained in:
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user