01: /*
02: * FindBugs - Find bugs in Java programs
03: * Copyright (C) 2005, University of Maryland
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: */
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:
28: public class BadUseOfReturnValue extends BytecodeScanningDetector {
29:
30: BugReporter bugReporter;
31:
32: public BadUseOfReturnValue(BugReporter bugReporter) {
33: this .bugReporter = bugReporter;
34: }
35:
36: boolean readLineOnTOS = false;
37: boolean stringIndexOfOnTOS = false;
38:
39: @Override
40: public void visit(Code obj) {
41: stringIndexOfOnTOS = false;
42: readLineOnTOS = false;
43: super .visit(obj);
44: }
45:
46: @Override
47: public void sawOpcode(int seen) {
48: if (seen == INVOKEVIRTUAL
49: && getNameConstantOperand().equals("indexOf")
50: && getClassConstantOperand().equals("java/lang/String")
51: && getSigConstantOperand().equals(
52: "(Ljava/lang/String;)I"))
53: stringIndexOfOnTOS = true;
54: else if (stringIndexOfOnTOS) {
55: if (seen == IFLE || seen == IFGT)
56: bugReporter.reportBug(new BugInstance(this ,
57: "RV_CHECK_FOR_POSITIVE_INDEXOF", LOW_PRIORITY)
58: .addClassAndMethod(this ).addSourceLine(this ));
59: stringIndexOfOnTOS = false;
60: }
61:
62: if (seen == INVOKEVIRTUAL
63: && getNameConstantOperand().equals("readLine")
64: && getSigConstantOperand().equals(
65: "()Ljava/lang/String;")
66: && getClassConstantOperand().startsWith("java/io")
67: && !getClassConstantOperand().equals(
68: "java/io/LineNumberReader"))
69: readLineOnTOS = true;
70: else if (readLineOnTOS) {
71: if (seen == IFNULL || seen == IFNONNULL)
72: bugReporter.reportBug(new BugInstance(this ,
73: "RV_DONT_JUST_NULL_CHECK_READLINE",
74: NORMAL_PRIORITY).addClassAndMethod(this )
75: .addSourceLine(this ));
76:
77: readLineOnTOS = false;
78: }
79: }
80:
81: }
|