mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Java: Autoformat 1.
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
* external/cwe/cwe-570
|
||||
* external/cwe/cwe-571
|
||||
*/
|
||||
|
||||
import semmle.code.java.controlflow.Guards
|
||||
import semmle.code.java.dataflow.SSA
|
||||
import semmle.code.java.dataflow.SignAnalysis
|
||||
@@ -19,45 +20,67 @@ import semmle.code.java.dataflow.RangeAnalysis
|
||||
|
||||
/** Holds if `cond` always evaluates to `isTrue`. */
|
||||
predicate constCond(BinaryExpr cond, boolean isTrue, Reason reason) {
|
||||
exists(ComparisonExpr comp, Expr lesser, Expr greater, Bound b, int d1, int d2, Reason r1, Reason r2 |
|
||||
exists(
|
||||
ComparisonExpr comp, Expr lesser, Expr greater, Bound b, int d1, int d2, Reason r1, Reason r2
|
||||
|
|
||||
comp = cond and
|
||||
lesser = comp.getLesserOperand() and
|
||||
greater = comp.getGreaterOperand() and
|
||||
bounded(lesser, b, d1, isTrue, r1) and
|
||||
bounded(greater, b, d2, isTrue.booleanNot(), r2) and
|
||||
(reason = r1 or reason = r2) and
|
||||
(r1 instanceof NoReason and r2 instanceof NoReason or not reason instanceof NoReason)
|
||||
|
|
||||
isTrue = true and comp.isStrict() and d1 < d2 or
|
||||
isTrue = true and not comp.isStrict() and d1 <= d2 or
|
||||
isTrue = false and comp.isStrict() and d1 >= d2 or
|
||||
(
|
||||
r1 instanceof NoReason and r2 instanceof NoReason
|
||||
or
|
||||
not reason instanceof NoReason
|
||||
)
|
||||
|
|
||||
isTrue = true and comp.isStrict() and d1 < d2
|
||||
or
|
||||
isTrue = true and not comp.isStrict() and d1 <= d2
|
||||
or
|
||||
isTrue = false and comp.isStrict() and d1 >= d2
|
||||
or
|
||||
isTrue = false and not comp.isStrict() and d1 > d2
|
||||
) or
|
||||
)
|
||||
or
|
||||
exists(EqualityTest eq, Expr lhs, Expr rhs |
|
||||
eq = cond and
|
||||
lhs = eq.getLeftOperand() and
|
||||
rhs = eq.getRightOperand()
|
||||
|
|
||||
|
|
||||
exists(Bound b, int d1, int d2, boolean upper, Reason r1, Reason r2 |
|
||||
bounded(lhs, b, d1, upper, r1) and
|
||||
bounded(rhs, b, d2, upper.booleanNot(), r2) and
|
||||
isTrue = eq.polarity().booleanNot() and
|
||||
(reason = r1 or reason = r2) and
|
||||
(r1 instanceof NoReason and r2 instanceof NoReason or not reason instanceof NoReason)
|
||||
|
|
||||
upper = true and d1 < d2 or // lhs <= b + d1 < b + d2 <= rhs
|
||||
upper = false and d1 > d2 // lhs >= b + d1 > b + d2 >= rhs
|
||||
) or
|
||||
(
|
||||
r1 instanceof NoReason and r2 instanceof NoReason
|
||||
or
|
||||
not reason instanceof NoReason
|
||||
)
|
||||
|
|
||||
upper = true and d1 < d2 // lhs <= b + d1 < b + d2 <= rhs
|
||||
or
|
||||
upper = false and d1 > d2 // lhs >= b + d1 > b + d2 >= rhs
|
||||
)
|
||||
or
|
||||
exists(Bound b, int d, Reason r1, Reason r2, Reason r3, Reason r4 |
|
||||
bounded(lhs, b, d, true, r1) and
|
||||
bounded(lhs, b, d, false, r2) and
|
||||
bounded(rhs, b, d, true, r3) and
|
||||
bounded(rhs, b, d, false, r4) and
|
||||
isTrue = eq.polarity()
|
||||
|
|
||||
|
|
||||
(reason = r1 or reason = r2 or reason = r3 or reason = r4) and
|
||||
(r1 instanceof NoReason and r2 instanceof NoReason and r3 instanceof NoReason and r4 instanceof NoReason or
|
||||
not reason instanceof NoReason)
|
||||
(
|
||||
r1 instanceof NoReason and
|
||||
r2 instanceof NoReason and
|
||||
r3 instanceof NoReason and
|
||||
r4 instanceof NoReason
|
||||
or
|
||||
not reason instanceof NoReason
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -73,34 +96,41 @@ Expr overFlowCand() {
|
||||
result = bin and
|
||||
positive(bin.getLeftOperand()) and
|
||||
positive(bin.getRightOperand())
|
||||
|
|
||||
|
|
||||
bin instanceof AddExpr or
|
||||
bin instanceof MulExpr or
|
||||
bin instanceof LShiftExpr
|
||||
) or
|
||||
)
|
||||
or
|
||||
exists(AssignOp op |
|
||||
result = op and
|
||||
positive(op.getDest()) and
|
||||
positive(op.getRhs())
|
||||
|
|
||||
|
|
||||
op instanceof AssignAddExpr or
|
||||
op instanceof AssignMulExpr or
|
||||
op instanceof AssignLShiftExpr
|
||||
) or
|
||||
)
|
||||
or
|
||||
exists(AddExpr add, CompileTimeConstantExpr c |
|
||||
result = add and
|
||||
add.hasOperands(overFlowCand(), c) and
|
||||
c.getIntValue() >= 0
|
||||
) or
|
||||
)
|
||||
or
|
||||
exists(AssignAddExpr add, CompileTimeConstantExpr c |
|
||||
result = add and
|
||||
add.getDest() = overFlowCand() and
|
||||
add.getRhs() = c and
|
||||
c.getIntValue() >= 0
|
||||
) or
|
||||
exists(SsaExplicitUpdate x | result = x.getAUse() and x.getDefiningExpr() = overFlowCand()) or
|
||||
result.(ParExpr).getExpr() = overFlowCand() or
|
||||
result.(AssignExpr).getRhs() = overFlowCand() or
|
||||
)
|
||||
or
|
||||
exists(SsaExplicitUpdate x | result = x.getAUse() and x.getDefiningExpr() = overFlowCand())
|
||||
or
|
||||
result.(ParExpr).getExpr() = overFlowCand()
|
||||
or
|
||||
result.(AssignExpr).getRhs() = overFlowCand()
|
||||
or
|
||||
result.(LocalVariableDeclExpr).getInit() = overFlowCand()
|
||||
}
|
||||
|
||||
@@ -110,15 +140,20 @@ Expr increaseOfVar(SsaVariable v) {
|
||||
result = add and
|
||||
positive(add.getDest()) and
|
||||
add.getRhs() = v.getAUse()
|
||||
) or
|
||||
)
|
||||
or
|
||||
exists(AddExpr add, Expr e |
|
||||
result = add and
|
||||
add.hasOperands(v.getAUse(), e) and
|
||||
positive(e)
|
||||
) or
|
||||
exists(SsaExplicitUpdate x | result = x.getAUse() and x.getDefiningExpr() = increaseOfVar(v)) or
|
||||
result.(ParExpr).getExpr() = increaseOfVar(v) or
|
||||
result.(AssignExpr).getRhs() = increaseOfVar(v) or
|
||||
)
|
||||
or
|
||||
exists(SsaExplicitUpdate x | result = x.getAUse() and x.getDefiningExpr() = increaseOfVar(v))
|
||||
or
|
||||
result.(ParExpr).getExpr() = increaseOfVar(v)
|
||||
or
|
||||
result.(AssignExpr).getRhs() = increaseOfVar(v)
|
||||
or
|
||||
result.(LocalVariableDeclExpr).getInit() = increaseOfVar(v)
|
||||
}
|
||||
|
||||
@@ -126,12 +161,12 @@ predicate overFlowTest(ComparisonExpr comp) {
|
||||
exists(SsaVariable v |
|
||||
comp.getLesserOperand() = increaseOfVar(v) and
|
||||
comp.getGreaterOperand() = v.getAUse()
|
||||
) or
|
||||
)
|
||||
or
|
||||
comp.getLesserOperand() = overFlowCand() and
|
||||
comp.getGreaterOperand().(IntegerLiteral).getIntValue() = 0
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Holds if `test` and `guard` are equality tests of the same integral variable v with constants `c1` and `c2`.
|
||||
*/
|
||||
@@ -160,12 +195,20 @@ predicate uselessEqTest(EqualityTest test, boolean testIsTrue, Guard guard) {
|
||||
from BinaryExpr test, boolean testIsTrue, string reason, ExprParent reasonElem
|
||||
where
|
||||
(
|
||||
if uselessEqTest(test, _, _) then
|
||||
exists(EqualityTest r | uselessEqTest(test, testIsTrue, r) and reason = ", because of $@" and reasonElem = r)
|
||||
else if constCondSimple(test, _) then
|
||||
(constCondSimple(test, testIsTrue) and reason = "" and reasonElem = test) // dummy reason element
|
||||
if uselessEqTest(test, _, _)
|
||||
then
|
||||
exists(EqualityTest r |
|
||||
uselessEqTest(test, testIsTrue, r) and reason = ", because of $@" and reasonElem = r
|
||||
)
|
||||
else
|
||||
exists(CondReason r | constCond(test, testIsTrue, r) and reason = ", because of $@" and reasonElem = r.getCond())
|
||||
if constCondSimple(test, _)
|
||||
then (
|
||||
constCondSimple(test, testIsTrue) and reason = "" and reasonElem = test
|
||||
) else
|
||||
// dummy reason element
|
||||
exists(CondReason r |
|
||||
constCond(test, testIsTrue, r) and reason = ", because of $@" and reasonElem = r.getCond()
|
||||
)
|
||||
) and
|
||||
not overFlowTest(test) and
|
||||
not exists(AssertStmt assert | assert.getExpr() = test.getParent*())
|
||||
|
||||
Reference in New Issue
Block a user