Thread: [Fb-contrib-commit] SF.net SVN: fb-contrib: [676] trunk/fb-contrib/src/com/mebigfatguy/ fbcontrib/
Brought to you by:
dbrosius
From: <dbr...@us...> - 2006-10-28 18:17:13
|
Revision: 676 http://svn.sourceforge.net/fb-contrib/?rev=676&view=rev Author: dbrosius Date: 2006-10-28 11:15:56 -0700 (Sat, 28 Oct 2006) Log Message: ----------- initial checkin SC detector Added Paths: ----------- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java Added: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java (rev 0) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2006-10-28 18:15:56 UTC (rev 676) @@ -0,0 +1,120 @@ +package com.mebigfatguy.fbcontrib.detect; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.bcel.Repository; +import org.apache.bcel.classfile.Code; +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.generic.Type; + +import edu.umd.cs.findbugs.BugInstance; +import edu.umd.cs.findbugs.BugReporter; +import edu.umd.cs.findbugs.BytecodeScanningDetector; +import edu.umd.cs.findbugs.OpcodeStack; +import edu.umd.cs.findbugs.ba.ClassContext; + +/** + * looks for class that implement Comparator or Comparable, and whose compare or compareTo + * methods return constant values only, but that don't represent the three possible choice + * (a negative number, 0, and a positive number). + */ +public class SuspiciousComparatorReturnValues extends BytecodeScanningDetector +{ + private static Map<JavaClass, String> compareClasses = new HashMap<JavaClass, String>(); + static { + try { + compareClasses.put(Repository.lookupClass("java/lang/Comparable"), "compare:2:I");; + compareClasses.put(Repository.lookupClass("java.lang/Comparator"), "compareTo:1:I"); + } catch (ClassNotFoundException cnfe) { + } + } + + private OpcodeStack stack; + private BugReporter bugReporter; + private String[] methodInfo; + private boolean indeterminate; + private boolean seenNegative; + private boolean seenPositive; + private boolean seenZero; + + + /** + * constructs a DRE detector given the reporter to report bugs on + * @param bugReporter the sync of bug reports + */ + public SuspiciousComparatorReturnValues(BugReporter bugReporter) { + this.bugReporter = bugReporter; + } + + public void visitClassContext(ClassContext classContext) { + try { + JavaClass cls = classContext.getJavaClass(); + for (Map.Entry<JavaClass, String> entry : compareClasses.entrySet()) { + if (cls.implementationOf(entry.getKey())) { + methodInfo = entry.getValue().split(":"); + stack = new OpcodeStack(); + super.visitClassContext(classContext); + break; + } + } + } catch (ClassNotFoundException cnfe) { + bugReporter.reportMissingClass(cnfe); + } finally { + methodInfo = null; + stack = null; + } + } + + public void visitCode(Code obj) { + String methodName = getMethodName(); + String methodSig = getMethodSig(); + if (methodName.equals(methodInfo[0]) + && methodSig.endsWith(methodInfo[2]) + && (Type.getArgumentTypes(methodSig).length == Integer.valueOf(methodInfo[1]))) { + stack.resetForMethodEntry(this); + indeterminate = false; + seenNegative = false; + seenPositive = false; + seenZero = false; + super.visitCode(obj); + if (!indeterminate) { + boolean seenAll = seenNegative & seenPositive & seenZero; + if (!seenAll) { + bugReporter.reportBug(new BugInstance(this, "SC_SUSPICIOUS_COMPARATOR", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLine(this, 0)); + } + } + } + } + + public void sawOpcode(int seen) { + try { + if (indeterminate) + return; + + if (seen == IRETURN) { + if (stack.getStackDepth() > 0) { + OpcodeStack.Item item = stack.getStackItem(0); + Integer returnValue = (Integer)item.getConstant(); + if (returnValue == null) + indeterminate = true; + else { + int v = returnValue.intValue(); + if (v < 0) + seenNegative = true; + else if (v > 0) + seenPositive = true; + else + seenZero = true; + } + } else + indeterminate = true; + } + } finally { + stack.sawOpcode(this, seen); + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dbr...@us...> - 2006-10-28 18:22:56
|
Revision: 677 http://svn.sourceforge.net/fb-contrib/?rev=677&view=rev Author: dbrosius Date: 2006-10-28 11:21:35 -0700 (Sat, 28 Oct 2006) Log Message: ----------- change to SCRV Modified Paths: -------------- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2006-10-28 18:15:56 UTC (rev 676) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2006-10-28 18:21:35 UTC (rev 677) @@ -81,7 +81,7 @@ if (!indeterminate) { boolean seenAll = seenNegative & seenPositive & seenZero; if (!seenAll) { - bugReporter.reportBug(new BugInstance(this, "SC_SUSPICIOUS_COMPARATOR", NORMAL_PRIORITY) + bugReporter.reportBug(new BugInstance(this, "SC_SUSPICIOUS_COMPARATOR_RETURN_VALUES", NORMAL_PRIORITY) .addClass(this) .addMethod(this) .addSourceLine(this, 0)); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dbr...@us...> - 2006-10-29 00:41:05
|
Revision: 681 http://svn.sourceforge.net/fb-contrib/?rev=681&view=rev Author: dbrosius Date: 2006-10-28 17:40:58 -0700 (Sat, 28 Oct 2006) Log Message: ----------- fix comparator class specification Modified Paths: -------------- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2006-10-29 00:08:39 UTC (rev 680) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2006-10-29 00:40:58 UTC (rev 681) @@ -25,7 +25,7 @@ static { try { compareClasses.put(Repository.lookupClass("java/lang/Comparable"), "compare:2:I");; - compareClasses.put(Repository.lookupClass("java.lang/Comparator"), "compareTo:1:I"); + compareClasses.put(Repository.lookupClass("java/util/Comparator"), "compareTo:1:I"); } catch (ClassNotFoundException cnfe) { } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dbr...@us...> - 2006-10-29 00:48:25
|
Revision: 682 http://svn.sourceforge.net/fb-contrib/?rev=682&view=rev Author: dbrosius Date: 2006-10-28 17:48:19 -0700 (Sat, 28 Oct 2006) Log Message: ----------- fix signatures Modified Paths: -------------- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2006-10-29 00:40:58 UTC (rev 681) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2006-10-29 00:48:19 UTC (rev 682) @@ -24,8 +24,8 @@ private static Map<JavaClass, String> compareClasses = new HashMap<JavaClass, String>(); static { try { - compareClasses.put(Repository.lookupClass("java/lang/Comparable"), "compare:2:I");; - compareClasses.put(Repository.lookupClass("java/util/Comparator"), "compareTo:1:I"); + compareClasses.put(Repository.lookupClass("java/lang/Comparable"), "compareTo:1:I");; + compareClasses.put(Repository.lookupClass("java/util/Comparator"), "compare:2:I"); } catch (ClassNotFoundException cnfe) { } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dbr...@us...> - 2006-12-22 21:34:20
|
Revision: 755 http://svn.sourceforge.net/fb-contrib/?rev=755&view=rev Author: dbrosius Date: 2006-12-22 13:34:17 -0800 (Fri, 22 Dec 2006) Log Message: ----------- avoid autoboxing Modified Paths: -------------- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2006-12-22 21:33:16 UTC (rev 754) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2006-12-22 21:34:17 UTC (rev 755) @@ -8,6 +8,8 @@ import org.apache.bcel.classfile.JavaClass; import org.apache.bcel.generic.Type; +import com.mebigfatguy.fbcontrib.utils.Integer14; + import edu.umd.cs.findbugs.BugInstance; import edu.umd.cs.findbugs.BugReporter; import edu.umd.cs.findbugs.BytecodeScanningDetector; @@ -73,7 +75,7 @@ String methodSig = getMethodSig(); if (methodName.equals(methodInfo[0]) && methodSig.endsWith(methodInfo[2]) - && (Type.getArgumentTypes(methodSig).length == Integer.valueOf(methodInfo[1]))) { + && (Type.getArgumentTypes(methodSig).length == Integer.valueOf(methodInfo[1]).intValue())) { stack.resetForMethodEntry(this); indeterminate = false; seenNegative = false; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dbr...@us...> - 2006-12-22 21:34:46
|
Revision: 756 http://svn.sourceforge.net/fb-contrib/?rev=756&view=rev Author: dbrosius Date: 2006-12-22 13:34:42 -0800 (Fri, 22 Dec 2006) Log Message: ----------- fix up imports Modified Paths: -------------- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2006-12-22 21:34:17 UTC (rev 755) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2006-12-22 21:34:42 UTC (rev 756) @@ -8,8 +8,6 @@ import org.apache.bcel.classfile.JavaClass; import org.apache.bcel.generic.Type; -import com.mebigfatguy.fbcontrib.utils.Integer14; - import edu.umd.cs.findbugs.BugInstance; import edu.umd.cs.findbugs.BugReporter; import edu.umd.cs.findbugs.BytecodeScanningDetector; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dbr...@us...> - 2007-01-28 21:00:36
|
Revision: 799 http://svn.sourceforge.net/fb-contrib/?rev=799&view=rev Author: dbrosius Date: 2007-01-28 13:00:11 -0800 (Sun, 28 Jan 2007) Log Message: ----------- NAB fixes Modified Paths: -------------- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2007-01-28 20:58:56 UTC (rev 798) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2007-01-28 21:00:11 UTC (rev 799) @@ -91,7 +91,7 @@ String methodSig = getMethodSig(); if (methodName.equals(methodInfo[0]) && methodSig.endsWith(methodInfo[2]) - && (Type.getArgumentTypes(methodSig).length == Integer.valueOf(methodInfo[1]).intValue())) { + && (Type.getArgumentTypes(methodSig).length == Integer.parseInt(methodInfo[1]))) { stack.resetForMethodEntry(this); indeterminate = false; seenNegative = false; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <dbr...@us...> - 2008-03-14 04:58:34
|
Revision: 998 http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=998&view=rev Author: dbrosius Date: 2008-03-13 21:58:39 -0700 (Thu, 13 Mar 2008) Log Message: ----------- fix for [ 1913611 ] FalsePositve SCRV Suspicious Comparator Return Values, assume that trinaries are indeterminate Modified Paths: -------------- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2008-03-14 04:38:15 UTC (rev 997) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SuspiciousComparatorReturnValues.java 2008-03-14 04:58:39 UTC (rev 998) @@ -133,6 +133,9 @@ } } else indeterminate = true; + } else if ((seen == GOTO) || (seen == GOTO_W)) { + if (stack.getStackDepth() > 0) + indeterminate = true; } } finally { stack.sawOpcode(this, seen); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |