【转】白盒测试静态检查——Java经典Bug

上一篇 / 下一篇  2010-08-01 22:43:59 / 个人分类:白盒测试

 

  经过一段时间白盒静态扫描,目前已经扫了两个包,发现了100多个bug。

  值得欣慰的是,自己开发的工具也扫描出了一些bug,这段时间熬夜的开发没有白费。总结一下,主要问题的分类。随着这些经验的积累,可以作为日后人工审查和开发学习的。

  经典案例:

 TypeheadlinedemoComment
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里面包含有属性进行读写
会引起数据错乱
2Bad
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
8Performance循环中创建字符串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(); 

TAG: 白盒测试 静态检查

 

评分:0

我来说两句

Open Toolbar