mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Java: Extend test to cover assertion-like barrier guards.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user