use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.
the class AbnormalFinallyBlockReturn method catchBlockInFinally.
/**
* looks to see if any try/catch block exists inside this finally block, that wrap the current pc. This is a lax check as the try catch block may not catch
* exceptions that are thrown, but doing so would be prohibitively slow. But it should catch some problems.
*
* @param fBlockInfo
* the finally block the pc is currently in
*
* @return if all exceptions are caught inside this finally block
*/
private boolean catchBlockInFinally(FinallyBlockInfo fBlockInfo) {
CodeException[] catchExceptions = getCode().getExceptionTable();
if (CollectionUtils.isEmpty(catchExceptions)) {
return false;
}
int pc = getPC();
for (CodeException ex : catchExceptions) {
if ((ex.getStartPC() <= pc) && (ex.getEndPC() >= pc) && (ex.getStartPC() >= fBlockInfo.startPC)) {
return true;
}
}
return false;
}
use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.
the class AbnormalFinallyBlockReturn method visitCode.
/**
* overrides the visitor to collect finally block info.
*
* @param obj
* the code object to scan for finally blocks
*/
@Override
public void visitCode(Code obj) {
fbInfo.clear();
loadedReg = -1;
CodeException[] exc = obj.getExceptionTable();
if (exc != null) {
for (CodeException ce : exc) {
if ((ce.getCatchType() == 0) && (ce.getStartPC() == ce.getHandlerPC())) {
fbInfo.add(new FinallyBlockInfo(ce.getStartPC()));
}
}
}
if (!fbInfo.isEmpty()) {
try {
super.visitCode(obj);
} catch (StopOpcodeParsingException e) {
// no more finally blocks to check
}
}
}
use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.
the class StackedTryBlocks method visitCode.
/**
* overrides the visitor to look for 'idea' try catch blocks to find issues specifically, method needs two or more try catch blocks that only catch one
* exception type.
*
* @param obj
* the currently parsed code object
*/
@Override
public void visitCode(Code obj) {
try {
XMethod xMethod = getXMethod();
if (xMethod != null) {
String[] tes = xMethod.getThrownExceptions();
Set<String> thrownExceptions = new HashSet<>(Arrays.<String>asList((tes == null) ? new String[0] : tes));
blocks = new ArrayList<>();
inBlocks = new ArrayList<>();
transitionPoints = new BitSet();
CodeException[] ces = obj.getExceptionTable();
for (CodeException ce : ces) {
TryBlock tb = new TryBlock(ce);
int existingBlock = blocks.indexOf(tb);
if (existingBlock >= 0) {
tb = blocks.get(existingBlock);
tb.addCatchType(ce);
} else {
blocks.add(tb);
}
}
Iterator<TryBlock> it = blocks.iterator();
while (it.hasNext()) {
TryBlock block = it.next();
if (block.hasMultipleHandlers() || block.isFinally() || block.catchIsThrown(getConstantPool(), thrownExceptions)) {
it.remove();
}
}
if (blocks.size() > 1) {
stack.resetForMethodEntry(this);
super.visitCode(obj);
if (blocks.size() > 1) {
TryBlock firstBlock = blocks.get(0);
for (int i = 1; i < blocks.size(); i++) {
TryBlock secondBlock = blocks.get(i);
if (!blocksSplitAcrossTransitions(firstBlock, secondBlock) && (firstBlock.getCatchType() == secondBlock.getCatchType()) && firstBlock.getThrowSignature().equals(secondBlock.getThrowSignature()) && firstBlock.getMessage().equals(secondBlock.getMessage()) && firstBlock.getExceptionSignature().equals(secondBlock.getExceptionSignature())) {
bugReporter.reportBug(new BugInstance(this, BugType.STB_STACKED_TRY_BLOCKS.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLineRange(this, firstBlock.getStartPC(), firstBlock.getEndHandlerPC()).addSourceLineRange(this, secondBlock.getStartPC(), secondBlock.getEndHandlerPC()));
}
firstBlock = secondBlock;
}
}
}
}
} finally {
blocks = null;
inBlocks = null;
transitionPoints = null;
}
}
use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.
the class OverlyConcreteParameter method isExceptionHandled.
/**
* returns whether this exception is handled either in a try/catch or throws clause at this pc
*
* @param ex
* the name of the exception
*
* @return whether the exception is handled
*/
private boolean isExceptionHandled(String ex) {
try {
JavaClass thrownEx = Repository.lookupClass(ex);
// First look at the throws clause
ExceptionTable et = getMethod().getExceptionTable();
if (et != null) {
String[] throwClauseExNames = et.getExceptionNames();
for (String throwClauseExName : throwClauseExNames) {
JavaClass throwClauseEx = Repository.lookupClass(throwClauseExName);
if (thrownEx.instanceOf(throwClauseEx)) {
return true;
}
}
}
// Next look at the try catch blocks
CodeException[] catchExs = getCode().getExceptionTable();
if (catchExs != null) {
int pc = getPC();
for (CodeException catchEx : catchExs) {
if ((pc >= catchEx.getStartPC()) && (pc <= catchEx.getEndPC())) {
int type = catchEx.getCatchType();
if (type != 0) {
String catchExName = getConstantPool().getConstantString(type, Const.CONSTANT_Class);
JavaClass catchException = Repository.lookupClass(catchExName);
if (thrownEx.instanceOf(catchException)) {
return true;
}
}
}
}
}
} catch (ClassNotFoundException cnfe) {
bugReporter.reportMissingClass(cnfe);
}
return false;
}
use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.
the class UnnecessaryNewNullCheck method visitCode.
@Override
public void visitCode(Code obj) {
if (prescreen()) {
stack.resetForMethodEntry(this);
allocationRegs.clear();
transitionPoints.clear();
CodeException[] ce = obj.getExceptionTable();
if (ce != null) {
for (CodeException element : ce) {
transitionPoints.set(element.getEndPC());
}
}
super.visitCode(obj);
}
}
Aggregations