Coverity中的bug们

编程知识 更新时间:2023-05-02 19:06:20

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们

本文发布于:2023-04-28 04:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/fbd8b699c494cd1f3f95085845840a74.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Coverity   bug

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 107681文章数
  • 27234阅读数
  • 0评论数