Skip to content

Commit

Permalink
fix: ignore RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE right after try-…
Browse files Browse the repository at this point in the history
…block

close #259
  • Loading branch information
KengoTODA committed Aug 20, 2020
1 parent 6b7f449 commit 7700cfc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.annotation.Nonnull;
import javax.annotation.meta.When;

import edu.umd.cs.findbugs.annotations.NonNull;
import org.apache.bcel.Const;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.CodeException;
Expand All @@ -49,6 +50,7 @@
import org.apache.bcel.generic.INVOKEDYNAMIC;
import org.apache.bcel.generic.Instruction;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.InstructionTargeter;
import org.apache.bcel.generic.InvokeInstruction;
import org.apache.bcel.generic.MethodGen;
Expand Down Expand Up @@ -126,6 +128,8 @@
import edu.umd.cs.findbugs.props.WarningPropertyUtil;
import edu.umd.cs.findbugs.util.Values;
import edu.umd.cs.findbugs.visitclass.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A Detector to find instructions where a NullPointerException might be raised.
Expand All @@ -137,6 +141,7 @@
* @see edu.umd.cs.findbugs.ba.npe.IsNullValueAnalysis
*/
public class FindNullDeref implements Detector, UseAnnotationDatabase, NullDerefAndRedundantComparisonCollector {
private final Logger log = LoggerFactory.getLogger(getClass());

public static final boolean DEBUG = SystemProperties.getBoolean("fnd.debug");

Expand Down Expand Up @@ -922,7 +927,6 @@ public boolean skipIfInsideCatchNull() {
* instead
*/
@Override
@Deprecated
public void foundNullDeref(Location location, ValueNumber valueNumber, IsNullValue refValue, ValueNumberFrame vnaFrame) {
foundNullDeref(location, valueNumber, refValue, vnaFrame, true);
}
Expand Down Expand Up @@ -1153,6 +1157,9 @@ public void foundRedundantNullCheck(Location location, RedundantBranch redundant
if (redundantBranch.firstValue.isDefinitelyNull()) {
warning = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE";
priority = NORMAL_PRIORITY;
} else if (isGeneratedCodeInCatchBlock(method.getConstantPool(), pc)) {
log.debug("skip RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE found in the generated code at {}", location);
return;
} else {
warning = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE";
valueIsNull = false;
Expand Down Expand Up @@ -1783,4 +1790,42 @@ boolean inIndirectCatchNullBlock(Location loc) {
"java/lang/Throwable", pc);
return catchSize < 5;
}

/**
* Java 11+ compiler generates redundant null checks for try-with-resources.
* This methods detects the {@code ifnull} bytecode generated by javac,
* to help detector to avoid to report needless {@code RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE} bug.
*
* @param cp the constant pool
* @param pc the program counter
* @return true if the pc specifies redundant null check generated by javac
* @see <a href="https://github.com/spotbugs/spotbugs/issues/259">false positive RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE on try-with-resources</a>
*/
private boolean isGeneratedCodeInCatchBlock(@NonNull ConstantPool cp, int pc) {
Code code = method.getCode();
if (code == null) {
return false;
}
CodeException[] table = code.getExceptionTable();

// TODO: This instantiation could be high cost computation
InstructionList list = new InstructionList(code.getCode());

return Arrays.stream(table)
.filter(codeException -> {
// assume that programmers rarely catch java.lang.Throwable instance explicitly
int catchType = codeException.getCatchType();
if (catchType == 0) {
// '0' means it catches any exceptions
return true;
}
String exceptionName = cp.getConstantString(catchType, Const.CONSTANT_Class);
return "java/lang/Throwable".equals(exceptionName);
})
.anyMatch(codeException -> {
InstructionHandle handle = list.findHandle(codeException.getEndPC());
int insnLength = handle.getInstruction().getLength();
return codeException.getEndPC() + insnLength == pc;
});
}
}
5 changes: 0 additions & 5 deletions spotbugsTestCases/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ javadoc {
}
}

tasks.compileJava {
// workaround for https://github.com/spotbugs/spotbugs/issues/996
exclude 'sfBugsNew/Bug1169.java'
}

task classesJava8(type:JavaCompile) {
destinationDir = file("$buildDir/classes/java/java8")
classpath = sourceSets.main.compileClasspath
Expand Down

0 comments on commit 7700cfc

Please sign in to comment.