您的位置:首页 > 编程语言

用FindBugs分析代码漏洞

2015-06-23 11:22 337 查看

Dead store to local variable


例:

系统提示 JSONArray arrayValue = new JSONArray();这行Dead store to local variable
是因为 arrayValue = toJSONArray(objectList, attributes,0);有重新实例化了对象arrayValue ,前一个 new JSONArray();是没有必要的。这样会消耗资源。
建议:JSONArray arrayValue =toJSONArray(objectList, attributes,0);


May
expose internal representation by incorporating reference to mutable object


修改一个对象,可能会引起其他对象的修改,因为JAVA里,对象是引用传递的......
建议:setObj的时候,对象不要直接赋值(this.regDate = regDate),而是赋值传入对象的拷贝(this.regDate
= (Date)regDate.clone();)。

Write to static field from instance method

[u]向static字段中写入值[/u]

[u]建议改为:

去掉static。


[/u]


FindBugs是一个专门分析JAVA代码问题的静态代码扫描工具,它是由一位马里兰大学的博士写的,官方网站是:http://findbugs.sourceforge.net/index.html,里面有FindBugs工具的下载,并且也包含了那位博士写的关于静态代码分析引擎的一些论文,不过偶还没看。FindBugs可以发现的问题包括:多线程竞争问题,性能问题,安全问题,代码规范......这些下面我都会详细介绍:>,如果想了解更多关于其他语言的静态代码扫描工具信息,可以参考《[hyddd安全性测试笔记2]浅淡静态代码分析工具》。
一.FindBugs的使用
  先说说FindBugs的使用,FindBugs它提供两种工具形式,一种是界面形式的工具,如下图:
  


  另外一种是以Eclipse插件的形式提供的:
  


  个人比较喜欢插件的形式,方便~!
  下面简单介绍FindBugs插件的使用:
  1.右键项目Pop Menu->Find Bugs->Find Bugs,如图:
  


  2.把FindBugs插件的3个功能WorkSpace导出,如图:
  


  (1). Bug Explorer:这里显示的是用FindBugs对这个项目扫描的结果。现在只扫出了一个类型的BUG,同时这个类型的BUG数量为1(见图中括号)。继续打开[DLS]Dead store to local variable,选择其中一项,右键单击,选择Pop Menu中的“Show Bug Details”,我们便会跳转到Bug Details这个WorkSpace。
  


  (2).Bug Details:这里是对BUG问题的描述,包括:Bug类别及程度,Bug的定位,对Bug的解释。
  


  这里这个Bug类别是Dodgy,程度是中等;双击下面会自动定位BUG的位置;最下面是这个BUG的解释,可以根据里面的提示去修改代码:>
  (3).Bug User Annotations:记录一些自己添加的评注。
二.FindBugs的Bug类型及实例代码:
  FindBugs对BUG的解释在官网有详细的说明:http://findbugs.sourceforge.net/bugDescriptions.html#IS2_INCONSISTENT_SYNC。虽然有说明,但说明一般比较简短,而且没有Demo代码,有时候还真没明白它说什么意思:<,所以在这里我打算整理一下之前看过的资料,把问题和解决方案记录下来:
Bad practice:
[Hight]
[H B BC]
Random object created and used only once
[Medium]
[M
B Nm] Method names should start with a lower case letter
[M
B Nm] Class names should start with an upper case letter
[M
B ODR] Method may fail to close database resource
[M B DE] Method might ignore exception

Correctness:
[Hight]
[H C FS]
More arguments are passed that are actually used in the format string
[H C FS]
Format string references missing argument

[H C EC] equals() used to compare array and nonarray

[Medium]
[M C RCN]
Nullcheck of value previously dereferenced

[M C NP]
Method call passes null for unconditionally dereferenced parameter
[M
C NP] Possible null pointer dereference
[M B Eq] Class defines compareTo(...) and uses Object.equals()

Experimental:
[Medium]
[M
X OBL] Method may fail to clean up stream or resource
Internationalization:
......
Malicious code vulnerability:
[Medium]
[M V EI2]
May expose internal representation by incorporating reference to mutable object
[M V EI]
May expose internal representation by returning reference to mutable object
[M
V MS] Public static method may expose internal representation by returning array
Multithreaded correctness:
[Medium]
[M
M IS] Inconsistent synchronization
[M M IS] Inconsistent synchronization追加说明

[M
M NP] Synchronize and null check on the same field

Performance:
[Medium]
[M P Bx]
Method invokes inefficient Number constructor; use static valueOf instead
[M
P Dm] Method invokes toString() method on a String
[M
P UuF] Unused field
Security:
[Medium]
[M S XSS] Servlet reflected cross site scripting vulnerability
Dodgy:  
[Medium]
[M D RCN]
Repeated conditional tests

[M D RCN]
Redundant nullcheck of value known to be non-null
[M
D DLS] Dead store to local variable
[M
D REC] Exception is caught when Exception is not thrown
[M D ICAST] Result of integer multiplication cast to long
.......
-----------------------------------------------------------------
三.参考文档
1.http://blog.csdn.net/axzywan/archive/2008/11/16/3312009.aspx
2.http://www.cnitblog.com/weitom1982/archive/2006/03/31/8367.html
3.http://www.ibm.com/developerworks/cn/java/j-jtp06294/
4.http://leewinq.javaeye.com/blog/283813

findbugs 出错类型及对应解释

1、Dead store to local variable 本地变量存储了闲置不用的对象

举例:

List accountCoList = new ArrayList();

我们为accountCoList新建了一个对象,但是程序的后面并没有使用这个这个新建对象。

建议改为:

List accountCoList = null;
2、Write to static field from instance method 向static字段中写入值

举例:

private static DBRBO dbrBO;

public final void refresh() {

danskeBankBO = null;

dbrBO = null;

fileAndPathBO = null;

}

建议改为:

去掉static。

3、Load of known null value 大体意思是加载了null的对象。

举例

if (null == boList) {
for (int i = 0; i < boList.size(); i++) {

entityList.add(productBOToEntity(boList.get(i)));

}

}

4、Exception is caught when Exception is not thrown 这个意思比较好理解:就是catch了异常但是try里并没有抛出异常

5、Method ignores exceptional return value 没有对方法的异常返回值进行检查

6、Comparison of String objects using == or !=

This code compares java.lang.String objects for reference equality using the == or != operators.

Unless both strings are either constants in a source file, or have been interned using the String.intern() method,

the same string value may be represented by two different String objects. Consider using the equals(Object) method

instead.

从字面意思可以理解String对象进行比较的时候:只有两种情况可以使用== or !=的,这两种情况是;在源文件中是个常数或者是调用

String.intern()方法,使用String的规范化表示形式来进行比较,如果不是这两中情况的话推荐使用.equals(object)方式

7、Method names should start with a lower case letter 这个好理解方法名的第一个字母不能是大写

8、Non-transient non-serializable instance field in serializable class

This Serializable class defines a non-primitive instance field which is neither transient, Serializable,

or java.lang.Object, and does not appear to implement the Externalizable interface or the readObject()

and writeObject() methods.? Objects of this class will not be deserialized correctly if a non-Serializable object

is stored in this field.

这个错误的意思是:在可序列化的类中存在不能序列化或者不能暂存的数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: