File tree Expand file tree Collapse file tree 4 files changed +55
-7
lines changed
java/ql/test/library-tests Expand file tree Collapse file tree 4 files changed +55
-7
lines changed Original file line number Diff line number Diff line change
1
+ public class A {
2
+ Object source () { return null ; }
3
+ void sink (Object o ) { }
4
+
5
+ boolean isSafe (Object o ) { return o == null ; }
6
+
7
+ void foo () {
8
+ Object x = source ();
9
+ if (!isSafe (x )) {
10
+ x = null ;
11
+ }
12
+ sink (x );
13
+
14
+ x = source ();
15
+ if (!isSafe (x )) {
16
+ if (isSafe (x )) {
17
+ sink (x );
18
+ } else {
19
+ throw new RuntimeException ();
20
+ }
21
+ }
22
+ sink (x );
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ import java
2
+ import semmle.code.java.controlflow.Guards
3
+ import semmle.code.java.dataflow.DataFlow
4
+
5
+ private predicate isSafe ( Guard g , Expr checked , boolean branch ) {
6
+ exists ( MethodCall mc | g = mc |
7
+ mc .getMethod ( ) .hasName ( "isSafe" ) and
8
+ checked = mc .getAnArgument ( ) and
9
+ branch = true
10
+ )
11
+ }
12
+
13
+ module TestConfig implements DataFlow:: ConfigSig {
14
+ predicate isSource ( DataFlow:: Node source ) {
15
+ source .asExpr ( ) .( MethodCall ) .getMethod ( ) .hasName ( "source" )
16
+ }
17
+
18
+ predicate isSink ( DataFlow:: Node sink ) {
19
+ exists ( MethodCall mc | mc .getMethod ( ) .hasName ( "sink" ) and mc .getAnArgument ( ) = sink .asExpr ( ) )
20
+ }
21
+
22
+ predicate isBarrier ( DataFlow:: Node node ) {
23
+ node = DataFlow:: BarrierGuard< isSafe / 3 > :: getABarrierNode ( )
24
+ }
25
+ }
26
+
27
+ module Flow = DataFlow:: Global< TestConfig > ;
28
+
29
+ from DataFlow:: Node source , DataFlow:: Node sink
30
+ where Flow:: flow ( source , sink )
31
+ select source , sink
Original file line number Diff line number Diff line change @@ -24,13 +24,6 @@ case String s when isSafe(s):
24
24
break ;
25
25
26
26
}
27
-
28
- String s2 = "string" ;
29
-
30
- if (!isSafe (s2 )) {
31
- s2 = null ;
32
- }
33
- sink (s2 );
34
27
}
35
28
36
29
}
You can’t perform that action at this time.
0 commit comments