Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11279d4

Browse files
committed
Java: Autoformat Overflow.qll and add comment about imprecise float.
1 parent 0f5a3d3 commit 11279d4

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

java/ql/src/semmle/code/java/arithmetic/Overflow.qll

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ import java
22

33
/** A subclass of `PrimitiveType` with width-based ordering methods. */
44
class OrdPrimitiveType extends PrimitiveType {
5+
predicate widerThan(OrdPrimitiveType that) { getWidthRank() > that.getWidthRank() }
56

6-
predicate widerThan(OrdPrimitiveType that) {
7-
getWidthRank() > that.getWidthRank()
8-
}
9-
10-
predicate widerThanOrEqualTo(OrdPrimitiveType that) {
11-
getWidthRank() >= that.getWidthRank()
12-
}
7+
predicate widerThanOrEqualTo(OrdPrimitiveType that) { getWidthRank() >= that.getWidthRank() }
138

149
OrdPrimitiveType maxType(OrdPrimitiveType that) {
1510
(this.widerThan(that) and result = this)
@@ -38,7 +33,8 @@ class OrdPrimitiveType extends PrimitiveType {
3833
or
3934
(this.getName() = "int" and result = 2147483647.0)
4035
or
41-
(this.getName() = "long" and result = 9223372036854775807.0)
36+
// Long.MAX_VALUE is 9223372036854775807 but floating point only has 53 bits of precision.
37+
(this.getName() = "long" and result = 9223372036854776000.0)
4238
// don't try for floats and doubles
4339
}
4440

@@ -49,7 +45,8 @@ class OrdPrimitiveType extends PrimitiveType {
4945
or
5046
(this.getName() = "int" and result = -2147483648.0)
5147
or
52-
(this.getName() = "long" and result = -9223372036854775808.0)
48+
// Long.MIN_VALUE is -9223372036854775808 but floating point only has 53 bits of precision.
49+
(this.getName() = "long" and result = -9223372036854776000.0)
5350
// don't try for floats and doubles
5451
}
5552
}
@@ -75,17 +72,17 @@ class NumType extends Type {
7572
this.getOrdPrimitiveType().widerThanOrEqualTo(that.getOrdPrimitiveType())
7673
}
7774

78-
int getWidthRank() {
79-
result = this.getOrdPrimitiveType().getWidthRank()
80-
}
75+
int getWidthRank() { result = this.getOrdPrimitiveType().getWidthRank() }
8176
}
8277

8378
class ArithExpr extends Expr {
8479
ArithExpr() {
8580
(
8681
this instanceof UnaryAssignExpr or
87-
this instanceof AddExpr or this instanceof MulExpr or
88-
this instanceof SubExpr or this instanceof DivExpr
82+
this instanceof AddExpr or
83+
this instanceof MulExpr or
84+
this instanceof SubExpr or
85+
this instanceof DivExpr
8986
) and
9087
forall(Expr e | e = this.(BinaryExpr).getAnOperand() or e = this.(UnaryAssignExpr).getExpr() |
9188
e.getType() instanceof NumType
@@ -112,9 +109,7 @@ class ArithExpr extends Expr {
112109
/**
113110
* Gets the right-hand operand if this is a binary expression.
114111
*/
115-
Expr getRightOperand() {
116-
result = this.(BinaryExpr).getRightOperand()
117-
}
112+
Expr getRightOperand() { result = this.(BinaryExpr).getRightOperand() }
118113

119114
/** Gets an operand of this arithmetic expression. */
120115
Expr getAnOperand() {

0 commit comments

Comments
 (0)