From b9b0059797875a25c8caadffba0fcd2f6bf1f1c2 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 30 Apr 2016 11:46:29 +0200 Subject: [PATCH] Fixes #1476 False positive of ArrayIsStoredDirectly --- .../rule/sunsecure/AbstractSunSecureRule.java | 13 ++++++++-- .../sunsecure/ArrayIsStoredDirectlyRule.java | 4 +-- .../sunsecure/xml/ArrayIsStoredDirectly.xml | 25 +++++++++++++++++++ src/site/markdown/overview/changelog.md | 2 ++ 4 files changed, 40 insertions(+), 4 deletions(-) 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); } } +} + ]]> + + + + #1476 False positive of ArrayIsStoredDirectly + 0 + diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 7a7b75d1c6..f41a7cf6f9 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -44,6 +44,8 @@ * [#1453](https://sourceforge.net/p/pmd/bugs/1453/): Test Class Without Test Cases gives false positive * java-optimizations/UseStringBufferForStringAppends: * [#1340](https://sourceforge.net/p/pmd/bugs/1340/): UseStringBufferForStringAppends False Positive with ternary operator +* java-sunsecure/ArrayIsStoredDirectly: + * [#1476](https://sourceforge.net/p/pmd/bugs/1476/): False positive of ArrayIsStoredDirectly * java-unnecessary/UnnecessaryFinalModifier: * [#1464](https://sourceforge.net/p/pmd/bugs/1464/): UnnecessaryFinalModifier false positive on a @SafeVarargs method * java-unusedcode/UnusedFormalParameter: