目录 DMS1、A "NullPointerException" could be thrown; "sra" is nullable here.2、Cast one of the opera
这种提示是指可能存在空指针异常,需要增加空值检测。
说明:未做非空校验,可能产生空指针
解决方案:加上非空校验
解决方式:先判断或者先实例化,再访问里面的属性或者成员。
说明:int数运算最终再把结果转为long将有可能产生溢出
解决方案:转换为long型预算 举例: long bigNum = Integer.MAX_VALUE + 2; // Noncompliant. Yields -2147483647 换为 long bigNum = Integer.MAX_VALUE + 2L;
说明:防止内存泄露溢出,ThreadLocal字段【requestContainer】应该至少调用一次remove()方法。
// 解决方案:定义删除方法 public void removeRequest() { requestContainer.remove(); }
说明:使用try-with-resources或在 “finally” 子句中关闭此 “BufferedOutputStream”。
// 解决方案1:使用try-with-resources BufferedOutputStream out = null; try(BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("C://test"))) { out.write(file.getBytes()); out.flush(); return Result.success("上传成功!", null); } catch (IOException e) { return Result.error("上传失败!"); }
// 解决方案2:“finally” 子句中关闭流 BufferedOutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(new File("C://test")); out.write(file.getBytes()); out.flush(); return Result.success("上传成功!", null); } catch (IOException e) { return Result.error("上传失败!"); } finally { CloseIoUtils.closeAll(out); }
说明:checkInsertParam方法没有返回false的情况一直是true,所以条件判断后一直返回的结果为false。需要改条件判断或者添加方案中异常false情况返回。
说明:使用引用等式==或!=,比较java.lang.String或装箱类型(如java.lang.Integer)的两个实例几乎总是一个错误,因为它不是比较实际值,而是比较内存中的位置
解决:将 “==” 换成 equals 比较
//解决方案:增加false判断if (!csvFile.delete()) { log.error("文件删除失败");}
//解决方案 try(){ //业务代码... }catch (InterruptedException e){ //抛出InterruptedException 异常需要重新清除线程的中断状态,添加如下 Thread.currentThread().interrupt(); }
说明:由于浮点不精确,您不太可能从BigDecimal(double)构造函数中获得预期的值。
//解决方案 BigDecimal.valueOf((double) (Float) r)
说明:移除break;或者把它放在条件中
说明:写if else的时候,卡条件卡得离散了一点,本身可以合成一个的,结果写成了多级if,增加了程序的复杂度。
解决:去掉if else语句
来源地址:https://blog.csdn.net/baidu_41937166/article/details/128393987
--结束END--
本文标题: SonarQube扫描常见Bug、漏洞修复整理(持续更新中)
本文链接: https://www.lsjlt.com/news/398426.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0