Merge branch 'bug-1475' into pmd/5.4.x

This commit is contained in:
Andreas Dangel
2016-04-30 18:40:02 +02:00
3 changed files with 70 additions and 0 deletions

View File

@ -5,8 +5,14 @@ package net.sourceforge.pmd.lang.java.rule.sunsecure;
import java.util.List;
import org.jaxen.JaxenException;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression;
import net.sourceforge.pmd.lang.java.ast.ASTArrayInitializer;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
@ -14,6 +20,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer;
/**
* Implementation note: this rule currently ignores return types of y.x.z,
@ -56,6 +64,9 @@ public class MethodReturnsInternalArrayRule extends AbstractSunSecureRule {
if (hasClone(ret, vn)) {
continue;
}
if (isEmptyArray(vn, td)) {
continue;
}
if (!isLocalVariable(vn, method)) {
addViolation(data, ret, vn);
} else {
@ -100,4 +111,32 @@ public class MethodReturnsInternalArrayRule extends AbstractSunSecureRule {
}
return false;
}
private boolean isEmptyArray(String varName, ASTTypeDeclaration typeDeclaration) {
final List<ASTFieldDeclaration> fds = typeDeclaration.findDescendantsOfType(ASTFieldDeclaration.class);
if (fds != null) {
for (ASTFieldDeclaration fd : fds) {
final ASTVariableDeclaratorId vid = fd.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
if (vid != null && vid.hasImageEqualTo(varName)) {
ASTVariableInitializer initializer = fd.getFirstDescendantOfType(ASTVariableInitializer.class);
if (initializer != null && initializer.jjtGetNumChildren() == 1) {
Node child = initializer.jjtGetChild(0);
if (child instanceof ASTArrayInitializer && child.jjtGetNumChildren() == 0) {
return true;
} else if (child instanceof ASTExpression) {
try {
List<? extends Node> arrayAllocation = child.findChildNodesWithXPath("./PrimaryExpression/PrimaryPrefix/AllocationExpression/ArrayDimsAndInits/Expression/PrimaryExpression/PrimaryPrefix/Literal[@IntLiteral=\"true\"][@Image=\"0\"]");
if (arrayAllocation != null && arrayAllocation.size() == 1) {
return true;
}
} catch (JaxenException e) {
return false;
}
}
}
}
}
}
return false;
}
}

View File

@ -184,6 +184,36 @@ public class A {
private Object[] getContent() {
return content;
}
}
]]></code>
</test-code>
<test-code>
<description>#1475 False positive of MethodReturnsInternalArray</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class MethodReturnsInternalArrayCase {
private static final byte[] DATA = {};
protected final byte[] getData()
{
return DATA;
}
}
]]></code>
</test-code>
<test-code>
<description>#1475 False positive of MethodReturnsInternalArray - ArrayAllocation</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class MethodReturnsInternalArrayCase {
private static final byte[] DATA = new byte[0];
protected final byte[] getData()
{
return DATA;
}
}
]]></code>
</test-code>

View File

@ -49,6 +49,7 @@
* java-optimizations/UseStringBufferForStringAppends:
* [#1340](https://sourceforge.net/p/pmd/bugs/1340/): UseStringBufferForStringAppends False Positive with ternary operator
* java-sunsecure/ArrayIsStoredDirectly:
* [#1475](https://sourceforge.net/p/pmd/bugs/1475/): False positive of MethodReturnsInternalArray
* [#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