[Fb-contrib-commit] SF.net SVN: fb-contrib:[1451] trunk/fb-contrib/src/com/mebigfatguy/ fbcontrib/d
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2010-01-09 04:24:28
|
Revision: 1451
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1451&view=rev
Author: dbrosius
Date: 2010-01-09 04:24:17 +0000 (Sat, 09 Jan 2010)
Log Message:
-----------
only report WNG if another variable with the same signature as the nonnull guard variable is loaded in the if block guarded by a non null statement
Modified Paths:
--------------
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/WrongNullGuard.java
Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/WrongNullGuard.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/WrongNullGuard.java 2010-01-05 09:04:32 UTC (rev 1450)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/WrongNullGuard.java 2010-01-09 04:24:17 UTC (rev 1451)
@@ -23,6 +23,8 @@
import java.util.Map;
import org.apache.bcel.classfile.Code;
+import org.apache.bcel.classfile.LocalVariable;
+import org.apache.bcel.classfile.LocalVariableTable;
import com.mebigfatguy.fbcontrib.utils.RegisterUtils;
@@ -44,6 +46,7 @@
private BugReporter bugReporter;
private OpcodeStack stack;
+ private LocalVariableTable lvt;
private Map<Integer, NullGuard> nullGuards;
/**
@@ -77,9 +80,14 @@
*/
@Override
public void visitCode(Code obj) {
- stack.resetForMethodEntry(this);
- nullGuards.clear();
- super.visitCode(obj);
+ try {
+ stack.resetForMethodEntry(this);
+ lvt = getMethod().getLocalVariableTable();
+ nullGuards.clear();
+ super.visitCode(obj);
+ } finally {
+ lvt = null;
+ }
}
/**
@@ -92,7 +100,7 @@
try {
Integer pc = Integer.valueOf(getPC());
NullGuard guard = nullGuards.remove(pc);
- if (guard != null) {
+ if ((guard != null) && guard.sawSignatureOfGuard()) {
boolean localBug = guard.getRegister() >= 0;
bugReporter.reportBug(new BugInstance(this, localBug ? "WNG_WRONG_NULL_LOCAL_GUARD" : "WNG_WRONG_NULL_FIELD_GUARD", localBug ? NORMAL_PRIORITY : LOW_PRIORITY)
.addClass(this)
@@ -107,17 +115,31 @@
int reg = item.getRegisterNumber();
Integer target = Integer.valueOf(getBranchTarget());
if (reg >= 0) {
- nullGuards.put(target, new NullGuard(reg, pc.intValue()));
+ nullGuards.put(target, new NullGuard(reg, pc.intValue(), item.getSignature()));
} else {
XField xf = item.getXField();
if (xf != null) {
- nullGuards.put(target, new NullGuard(xf, pc.intValue()));
+ nullGuards.put(target, new NullGuard(xf, pc.intValue(), item.getSignature()));
}
}
}
}
break;
+ case ALOAD:
+ case ALOAD_0:
+ case ALOAD_1:
+ case ALOAD_2:
+ case ALOAD_3: {
+ if (lvt != null) {
+ LocalVariable lv = lvt.getLocalVariable(RegisterUtils.getALoadReg(this, seen), getNextPC());
+ if (lv != null) {
+ markNullGuards(lv.getSignature());
+ }
+ }
+ }
+ break;
+
case ASTORE:
case ASTORE_0:
case ASTORE_1:
@@ -127,6 +149,11 @@
}
break;
+ case GETFIELD: {
+ markNullGuards(getSigConstantOperand());
+ }
+ break;
+
case PUTFIELD: {
removeGuardForField(getXField());
}
@@ -150,27 +177,6 @@
}
break;
- case IRETURN:
- if (stack.getStackDepth() > 0) {
- OpcodeStack.Item item = stack.getStackItem(0);
- Integer value = (Integer)item.getConstant();
- if (value != null) {
- int val = value.intValue();
- if ((val == 0) || (val == 1))
- nullGuards.clear();
- }
- }
- //FALL_THRU
-
- case FRETURN:
- case DRETURN:
- case ARETURN:
- case LRETURN:
- case RETURN: {
- nullGuards.remove(Integer.valueOf(getPC() + 1));
- }
- break;
-
case GOTO: {
if (stack.getStackDepth() > 0) {
nullGuards.clear();
@@ -194,6 +200,14 @@
}
}
+ private void markNullGuards(String signature) {
+ for (NullGuard ng : nullGuards.values()) {
+ if (ng.getSignature().equals(signature)) {
+ ng.markSignatureOfGuard();
+ }
+ }
+ }
+
private void removeGuardForRegister(int reg) {
Iterator<NullGuard> it = nullGuards.values().iterator();
while (it.hasNext()) {
@@ -220,18 +234,22 @@
int register;
XField field;
int location;
+ String signature;
+ boolean sawSignature = false;
- public NullGuard(int reg, int start) {
+ public NullGuard(int reg, int start, String guardSignature) {
register = reg;
field = null;
location = start;
+ signature = guardSignature;
}
- public NullGuard(XField xf, int start) {
+ public NullGuard(XField xf, int start, String guardSignature) {
register = -1;
field = xf;
location = start;
+ signature = guardSignature;
}
public int getRegister() {
@@ -245,5 +263,17 @@
public int getLocation() {
return location;
}
+
+ public String getSignature() {
+ return signature;
+ }
+
+ public void markSignatureOfGuard() {
+ sawSignature = true;
+ }
+
+ public boolean sawSignatureOfGuard() {
+ return sawSignature;
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|