白盒测试静态检查--java经典bug
经过一段时间白盒静态扫描,目前已经扫了两个包,发现了100多个bug.
经典案例。
如下列表展示
Type
headline
demo
Comment
1
多线程
单例action里面包含有属性并且允许读写
public class XxxAction extends Action{
private StringBuffer vo = new StringBuffer();
public void setVo(StringBuffer invo){
vo=invo;
}
public StringBuffer getVo(){
return this.vo;
.................
单例action里面包含有属性进行读写
会引起数据错乱
2
Bad practice
字符串比较用==或者=
if(dao.getADTerminalByServiceCode(serviceCode).getServicecode() == serviceCode){
String类型的比较不要用==或者!=,应该用equal方法
就上面的代码永远都为false,因为==表示在同一常量区才会为true
3
字符串分割正则表达式使用不对
String[] area = areaSize.split("*");
正则表达式使用不对,会一直抛出异常
应该使用转义字符
4
字符串比较的空点异常
vo.getVirtualFlag().equals("NO")
建议修改为"NO".equals(vo.getVirtualFlag())
5
对象比较不对
if (this == obj) {
return true;
}
对象必须重再了hashcode方法,比较才有意义
6
流没关闭
InputStream input = new BufferedInputStream(file.getInputStream());
流没有关闭
会造成文件句柄资源耗尽,那么就无法再打开新的流
7
connection statement rs等没关闭
try{
.....
} catch(..){
}finally
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
在rs有异常会关闭不了statement conn
在statement有异常会关闭不了conn
8
<a>Performance</a>
循环中创建字符串
String children = "" ;
ClassSpecVO[] t = cs.getTree();
if ( t == null )
return "";
for ( int i = 0 ; i < t.length ; i ++ )
{
if ( children.length() != 0 )
children += ",";
children += constructJsonString(t[i]);
children会循环创建对象,必须改为StringBuffer对象。用append进行连接字符串
9
不应该创建字符串对象
String vsplist=new String();
修改为高效的构造方法String vsplist=“”
“”的字符串会在常量区不用创建对象
本文转自hcy's workbench博客园博客,原文链接:http://www.cnblogs.com/alterhu/archive/2012/03/14/2395696.html,如需转载请自行联系原作者。