FindBugs 常见错误类型整理

3,074 阅读10分钟

FindBugs 常见错误类型整理

1.Dodgy code 糟糕的代码

1.1 Misuse of static fields

滥用静态字段

  • Write to static field from instance method
1由于在方法内对静态属性进行赋值操作
2class A{
3    private static int count;
4
5    public void init(int num){
6        count = num;
7    }
8}
9

1.2 Null pointer dereference

引用null对象

  • Load of known null value
1引用了一个为 NULL 的对象
2
3class A{
4    private static int count;
5
6    public void init(int num){
7        Integer num = null;
8        num.intValue();
9    }
10}

1.3 Dead local store

本地变量存储了闲置不用的对象

  • Dead store to local variable
1对变量进行赋值,但是后面的代码没有用到这个变量
2class A{
3    //第一种情况
4    public void init(int a){
5        int num = 3;
6        num = a;
7    }
8    //第二种情况
9    public void init(int a){
10        int num = 3;
11        //...
12    }
13}
14

1.4 Useless code

无用的代码

  • Useless object created
1创建了无用的对象
2
3class A{
4    public void init(int a){
5        List<String> list = new ArrayList<>();
6    }
7
8}
  • Useless non-empty void method
1创建了无用的方法
2
3class A{
4    public void init(){
5        List<String> list = new ArrayList<>();
6        list.add("123");
7        list.add("234");
8    }
9}

1.5 RuntimeException capture

(滥用)异常捕获

  • Exception is caught when Exception is not thrown
1在不会抛出异常的代码里尝试捕获异常
2
3class A{
4    public void init(){
5        try{
6            List<String> list = new ArrayList<>();
7        }catch (Exception e) {
8
9        }
10    }
11}

1.6 Bad use of return value from method

方法的返回值使用不当

  • Return value of method without side effect is ignored
1忽略没有副作用的方法的返回值
2
3class A{
4    public void init(){
5        List<String> list = new ArrayList<>();
6        list.size();
7    }
8}

1.7 Redundant comparison to null

对 null 值的冗余校验

  • Redundant nullcheck of value known to be non-null
1对已知的非空对象进行null值校验
2
3class A{
4    public void init(){
5
6        if(getList() != null){
7
8        }else{
9
10        }
11    }
12
13    private List<String> getList(){
14        List<String> list = new ArrayList<>();
15        return list
16    }
17}
  • Redundant nullcheck of value known to be null
1对已知的空对象进行null值校验
2
3class A{
4    public void init(){
5
6        if(getList() == null){
7
8        }else{
9
10        }
11    }
12
13    private List<String> getList(){
14        return null
15    }
16}
17

1.7 Suspicious integer expression

可疑的整数表达式

  • Vacuous bit mask operation on integer value
1整数值上的空位掩码操作
2
3class A{
4    public void init(int num){
5        int a = num & 0xFFFFFFFF;
6    }
7}

1.8 Duplicate Branches

重复分支

  • Method uses the same code for two branches
1两个分支内的代码一样
2
3class A{
4    public void init(int num){
5        if(num>0){
6            num = 0;
7        }else{
8            num = 0;
9        }
10    }
11}

1.9 Dubious method invocation

可疑的方法调用

  • Code contains a hard coded reference to an absolute pathname
1代码包含绝对路径的硬编码引用
2
3class A{
4    public void init(int num){
5        File file = new File("D:\test\test.txt");
6    }
7}

1.10 Test for floating point equality

比较两个浮点数是否相等

  • Test for floating point equality
1比较两个浮点数是否相等
2
3class A{
4    public void init(double a,double b){
5        if (a == b){
6
7        }
8    }
9}

1.11 Null pointer dereference

引用空对象

  • Load of known null value
1引用已知为null的对象
2
3class A{
4    public void init(List<String> list){
5        if (list == null){
6            System.out.print(list.7    [native code]
8}">toString());
9        }
10    }
11}

1.12 Switch case falls through

switch 语句使用不当

  • Switch statement found where default case is missing
1switch 语句缺少 default 分支
2
3class A{
4    public void init(int a){
5        switch a {
6            case 1:
7                break;
8            case 2: 
9                break;
10            }
11    }
12}

2.Correctness 正确性

2.1 Null pointer dereference

引用空指针

  • Possible null pointer dereference
1可能会抛出空指针异常
2
3class A{
4    public void init(List<String> list){
5        System.out.print(list.size());
6    }
7}
  • Possible null pointer dereference in method on exception path
1引用了某个异常控制的对象。
2
3class A{
4    public void init(){
5        List<String> list = null;
6        try{
7
8            list = new ArrayList<>();
9
10        }catch (Exception e) {
11
12        }
13
14        System.out.print(list.size());
15    }
16}
  • Method call passes null for non-null parameter
1方法调用对非null参数传递null值
2
3class A{
4    public void init(String str){
5        //compareTo方法要求参数不能为null,否则会抛出NullPointerException
6        "abc".compareTo(str)
7    }
8}
  • Null value is guaranteed to be dereferenced
1保证不能引用空值
2
3class A{
4    public void init(boolean flag){
5        List<String> list = null;
6        if(flag){
7            list.add("abc");
8        }
9    }
10}

2.2 Useless/non-informative string generated

生成了无用/非信息的字符串

  • Invocation of toString on an array
1无效的 2    [native code]
3}">toString 方法
4
5class A{
6    public void init(int[] arr){
7        //会打印出 arr 的哈希值,而不是数组的的内容
8        System.out.print(arr);
9    }
10}

2.3 Bad use of return value from method

方法的返回值使用不当

  • Method ignores return value
1该方法的返回值应该进行检查
2
3class A{
4    public void init(int[] arr){
5        String dateString = getHeaderField(name);
6        dateString.trim();
7    }
8}
9
10PS:这种警告通常出现在调用一个不可变对象的方法,认为它更新了对象的值。
11
12程序员似乎以为trim()方法将更新dateString引用的字符串。
13
14但由于字符串是不可改变的,trim()函数返回一个新字符串值,在这里它是被忽略了。
15
16该代码应更正:
17
18String dateString = getHeaderField(name);
19dateString = dateString.trim();

2.4 Format string problem

字符串格式化问题

  • More arguments are passed than are actually used in the format string
1传递的参数多于格式字符串中实际使用的参数
2
3class A{
4    public void init(String num1,String num2){
5        //错误,这种写法不会格式化字符串
6        String dateString = String.format("num1: {},num2:{}", num1,num2)
7        String dateString2 = String.format("num1: {0},num2:{1}", num1,num2)
8        //正确的写法
9        String dateString3 = String.format("num1: %s,num2:%s", num1,num2)
10    }
11}
12

2.5 Comparing incompatible types for equality

比较不兼容的类型是否相等

  • Call to equals() comparing different types
1调用equals()比较不同的类型
2
3class A{
4    public void init(String str){
5        Integer num = new Integer()
6        if(str.equals(num)){
7
8        }
9    }
10}

2.6 Questionable use of reference equality rather than calling equals

对象的比较使用了 “==” 而不是调用 equals 方法

  • Suspicious reference comparison
1可疑的比较
2
3class A{
4    public void init(String str){
5        if("abc" == str){
6        }
7    }
8}

2.7 Redundant comparison to null

与null值的冗余比较

  • Nullcheck of value previously dereferenced
1没有对参数进行空值校验
2
3class A{
4    public void init(String str){
5        System.out.println(getStr().size())
6    }
7    private List<String> getList(){
8        return null;
9    }
10}

3.Bad practice 不好的做法

3.1 Stream not closed on all paths

IO流没有关闭

  • Method may fail to close stream
1方法体内没有对IO流进行关闭

3.2 Dropped or ignored exception

忽略了异常

  • Method might ignore exception
1方法忽略了异常
2
3class A{
4    public void init(){
5        try{
6
7        }catch (Exception e) {
8            //当抛出异常时什么都没有做
9        }
10    }
11}

3.3 Bad use of return value from method

方法返回值使用不当(多为忽略了方法返回值)

  • Method ignores exceptional return value
1忽略了方法的异常返回值
2
3class A{
4    public void init(){
5        File file = new File("/aaa")
6        //文件mkdir可能返回false
7        file.getParentFile().mkdirs()
8    }
9}

3.4 Incorrect definition of Serializable class

序列化的类定义不正确

  • Non-transient non-serializable instance field in serializable class
1可序列化的类中存在不可序列化的属性
2
3class A implements Serializable{
4    //List对象不能直接序列化
5    private List<String> list;
6}

3.5 Checking String equality using == or !=

Stirng 对象的比较使用了 == 或 !=

  • Comparison of String parameter using == or !=
1Stirng 对象的比较使用了 == 或 !=
2
3String对象的比较应该使用 equals() 方法
4

3.6 Confusing method name

不规范的方法名

  • Method names should start with a lower case letter
1方法名应该以小写字母开头,符合驼峰式命名格式

4 Malicious code vulnerability 恶意代码漏洞

4.1 Method returning array may expose internal representation

返回数组的方法可以暴露内部表示

  • May expose internal representation by returning reference to mutable object
1可以通过返回的可变对象的引用来公开内部表示
2
3class A{
4    private List<String> list = new ArrayList<>();
5    public List<String> getList(){
6        return list;
7    }
8}
  • May expose internal representation by incorporating reference to mutable object
1可以通过引用可变对象来公开内部表示
2
3class A{
4    private List<String> list;
5    public void setList(List<String> list){
6        this.list = list;
7    }
8}

转载请注明出处
本文链接:zdran.com/20180717.ht…