Skip to content

Commit 291ea6f

Browse files
committed
Java: Move SSA data flow test and extend it to cover phi-read input edges.
1 parent 5379506 commit 291ea6f

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class A {
2+
Object source() { return null; }
3+
void sink(Object o) { }
4+
5+
boolean isSafe(Object o) { return o == null; }
6+
7+
void foo() {
8+
Object x = source();
9+
if (!isSafe(x)) {
10+
x = null;
11+
}
12+
sink(x);
13+
14+
x = source();
15+
if (!isSafe(x)) {
16+
if (isSafe(x)) {
17+
sink(x);
18+
} else {
19+
throw new RuntimeException();
20+
}
21+
}
22+
sink(x);
23+
}
24+
}

java/ql/test/library-tests/dataflow/ssa/test.expected

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java
2+
import semmle.code.java.controlflow.Guards
3+
import semmle.code.java.dataflow.DataFlow
4+
5+
private predicate isSafe(Guard g, Expr checked, boolean branch) {
6+
exists(MethodCall mc | g = mc |
7+
mc.getMethod().hasName("isSafe") and
8+
checked = mc.getAnArgument() and
9+
branch = true
10+
)
11+
}
12+
13+
module TestConfig implements DataFlow::ConfigSig {
14+
predicate isSource(DataFlow::Node source) {
15+
source.asExpr().(MethodCall).getMethod().hasName("source")
16+
}
17+
18+
predicate isSink(DataFlow::Node sink) {
19+
exists(MethodCall mc | mc.getMethod().hasName("sink") and mc.getAnArgument() = sink.asExpr())
20+
}
21+
22+
predicate isBarrier(DataFlow::Node node) {
23+
node = DataFlow::BarrierGuard<isSafe/3>::getABarrierNode()
24+
}
25+
}
26+
27+
module Flow = DataFlow::Global<TestConfig>;
28+
29+
from DataFlow::Node source, DataFlow::Node sink
30+
where Flow::flow(source, sink)
31+
select source, sink

java/ql/test/library-tests/pattern-switch/dfg/GuardTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ case String s when isSafe(s):
2424
break;
2525

2626
}
27-
28-
String s2 = "string";
29-
30-
if (!isSafe(s2)) {
31-
s2 = null;
32-
}
33-
sink(s2);
3427
}
3528

3629
}

0 commit comments

Comments
 (0)