Skip to content

Commit eaa9686

Browse files
committed
Java: Extend test to cover assertion-like barrier guards.
1 parent 9cd2247 commit eaa9686

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ void sink(Object o) { }
44

55
boolean isSafe(Object o) { return o == null; }
66

7-
void foo() {
7+
void assertSafe(Object o) { if (o != null) throw new RuntimeException(); }
8+
9+
private boolean wrapIsSafe(Object o) { return isSafe(o); }
10+
11+
private void wrapAssertSafe(Object o) { assertSafe(o); }
12+
13+
void test1() {
814
Object x = source();
915
if (!isSafe(x)) {
1016
x = null;
@@ -21,4 +27,23 @@ void foo() {
2127
}
2228
sink(x);
2329
}
30+
31+
void test2() {
32+
Object x = source();
33+
assertSafe(x);
34+
sink(x);
35+
}
36+
37+
void test3() {
38+
Object x = source();
39+
if (wrapIsSafe(x)) {
40+
sink(x);
41+
}
42+
}
43+
44+
void test4() {
45+
Object x = source();
46+
wrapAssertSafe(x);
47+
sink(x);
48+
}
2449
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ private predicate isSafe(Guard g, Expr checked, boolean branch) {
1010
)
1111
}
1212

13+
private predicate assertSafe(Guard g, Expr checked, GuardValue gv) {
14+
exists(MethodCall mc | g = mc |
15+
mc.getMethod().hasName("assertSafe") and
16+
checked = mc.getAnArgument() and
17+
gv.getDualValue().isThrowsException()
18+
)
19+
}
20+
1321
module TestConfig implements DataFlow::ConfigSig {
1422
predicate isSource(DataFlow::Node source) {
1523
source.asExpr().(MethodCall).getMethod().hasName("source")
@@ -21,6 +29,8 @@ module TestConfig implements DataFlow::ConfigSig {
2129

2230
predicate isBarrier(DataFlow::Node node) {
2331
node = DataFlow::BarrierGuard<isSafe/3>::getABarrierNode()
32+
or
33+
node = DataFlow::BarrierGuardValue<assertSafe/3>::getABarrierNode()
2434
}
2535
}
2636

0 commit comments

Comments
 (0)