diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/AbstractSunSecureRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/AbstractSunSecureRule.java index acaa077f34..54fd9f0799 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/AbstractSunSecureRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/AbstractSunSecureRule.java @@ -117,8 +117,17 @@ public abstract class AbstractSunSecureRule extends AbstractJavaRule { } /** - * Gets the image of the first ASTName node found by + * Gets the image of the ASTName node found by * {@link Node#getFirstDescendantOfType(Class)} + * if it is the greatgrandchild of the given node. + * + * E.g. + *
+ * n = Expression || StatementExpression + * PrimaryExpression + * PrimaryPrefix + * Name + ** * @param n * the node to search @@ -126,7 +135,7 @@ public abstract class AbstractSunSecureRule extends AbstractJavaRule { */ protected String getFirstNameImage(Node n) { ASTName name = n.getFirstDescendantOfType(ASTName.class); - if (name != null) { + if (name != null && name.getNthParent(3) == n) { return name.getImage(); } return null; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/ArrayIsStoredDirectlyRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/ArrayIsStoredDirectlyRule.java index 9fec7141b3..b0bf4d4ca3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/ArrayIsStoredDirectlyRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/sunsecure/ArrayIsStoredDirectlyRule.java @@ -98,12 +98,12 @@ public class ArrayIsStoredDirectlyRule extends AbstractSunSecureRule { if (se == null || !(se.jjtGetChild(0) instanceof ASTPrimaryExpression)) { continue; } - ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0); - String assignedVar = getExpressionVarName(pe); + String assignedVar = getExpressionVarName(se); if (assignedVar == null) { continue; } + ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0); Node n = pe.getFirstParentOfType(ASTMethodDeclaration.class); if (n == null) { n = pe.getFirstParentOfType(ASTConstructorDeclaration.class); diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/sunsecure/xml/ArrayIsStoredDirectly.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/sunsecure/xml/ArrayIsStoredDirectly.xml index 6928431507..1c972dc8fe 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/sunsecure/xml/ArrayIsStoredDirectly.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/sunsecure/xml/ArrayIsStoredDirectly.xml @@ -150,6 +150,31 @@ public class TestClass { this.obj = Arrays.copyOf(obj, obj.length); } } +} + ]]> + + +