mirror of
https://github.com/github/codeql.git
synced 2026-04-26 09:15:12 +02:00
java: test type tracking through flow summaries
This commit is contained in:
31
java/ql/test/library-tests/dispatch/CallableViaSummary.java
Normal file
31
java/ql/test/library-tests/dispatch/CallableViaSummary.java
Normal file
@@ -0,0 +1,31 @@
|
||||
import java.util.*;
|
||||
|
||||
public class CallableViaSummary {
|
||||
public interface Element {
|
||||
public void handle(String message);
|
||||
}
|
||||
|
||||
public void main(String[] args) {
|
||||
List<Element> elements = new ArrayList<>();
|
||||
|
||||
List<Element> elements2 = new ArrayList<>();
|
||||
|
||||
elements.add(new Element() {
|
||||
@Override
|
||||
public void handle(String message) {
|
||||
System.out.println(message);
|
||||
}
|
||||
});
|
||||
|
||||
elements.add(message -> System.out.println(message));
|
||||
|
||||
// This dispatches to the two added elements because
|
||||
// the summary of ArrayList causes flow via type tracking.
|
||||
elements.get(0).handle("Hello, world!");
|
||||
|
||||
// This does not dispatch to anything, showing that the
|
||||
// open-world assumption does not apply
|
||||
// (and hence that type tracking is necessary above).
|
||||
elements2.get(0).handle("Hello, world!");
|
||||
}
|
||||
}
|
||||
2
java/ql/test/library-tests/dispatch/viaSummary.expected
Normal file
2
java/ql/test/library-tests/dispatch/viaSummary.expected
Normal file
@@ -0,0 +1,2 @@
|
||||
| CallableViaSummary.java:24:9:24:47 | handle(...) | CallableViaSummary.java:15:25:15:30 | handle |
|
||||
| CallableViaSummary.java:24:9:24:47 | handle(...) | CallableViaSummary.java:20:22:20:59 | handle |
|
||||
9
java/ql/test/library-tests/dispatch/viaSummary.ql
Normal file
9
java/ql/test/library-tests/dispatch/viaSummary.ql
Normal file
@@ -0,0 +1,9 @@
|
||||
import java
|
||||
import semmle.code.java.dispatch.VirtualDispatch
|
||||
|
||||
from MethodAccess ma, Method m
|
||||
where
|
||||
m = viableImpl(ma) and
|
||||
m.fromSource() and
|
||||
ma.getFile().toString().matches("CallableViaSummary")
|
||||
select ma, m
|
||||
Reference in New Issue
Block a user