经过一段时间白盒静态扫描,目前已经扫了两个包,发现了100多个bug。
值得欣慰的是,自己开发的工具也扫描出了一些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 | Performance | 循环中创建字符串 | 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(); |
(以上言论仅代表作者的个人观点,不代表51Testing观点)
版权声明:本文出自xiaohanjiang的51Testing软件测试博客:http://www.51testing.com/?294525
原创作品,转载时请务必以超链接形式标明本文原始出处、作者信息和本声明,否则将追究法律责任。
相关链接: