|
| 1 | +import java.util.*; |
| 2 | +import java.util.function.*; |
| 3 | + |
| 4 | +public class A { |
| 5 | + static String source(String tag) { return null; } |
| 6 | + |
| 7 | + static void sink(Object o) { } |
| 8 | + |
| 9 | + interface MyConsumer { |
| 10 | + void run(Object o); |
| 11 | + } |
| 12 | + |
| 13 | + void apply(MyConsumer f, Object x) { |
| 14 | + f.run(x); |
| 15 | + } |
| 16 | + |
| 17 | + void apply_wrap(MyConsumer f, Object x) { |
| 18 | + apply(f, x); |
| 19 | + } |
| 20 | + |
| 21 | + void testLambdaDispatch1() { |
| 22 | + apply_wrap(x -> { sink(x); }, source("A")); // $ hasValueFlow=A |
| 23 | + apply_wrap(x -> { sink(x); }, null); // no flow |
| 24 | + apply_wrap(x -> { }, source("B")); |
| 25 | + apply_wrap(x -> { }, null); |
| 26 | + } |
| 27 | + |
| 28 | + void forEach_wrap(List<Object> l, Consumer<Object> f) { |
| 29 | + l.forEach(f); |
| 30 | + } |
| 31 | + |
| 32 | + void testLambdaDispatch2() { |
| 33 | + List<Object> tainted = new ArrayList<>(); |
| 34 | + tainted.add(source("L")); |
| 35 | + List<Object> safe = new ArrayList<>(); |
| 36 | + forEach_wrap(safe, x -> { sink(x); }); // no flow |
| 37 | + forEach_wrap(tainted, x -> { sink(x); }); // $ hasValueFlow=L |
| 38 | + } |
| 39 | + |
| 40 | + static class TaintedClass { |
| 41 | + public String toString() { return source("TaintedClass"); } |
| 42 | + } |
| 43 | + |
| 44 | + static class SafeClass { |
| 45 | + public String toString() { return "safe"; } |
| 46 | + } |
| 47 | + |
| 48 | + String convertToString(Object o) { |
| 49 | + return o.toString(); |
| 50 | + } |
| 51 | + |
| 52 | + String convertToString_wrap(Object o) { |
| 53 | + return convertToString(o); |
| 54 | + } |
| 55 | + |
| 56 | + void testToString1() { |
| 57 | + String unused = convertToString_wrap(new TaintedClass()); |
| 58 | + sink(convertToString_wrap(new SafeClass())); // no flow |
| 59 | + } |
| 60 | +} |
0 commit comments