【问题标题】:How to write custom detector for find sec bug plugin?如何为 find sec bug 插件编写自定义检测器?
【发布时间】:2016-10-25 07:50:24
【问题描述】:

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

【问题讨论】:

  • 欢迎来到 SO!请分享你也尝试过的东西。 stackoverflow.com/help/how-to-ask
  • 我编写了一个自定义检测器,用于查找 system.out.println 的使用情况。但它给出了很多误报。例如它在以下语句中显示错误 (UploadForm object : uploadFormObj.getAllObjInExcel())。

标签: findbugs spotbugs find-sec-bugs


【解决方案1】:

以下是用于检测 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
            }
        }
    }

}

【讨论】:

    猜你喜欢
    • 2019-09-29
    • 2015-03-29
    • 1970-01-01
    • 2012-03-15
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多