Skip to content

Commit e6f4d42

Browse files
committed
Java: Add test for exception flow.
1 parent f39dda9 commit e6f4d42

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

java/ql/test/library-tests/dataflow/capture/B.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,15 @@ String get() {
279279
sink(new MyLocal4().get()); // $ hasValueFlow=init
280280
sink(new MyLocal4(1).get()); // $ hasValueFlow=init
281281
}
282+
283+
void testCapturedCatch() {
284+
try {
285+
throw new RuntimeException(source("rte"));
286+
} catch (RuntimeException e) {
287+
Runnable r = () -> {
288+
sink(e.getMessage()); // $ hasValueFlow=rte
289+
};
290+
r.run();
291+
}
292+
}
282293
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import java.util.*;
2+
3+
public class A {
4+
static String source(String tag) { return tag; }
5+
6+
static void sink(Object o) { }
7+
8+
static class MyException extends RuntimeException {
9+
String msg;
10+
MyException(String msg) {
11+
this.msg = msg;
12+
}
13+
}
14+
15+
static class MySubException extends MyException {
16+
MySubException(String msg) {
17+
super(msg);
18+
}
19+
}
20+
21+
static class MyOtherException extends RuntimeException {
22+
String msg;
23+
MyOtherException(String msg) {
24+
this.msg = msg;
25+
}
26+
}
27+
28+
void throwSome(int i) {
29+
if (i == 1) throw new MyException(source("my"));
30+
if (i == 2) throw new MySubException(source("sub"));
31+
try {
32+
if (i == 3) throw new MyOtherException(source("other"));
33+
} catch (ClassCastException e) {
34+
}
35+
}
36+
37+
void foo(int i) {
38+
try {
39+
try {
40+
throwSome(i);
41+
} finally {
42+
}
43+
} catch (MySubException e) {
44+
sink(e.msg); // $ hasValueFlow=sub SPURIOUS: hasValueFlow=my
45+
} catch (MyException e) {
46+
sink(e.msg); // $ hasValueFlow=my SPURIOUS: hasValueFlow=sub
47+
} catch (MyOtherException e) {
48+
sink(e.msg); // $ hasValueFlow=other
49+
} catch (Exception e) {
50+
sink(((MyOtherException)e).msg); // $ SPURIOUS: hasValueFlow=other
51+
}
52+
}
53+
54+
void throwArg(String msg) {
55+
throw new MyException(msg);
56+
}
57+
58+
void catchSummary() {
59+
try {
60+
throwArg(source("arg"));
61+
} catch (MyException e) {
62+
sink(e.msg); // $ hasValueFlow=arg
63+
}
64+
}
65+
66+
void runCallback(Runnable r) {
67+
r.run();
68+
}
69+
70+
void catchCallback() {
71+
try {
72+
runCallback(() -> { throw new MyException(source("cb")); });
73+
} catch (MyException e) {
74+
sink(e.msg); // $ hasValueFlow=cb
75+
}
76+
77+
try {
78+
List<String> l = Arrays.asList(new String[] { "s" });
79+
l.forEach(s -> { throw new MyException(source("cb2")); });
80+
} catch (MyException e) {
81+
sink(e.msg); // $ hasValueFlow=cb2
82+
}
83+
}
84+
85+
void catchRuntimeException() {
86+
try {
87+
throw new RuntimeException(source("rte"));
88+
} catch (RuntimeException e) {
89+
sink(e.getMessage()); // $ hasValueFlow=rte
90+
}
91+
}
92+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import utils.test.InlineFlowTest
2+
import DefaultFlowTest
3+
import ValueFlow::PathGraph

0 commit comments

Comments
 (0)