Skip to content

Commit f508f8e

Browse files
committed
Java: address review comments
1 parent 25664d0 commit f508f8e

File tree

2 files changed

+54
-19
lines changed
  • java/ql/test/library-tests/dataflow/range-analysis-inline

2 files changed

+54
-19
lines changed

java/ql/test/library-tests/dataflow/range-analysis-inline/B.java

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class B {
44
// that should also be annotated.
55
static void bound(int b) { }
66

7-
public int forloop() {
7+
public int forLoop() {
88
int result = 0;
99
for (int i = 0;
1010
i < 10; // $ bound="i in [0..10]"
@@ -14,31 +14,31 @@ public int forloop() {
1414
return result; // $ bound="result in [0..9]"
1515
}
1616

17-
public int forloopexit() {
17+
public int forLoopExit() {
1818
int result = 0;
1919
for (; result < 10;) { // $ bound="result in [0..10]"
2020
result += 1; // $ bound="result in [0..9]"
2121
}
2222
return result; // $ bound="result = 10"
2323
}
2424

25-
public int forloopexitstep() {
25+
public int forLoopExitStep() {
2626
int result = 0;
2727
for (; result < 10;) { // $ bound="result in [0..12]"
2828
result += 3; // $ bound="result in [0..9]"
2929
}
3030
return result; // $ bound="result = 12"
3131
}
3232

33-
public int forloopexitupd() {
33+
public int forLoopExitUpd() {
3434
int result = 0;
3535
for (; result < 10; // $ bound="result in [0..10]"
3636
result++) { // $ bound="result in [0..9]"
3737
}
3838
return result; // $ bound="result = 10"
3939
}
4040

41-
public int forloopexitnested() {
41+
public int forLoopExitNested() {
4242
int result = 0;
4343
for (; result < 10;) {
4444
int i = 0;
@@ -50,7 +50,7 @@ public int forloopexitnested() {
5050
return result; // $ MISSING:bound="result = 12"
5151
}
5252

53-
public int emptyforloop() {
53+
public int emptyForLoop() {
5454
int result = 0;
5555
for (int i = 0; i < 0; // $ bound="i = 0"
5656
i++) { // $ bound="i in [0..-1]"
@@ -59,45 +59,45 @@ public int emptyforloop() {
5959
return result; // $ bound="result = 0"
6060
}
6161

62-
public int noloop() {
62+
public int noLoop() {
6363
int result = 0;
6464
result += 1; // $ bound="result = 0"
6565
return result; // $ bound="result = 1"
6666
}
6767

68-
public int foreachloop() {
68+
public int foreachLoop() {
6969
int result = 0;
7070
for (int i : new int[] {1, 2, 3, 4, 5}) {
7171
result = i;
7272
}
7373
return result;
7474
}
7575

76-
public int emptyforeachloop() {
76+
public int emptyForeachLoop() {
7777
int result = 0;
7878
for (int i : new int[] {}) {
7979
result = i;
8080
}
8181
return result;
8282
}
8383

84-
public int whileloop() {
84+
public int whileLoop() {
8585
int result = 100;
8686
while (result > 5) { // $ bound="result in [4..100]"
8787
result = result - 2; // $ bound="result in [6..100]"
8888
}
8989
return result; // $ bound="result = 4"
9090
}
9191

92-
public int oddwhileloop() {
92+
public int oddWhileLoop() {
9393
int result = 101;
9494
while (result > 5) { // $ bound="result in [5..101]"
9595
result = result - 2; // $ bound="result in [7..101]"
9696
}
9797
return result; // $ bound="result = 5"
9898
}
9999

100-
static void arraylength(int[] arr) {
100+
static void arrayLength(int[] arr) {
101101
bound(arr.length);
102102
for (int i = 0;
103103
i < arr.length;
@@ -106,15 +106,53 @@ static void arraylength(int[] arr) {
106106
}
107107
}
108108

109-
static int varbound(int b) {
109+
static int varBound(int b) {
110110
bound(b);
111111
int result = 0;
112112
for (int i = 0;
113113
i < b;
114114
i++) { // $ bound="i <= b - 1"
115115
result = i; // $ bound="i <= b - 1"
116116
}
117+
return result; // We cannot conclude anything here, since we do not know that b > 0
118+
}
119+
120+
static int varBoundPositiveGuard(int b) {
121+
bound(b);
122+
if (b > 0) {
123+
int result = 0;
124+
for (int i = 0;
125+
i < b;
126+
i++) { // $ bound="i <= b - 1"
127+
result = i; // $ bound="i <= b - 1"
128+
}
129+
return result; // $ MISSING: bound="result <= b - 1"
130+
} else {
131+
return 0;
132+
}
133+
}
134+
135+
static int varBoundPositiveGuardEarlyReturn(int b) {
136+
bound(b);
137+
if (b <= 0) return 0;
138+
int result = 0;
139+
for (int i = 0;
140+
i < b;
141+
i++) { // $ bound="i <= b - 1"
142+
result = i; // $ bound="i <= b - 1"
143+
}
117144
return result; // $ MISSING: bound="result <= b - 1"
118145
}
119146

147+
static int varBoundPositiveAssert(int b) {
148+
bound(b);
149+
assert b > 0;
150+
int result = 0;
151+
for (int i = 0;
152+
i < b;
153+
i++) { // $ bound="i <= b - 1"
154+
result = i; // $ bound="i <= b - 1"
155+
}
156+
return result; // $ MISSING: bound="result <= b - 1"
157+
}
120158
}

java/ql/test/library-tests/dataflow/range-analysis-inline/range.ql

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java
77
import semmle.code.java.dataflow.RangeAnalysis
88
private import TestUtilities.InlineExpectationsTest as IET
9-
private import semmle.code.java.dataflow.DataFlow
109

1110
module RangeTest implements IET::TestSig {
1211
string getARelevantTag() { result = "bound" }
@@ -29,10 +28,8 @@ module RangeTest implements IET::TestSig {
2928
)
3029
or
3130
// advanced bounds
32-
exists(
33-
Expr e, int delta, string deltaStr, boolean upper, string cmp, Bound b, Expr boundExpr
34-
|
35-
annotatedBound(e, b, boundExpr, delta, upper) and
31+
exists(Expr e, int delta, string deltaStr, boolean upper, string cmp, Expr boundExpr |
32+
annotatedBound(e, _, boundExpr, delta, upper) and
3633
e instanceof VarRead and
3734
e.getCompilationUnit().fromSource() and
3835
(
@@ -67,7 +64,7 @@ module RangeTest implements IET::TestSig {
6764
boundExpr = b.getExpr() and
6865
exists(Call c | c.getCallee().getName() = "bound" and c.getArgument(0) = boundExpr) and
6966
// non-trivial bound
70-
(DataFlow::localFlow(DataFlow::exprNode(boundExpr), DataFlow::exprNode(e)) implies delta != 0)
67+
not e = b.getExpr()
7168
}
7269
}
7370

0 commit comments

Comments
 (0)