1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| package net.sourceforge.pmd.rules.sunsecure; |
7 |
| |
8 |
| import net.sourceforge.pmd.ast.ASTAssignmentOperator; |
9 |
| import net.sourceforge.pmd.ast.ASTBlockStatement; |
10 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; |
11 |
| import net.sourceforge.pmd.ast.ASTConstructorDeclaration; |
12 |
| import net.sourceforge.pmd.ast.ASTEqualityExpression; |
13 |
| import net.sourceforge.pmd.ast.ASTExpression; |
14 |
| import net.sourceforge.pmd.ast.ASTFormalParameter; |
15 |
| import net.sourceforge.pmd.ast.ASTFormalParameters; |
16 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
17 |
| import net.sourceforge.pmd.ast.ASTPrimaryExpression; |
18 |
| import net.sourceforge.pmd.ast.ASTPrimarySuffix; |
19 |
| import net.sourceforge.pmd.ast.ASTStatementExpression; |
20 |
| import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; |
21 |
| |
22 |
| import java.util.Iterator; |
23 |
| import java.util.List; |
24 |
| import java.util.Vector; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public class ArrayIsStoredDirectly extends AbstractSunSecureRule { |
31 |
| |
32 |
7
| public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
33 |
7
| if (node.isInterface()) {
|
34 |
1
| return data;
|
35 |
| } |
36 |
6
| return super.visit(node, data);
|
37 |
| } |
38 |
| |
39 |
0
| public Object visit(ASTConstructorDeclaration node, Object data) {
|
40 |
0
| ASTFormalParameter[] arrs = getArrays((ASTFormalParameters) node.jjtGetChild(0));
|
41 |
0
| if (arrs!=null) {
|
42 |
| |
43 |
0
| List bs = node.findChildrenOfType(ASTBlockStatement.class);
|
44 |
0
| checkAll(data, arrs, bs);
|
45 |
| } |
46 |
0
| return data;
|
47 |
| } |
48 |
| |
49 |
7
| public Object visit(ASTMethodDeclaration node, Object data) {
|
50 |
7
| final ASTFormalParameters params = (ASTFormalParameters) node.getFirstChildOfType(ASTFormalParameters.class);
|
51 |
7
| ASTFormalParameter[] arrs = getArrays(params);
|
52 |
7
| if (arrs!=null) {
|
53 |
7
| List bs = node.findChildrenOfType(ASTBlockStatement.class);
|
54 |
7
| checkAll(data, arrs, bs);
|
55 |
| } |
56 |
7
| return data;
|
57 |
| } |
58 |
| |
59 |
7
| private void checkAll(Object context, ASTFormalParameter[] arrs, List bs) {
|
60 |
7
| for (int i=0;i<arrs.length;i++) {
|
61 |
7
| checkForDirectAssignment(context, arrs[i], bs);
|
62 |
| } |
63 |
| } |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
7
| private boolean checkForDirectAssignment(Object ctx, final ASTFormalParameter parameter, final List bs) {
|
69 |
7
| final ASTVariableDeclaratorId vid = (ASTVariableDeclaratorId) parameter.getFirstChildOfType(ASTVariableDeclaratorId.class);
|
70 |
7
| final String varName = vid.getImage();
|
71 |
7
| for (Iterator it = bs.iterator() ; it.hasNext() ; ) {
|
72 |
6
| final ASTBlockStatement b = (ASTBlockStatement) it.next();
|
73 |
6
| if (b.containsChildOfType(ASTAssignmentOperator.class)) {
|
74 |
5
| final ASTStatementExpression se = (ASTStatementExpression) b.getFirstChildOfType(ASTStatementExpression.class);
|
75 |
5
| if (se == null || !(se.jjtGetChild(0) instanceof ASTPrimaryExpression)) {
|
76 |
0
| continue;
|
77 |
| } |
78 |
5
| ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
|
79 |
5
| String assignedVar = getFirstNameImage(pe);
|
80 |
5
| if (assignedVar==null) {
|
81 |
2
| assignedVar = ((ASTPrimarySuffix)se.getFirstChildOfType(ASTPrimarySuffix.class)).getImage();
|
82 |
| } |
83 |
| |
84 |
5
| ASTMethodDeclaration n = (ASTMethodDeclaration) pe.getFirstParentOfType(ASTMethodDeclaration.class);
|
85 |
5
| if (n == null) {
|
86 |
0
| continue;
|
87 |
| } |
88 |
5
| if (!isLocalVariable(assignedVar, n)) {
|
89 |
4
| if (se.jjtGetNumChildren() < 3) {
|
90 |
0
| continue;
|
91 |
| } |
92 |
4
| ASTExpression e = (ASTExpression) se.jjtGetChild(2);
|
93 |
4
| if (e.findChildrenOfType(ASTEqualityExpression.class).size() > 0) {
|
94 |
1
| continue;
|
95 |
| } |
96 |
3
| String val = getFirstNameImage(e);
|
97 |
3
| if (val==null) {
|
98 |
0
| ASTPrimarySuffix foo = (ASTPrimarySuffix)se.getFirstChildOfType(ASTPrimarySuffix.class);
|
99 |
0
| if (foo == null) {
|
100 |
0
| continue;
|
101 |
| } |
102 |
0
| val = foo.getImage();
|
103 |
| } |
104 |
3
| if (val == null) {
|
105 |
0
| continue;
|
106 |
| } |
107 |
| |
108 |
3
| if (val.equals(varName)) {
|
109 |
3
| ASTMethodDeclaration md = (ASTMethodDeclaration) parameter.getFirstParentOfType(ASTMethodDeclaration.class);
|
110 |
3
| if (!isLocalVariable(varName, md)) {
|
111 |
3
| addViolation(ctx, parameter, varName);
|
112 |
| } |
113 |
| } |
114 |
| } |
115 |
| } |
116 |
| } |
117 |
7
| return false;
|
118 |
| } |
119 |
| |
120 |
7
| private final ASTFormalParameter[] getArrays(ASTFormalParameters params) {
|
121 |
7
| final List l = params.findChildrenOfType(ASTFormalParameter.class);
|
122 |
7
| if (l!=null && !l.isEmpty()) {
|
123 |
7
| Vector v = new Vector();
|
124 |
7
| for (Iterator it = l.iterator() ; it.hasNext() ; ) {
|
125 |
7
| ASTFormalParameter fp = (ASTFormalParameter) it.next();
|
126 |
7
| if (fp.isArray())
|
127 |
7
| v.add(fp);
|
128 |
| } |
129 |
7
| return (ASTFormalParameter[]) v.toArray(new ASTFormalParameter[v.size()]);
|
130 |
| } |
131 |
0
| return null;
|
132 |
| } |
133 |
| |
134 |
| } |