Merge branch 'bug-1475' into pmd/5.3.x
This commit is contained in:
@ -5,8 +5,14 @@ package net.sourceforge.pmd.lang.java.rule.sunsecure;
|
|||||||
|
|
||||||
import java.util.List;
|
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.ASTAllocationExpression;
|
||||||
|
import net.sourceforge.pmd.lang.java.ast.ASTArrayInitializer;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
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.ASTMethodDeclaration;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTName;
|
import net.sourceforge.pmd.lang.java.ast.ASTName;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
|
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.ASTPrimarySuffix;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
|
import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
|
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,
|
* 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)) {
|
if (hasClone(ret, vn)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (isEmptyArray(vn, td)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (!isLocalVariable(vn, method)) {
|
if (!isLocalVariable(vn, method)) {
|
||||||
addViolation(data, ret, vn);
|
addViolation(data, ret, vn);
|
||||||
} else {
|
} else {
|
||||||
@ -100,4 +111,32 @@ public class MethodReturnsInternalArrayRule extends AbstractSunSecureRule {
|
|||||||
}
|
}
|
||||||
return false;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,6 +184,36 @@ public class A {
|
|||||||
private Object[] getContent() {
|
private Object[] getContent() {
|
||||||
return content;
|
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>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
@ -47,6 +47,7 @@
|
|||||||
* java-optimizations/UseStringBufferForStringAppends:
|
* java-optimizations/UseStringBufferForStringAppends:
|
||||||
* [#1340](https://sourceforge.net/p/pmd/bugs/1340/): UseStringBufferForStringAppends False Positive with ternary operator
|
* [#1340](https://sourceforge.net/p/pmd/bugs/1340/): UseStringBufferForStringAppends False Positive with ternary operator
|
||||||
* java-sunsecure/ArrayIsStoredDirectly:
|
* 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
|
* [#1476](https://sourceforge.net/p/pmd/bugs/1476/): False positive of ArrayIsStoredDirectly
|
||||||
* java-unnecessary/UnnecessaryFinalModifier:
|
* java-unnecessary/UnnecessaryFinalModifier:
|
||||||
* [#1464](https://sourceforge.net/p/pmd/bugs/1464/): UnnecessaryFinalModifier false positive on a @SafeVarargs method
|
* [#1464](https://sourceforge.net/p/pmd/bugs/1464/): UnnecessaryFinalModifier false positive on a @SafeVarargs method
|
||||||
|
Reference in New Issue
Block a user