我是靠谱客的博主 饱满小猫咪,最近开发中收集的这篇文章主要介绍Coverity中的bug们,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.DM_DEFAULT_ENCODING
 
1.1 Found reliance on default encoding in com.cmcc.aoi.httprequest.service.HttpRequest.sendGet(String, String): new java.io.InputStreamReader(InputStream)

Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behaviour to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.

new BufferedReader(new InputStreamReader(connection.getInputStream()));
 
修改为: InputStreamReader fileData = new InputStreamReader(file ,"utf-8");


2. RV_RETURN_VALUE_IGNORED_BAD_PRACTICE    忽略了 java.io.File.mkdirs() 的异常返回值。

dirFile.mkdirs();
修改为
boolean mkdirs = dirFile.mkdirs();
logger.debug("debug",mkdirs);

3.RuntimeException 捕获 (FB.REC_CATCH_EXCEPTION)

catch(Exception e){}
改为
catch(RunTimeException e){
    throw e
}catch(Exception e){}

4.Comparator不实现Serializable(SE_COMPARATOR_SHOULD_BE_SERIALIZABLE)

implements Comparator,Serializable{}

5.Bx:有问题的装箱 (Boxing) 原语值 (FB.BX_UNBOXING_IMMEDIATELY_REBOXED)

    defect: 装箱 (boxed) 值被拆箱 (unboxed),然后又被立即重新装箱 (reboxed)。

Integer.valueOf(arg).intValue());-->Integer.valueOf(arg); 

6.Bx:有问题的装箱 (Boxing) 原语值 (FB.BX_BOXING_IMMEDIATELY_UNBOXED_TO_PERFORM_COERCION)

1. defect: 原语值被装箱 (boxed),然后又被拆箱 (unboxed) 以执行原语强制。

new Double(value).intValue();-->value.intValue())

7.在循环中使用了 + 运算符连接字符串 (FB.SBSC_USE_STRINGBUFFER_CONCATENATION)

 

每次循环里的字符串+连接,都会新产生一个string对象,在java中,新建一个对象的代价是很昂贵的,特别是在循环语句中,效率较低。故在循环中一般使用StringBuffer.append来代替string的+运算符

// This is bad
  String s = "";
  for (int i = 0; i < field.length; ++i) {
    s = s + field[i];
  }
 

// This is better

  StringBuffer buf = new StringBuffer();
  for (int i = 0; i < field.length; ++i) {
    buf.append(field[i]);
  }
  String s = buf.toString();

 

 

最后

以上就是饱满小猫咪为你收集整理的Coverity中的bug们的全部内容,希望文章能够帮你解决Coverity中的bug们所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(187)

评论列表共有 0 条评论

立即
投稿
返回
顶部