如何为find sec bug插件编写自定义检测器?

时间:2016-06-23 06:28:14

标签: findbugs spotbugs find-sec-bugs

如何为find sec bug插件编写自定义检测器?如果有人编写样本检测器来检测单词的使用,那将会很有帮助。  提前致谢

1 个答案:

答案 0 :(得分:2)

下面是检测system.out.printl的示例代码,但它显示了许多误报错误

  package com.h3xstream.findsecbugs;

import org.apache.bcel.Constants;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.Priorities;
import edu.umd.cs.findbugs.bcel.OpcodeStackDetector;
import edu.umd.cs.findbugs.classfile.ClassDescriptor;
import edu.umd.cs.findbugs.classfile.FieldDescriptor;

public class CallToSystemOutPrintlnDetector2 extends OpcodeStackDetector {

    private BugReporter bugReporter;

    public CallToSystemOutPrintlnDetector2(BugReporter bugReporter) {
        super();
        this.bugReporter = bugReporter;

    }

    public void sawOpcode(int seen) {
        if (seen == Constants.GETSTATIC) {
            try {
                FieldDescriptor operand = getFieldDescriptorOperand();
                ClassDescriptor classDescriptor = operand.getClassDescriptor();
                if ("java/lang/System".equals(classDescriptor.getClassName())
                        && ("err".equals(operand.getName()) || "out"
                                .equals(operand.getName()))) {

                    bugReporter
                            .reportBug(new BugInstance(this,
                                    "MY_CALL_TO_SYSTEM_OUT_BUG",
                                    Priorities.NORMAL_PRIORITY)
                                    //
                                    .addClass(this).addMethod(this)
                                    .addSourceLine(this));
                }
            } catch (Exception e) {
                // ignore
            }
        }
    }

}
相关问题