1111 * modularity
1212 * external/cwe/cwe-485
1313 */
14+
1415import java
1516import semmle.code.java.dataflow.DefUse
1617
1718predicate relevantType ( RefType t ) {
18- t instanceof Array or
19+ t instanceof Array
20+ or
1921 exists ( RefType sup | sup = t .getASupertype * ( ) .getSourceDeclaration ( ) |
2022 sup .hasQualifiedName ( "java.util" , "Map" ) or
2123 sup .hasQualifiedName ( "java.util" , "Collection" )
2224 )
2325}
2426
2527predicate modifyMethod ( Method m ) {
26- relevantType ( m .getDeclaringType ( ) ) and (
27- m .hasName ( "add" ) or m .hasName ( "addAll" ) or
28- m .hasName ( "put" ) or m .hasName ( "putAll" ) or
29- m .hasName ( "push" ) or m .hasName ( "pop" ) or
30- m .hasName ( "remove" ) or m .hasName ( "removeAll" ) or
31- m .hasName ( "clear" ) or m .hasName ( "set" )
28+ relevantType ( m .getDeclaringType ( ) ) and
29+ (
30+ m .hasName ( "add" ) or
31+ m .hasName ( "addAll" ) or
32+ m .hasName ( "put" ) or
33+ m .hasName ( "putAll" ) or
34+ m .hasName ( "push" ) or
35+ m .hasName ( "pop" ) or
36+ m .hasName ( "remove" ) or
37+ m .hasName ( "removeAll" ) or
38+ m .hasName ( "clear" ) or
39+ m .hasName ( "set" )
3240 )
3341}
3442
@@ -47,36 +55,35 @@ predicate returnsArray(Callable c, Field f) {
4755}
4856
4957predicate mayWriteToArray ( Expr modified ) {
50- writesToArray ( modified ) or
51-
58+ writesToArray ( modified )
59+ or
5260 // x = __y__; x[0] = 1;
5361 exists ( AssignExpr e , LocalVariableDecl v | e .getDest ( ) = v .getAnAccess ( ) |
5462 modified = e .getSource ( ) and
5563 mayWriteToArray ( v .getAnAccess ( ) )
56- ) or
57-
64+ )
65+ or
5866 // int[] x = __y__; x[0] = 1;
5967 exists ( LocalVariableDeclExpr e , Variable v | e .getVariable ( ) = v |
6068 modified = e .getInit ( ) and
6169 mayWriteToArray ( v .getAnAccess ( ) )
62- ) or
63-
70+ )
71+ or
6472 // return __array__; ... method()[1] = 0
6573 exists ( ReturnStmt rs | modified = rs .getResult ( ) and relevantType ( modified .getType ( ) ) |
6674 exists ( Callable enclosing , MethodAccess ma |
6775 enclosing = rs .getEnclosingCallable ( ) and ma .getMethod ( ) = enclosing
68- |
76+ |
6977 mayWriteToArray ( ma )
7078 )
7179 )
7280}
7381
7482predicate writesToArray ( Expr array ) {
7583 relevantType ( array .getType ( ) ) and
76- (
77- exists ( Assignment a , ArrayAccess access | a .getDest ( ) = access | access .getArray ( ) = array ) ) or
78- exists ( MethodAccess ma | ma .getQualifier ( ) = array | modifyMethod ( ma .getMethod ( ) )
79- )
84+ ( exists ( Assignment a , ArrayAccess access | a .getDest ( ) = access | access .getArray ( ) = array ) )
85+ or
86+ exists ( MethodAccess ma | ma .getQualifier ( ) = array | modifyMethod ( ma .getMethod ( ) ) )
8087}
8188
8289VarAccess modificationAfter ( VarAccess v ) {
@@ -85,14 +92,14 @@ VarAccess modificationAfter(VarAccess v) {
8592}
8693
8794VarAccess varPassedInto ( Callable c , int i ) {
88- exists ( Call call | call .getCallee ( ) = c |
89- call .getArgument ( i ) = result
90- )
95+ exists ( Call call | call .getCallee ( ) = c | call .getArgument ( i ) = result )
9196}
9297
9398predicate exposesByReturn ( Callable c , Field f , Expr why , string whyText ) {
9499 returnsArray ( c , f ) and
95- exists ( MethodAccess ma | ma .getMethod ( ) = c and ma .getCompilationUnit ( ) != c .getCompilationUnit ( ) |
100+ exists ( MethodAccess ma |
101+ ma .getMethod ( ) = c and ma .getCompilationUnit ( ) != c .getCompilationUnit ( )
102+ |
96103 mayWriteToArray ( ma ) and
97104 why = ma and
98105 whyText = "after this call to " + c .getName ( )
@@ -113,6 +120,6 @@ from Callable c, Field f, Expr why, string whyText
113120where
114121 exposesByReturn ( c , f , why , whyText ) or
115122 exposesByStore ( c , f , why , whyText )
116- select c , c . getName ( ) + " exposes the internal representation stored in field " + f . getName ( ) +
117- ". The value may be modified $@." ,
118- why .getLocation ( ) , whyText
123+ select c ,
124+ c . getName ( ) + " exposes the internal representation stored in field " + f . getName ( ) +
125+ ". The value may be modified $@." , why .getLocation ( ) , whyText
0 commit comments