Java: Extend test to cover assertion-like barrier guards.

This commit is contained in:
Anders Schack-Mulligen
2025-12-10 12:20:56 +01:00
parent 9cd2247b91
commit eaa96864f7
2 changed files with 36 additions and 1 deletions

View File

@@ -4,7 +4,13 @@ public class A {
boolean isSafe(Object o) { return o == null; }
void foo() {
void assertSafe(Object o) { if (o != null) throw new RuntimeException(); }
private boolean wrapIsSafe(Object o) { return isSafe(o); }
private void wrapAssertSafe(Object o) { assertSafe(o); }
void test1() {
Object x = source();
if (!isSafe(x)) {
x = null;
@@ -21,4 +27,23 @@ public class A {
}
sink(x);
}
void test2() {
Object x = source();
assertSafe(x);
sink(x);
}
void test3() {
Object x = source();
if (wrapIsSafe(x)) {
sink(x);
}
}
void test4() {
Object x = source();
wrapAssertSafe(x);
sink(x);
}
}

View File

@@ -10,6 +10,14 @@ private predicate isSafe(Guard g, Expr checked, boolean branch) {
)
}
private predicate assertSafe(Guard g, Expr checked, GuardValue gv) {
exists(MethodCall mc | g = mc |
mc.getMethod().hasName("assertSafe") and
checked = mc.getAnArgument() and
gv.getDualValue().isThrowsException()
)
}
module TestConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) {
source.asExpr().(MethodCall).getMethod().hasName("source")
@@ -21,6 +29,8 @@ module TestConfig implements DataFlow::ConfigSig {
predicate isBarrier(DataFlow::Node node) {
node = DataFlow::BarrierGuard<isSafe/3>::getABarrierNode()
or
node = DataFlow::BarrierGuardValue<assertSafe/3>::getABarrierNode()
}
}