1 19 20 package edu.umd.cs.findbugs.detect; 21 22 import org.apache.bcel.classfile.Code; 23 24 import edu.umd.cs.findbugs.BugInstance; 25 import edu.umd.cs.findbugs.BugReporter; 26 import edu.umd.cs.findbugs.BytecodeScanningDetector; 27 import edu.umd.cs.findbugs.StatelessDetector; 28 29 public class BadUseOfReturnValue extends BytecodeScanningDetector { 30 31 BugReporter bugReporter; 32 33 public BadUseOfReturnValue(BugReporter bugReporter) { 34 this.bugReporter = bugReporter; 35 } 36 37 38 39 boolean readLineOnTOS = false; 40 boolean stringIndexOfOnTOS= false; 41 @Override 42 public void visit(Code obj) { 43 stringIndexOfOnTOS= false; 44 readLineOnTOS = false; 45 super.visit(obj); 46 } 47 48 49 @Override 50 public void sawOpcode(int seen) { 51 if (seen == INVOKEVIRTUAL && 52 getNameConstantOperand().equals("indexOf") 53 && getClassConstantOperand().equals("java/lang/String") 54 && getSigConstantOperand().equals("(Ljava/lang/String;)I")) 55 stringIndexOfOnTOS= true; 56 else if (stringIndexOfOnTOS) { 57 if (seen == IFLE || seen == IFGT) 58 bugReporter.reportBug(new BugInstance(this, "RV_CHECK_FOR_POSITIVE_INDEXOF", LOW_PRIORITY) 59 .addClassAndMethod(this) 60 .addSourceLine(this)); 61 stringIndexOfOnTOS = false; 62 } 63 64 if (seen == INVOKEVIRTUAL && 65 getNameConstantOperand().equals("readLine") 66 && getSigConstantOperand().equals("()Ljava/lang/String;")) 67 readLineOnTOS = true; 68 else if (readLineOnTOS) { 69 if (seen == IFNULL || seen == IFNONNULL) 70 bugReporter.reportBug(new BugInstance(this, "RV_DONT_JUST_NULL_CHECK_READLINE", NORMAL_PRIORITY) 71 .addClassAndMethod(this) 72 .addSourceLine(this)); 73 74 readLineOnTOS = false; 75 } 76 } 77 78 } 79 | Popular Tags |