mirror of
https://github.com/github/codeql.git
synced 2025-12-24 04:36:35 +01:00
Java: extend typeflow tests to cover union types.
This commit is contained in:
54
java/ql/test/library-tests/typeflow/UnionTypes.java
Normal file
54
java/ql/test/library-tests/typeflow/UnionTypes.java
Normal file
@@ -0,0 +1,54 @@
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class UnionTypes {
|
||||
public void m1() {
|
||||
m1_map_put(new LinkedHashMap<>(), "k", "v");
|
||||
m1_map_put(new ConcurrentHashMap<>(), "k", "v");
|
||||
}
|
||||
|
||||
private void m1_map_put(Map<String, String> m, String k, String v) {
|
||||
m.put(k, v);
|
||||
}
|
||||
|
||||
static class Sup { }
|
||||
interface Inter { }
|
||||
|
||||
static class A1 extends Sup implements Inter { }
|
||||
static class A2 extends Sup implements Inter { }
|
||||
static class A3 extends Sup { }
|
||||
static class A4 extends Sup implements Inter { }
|
||||
static class A2sub extends A2 { }
|
||||
|
||||
private void m2(boolean b) {
|
||||
scc1(new A1(), 10);
|
||||
Sup x = b ? new A2() : new A3();
|
||||
scc2(x, 10);
|
||||
}
|
||||
|
||||
private void scc1(Sup x1, int i) {
|
||||
scc2(x1, i);
|
||||
}
|
||||
|
||||
private void scc2(Sup x2, int i) {
|
||||
scc3(x2, i);
|
||||
}
|
||||
|
||||
private void scc3(Sup x3, int i) {
|
||||
next(x3);
|
||||
if (i > 0)
|
||||
scc1(x3, --i);
|
||||
}
|
||||
|
||||
private void next(Sup x) {
|
||||
if (x instanceof Inter) {
|
||||
x.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
void m3(Object d) {
|
||||
if (d instanceof A1 || d instanceof A2 || d instanceof A3) {
|
||||
d.hashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,3 +13,4 @@
|
||||
| A.java:67:22:67:22 | x | Integer | false |
|
||||
| A.java:70:23:70:24 | x2 | Integer | false |
|
||||
| A.java:92:18:92:18 | n | Integer | false |
|
||||
| UnionTypes.java:45:7:45:7 | x | Inter | false |
|
||||
|
||||
24
java/ql/test/library-tests/typeflow/uniontypeflow.expected
Normal file
24
java/ql/test/library-tests/typeflow/uniontypeflow.expected
Normal file
@@ -0,0 +1,24 @@
|
||||
| UnionTypes.java:11:5:11:5 | m | 2 | ConcurrentHashMap<String,String> | false |
|
||||
| UnionTypes.java:11:5:11:5 | m | 2 | LinkedHashMap<String,String> | false |
|
||||
| UnionTypes.java:26:10:26:10 | x | 2 | A2 | true |
|
||||
| UnionTypes.java:26:10:26:10 | x | 2 | A3 | false |
|
||||
| UnionTypes.java:30:10:30:11 | x1 | 3 | A1 | false |
|
||||
| UnionTypes.java:30:10:30:11 | x1 | 3 | A2 | true |
|
||||
| UnionTypes.java:30:10:30:11 | x1 | 3 | A3 | false |
|
||||
| UnionTypes.java:34:10:34:11 | x2 | 3 | A1 | false |
|
||||
| UnionTypes.java:34:10:34:11 | x2 | 3 | A2 | true |
|
||||
| UnionTypes.java:34:10:34:11 | x2 | 3 | A3 | false |
|
||||
| UnionTypes.java:38:10:38:11 | x3 | 3 | A1 | false |
|
||||
| UnionTypes.java:38:10:38:11 | x3 | 3 | A2 | true |
|
||||
| UnionTypes.java:38:10:38:11 | x3 | 3 | A3 | false |
|
||||
| UnionTypes.java:40:12:40:13 | x3 | 3 | A1 | false |
|
||||
| UnionTypes.java:40:12:40:13 | x3 | 3 | A2 | true |
|
||||
| UnionTypes.java:40:12:40:13 | x3 | 3 | A3 | false |
|
||||
| UnionTypes.java:44:9:44:9 | x | 3 | A1 | false |
|
||||
| UnionTypes.java:44:9:44:9 | x | 3 | A2 | true |
|
||||
| UnionTypes.java:44:9:44:9 | x | 3 | A3 | false |
|
||||
| UnionTypes.java:45:7:45:7 | x | 2 | A1 | false |
|
||||
| UnionTypes.java:45:7:45:7 | x | 2 | A2 | true |
|
||||
| UnionTypes.java:51:7:51:7 | d | 3 | A1 | false |
|
||||
| UnionTypes.java:51:7:51:7 | d | 3 | A2 | false |
|
||||
| UnionTypes.java:51:7:51:7 | d | 3 | A3 | false |
|
||||
10
java/ql/test/library-tests/typeflow/uniontypeflow.ql
Normal file
10
java/ql/test/library-tests/typeflow/uniontypeflow.ql
Normal file
@@ -0,0 +1,10 @@
|
||||
import java
|
||||
import semmle.code.java.dataflow.TypeFlow
|
||||
|
||||
int countUnionTypes(Expr e) {
|
||||
result = strictcount(RefType t, boolean exact | exprUnionTypeFlow(e, t, exact))
|
||||
}
|
||||
|
||||
from RValue e, RefType t, boolean exact
|
||||
where exprUnionTypeFlow(e, t, exact)
|
||||
select e, countUnionTypes(e), t.toString(), exact
|
||||
Reference in New Issue
Block a user