DMS
1、A “NullPointerException” could be thrown; “sra” is nullable here.
這種提示是指可能存在空指針異常,需要增加空值檢測(cè)。
說(shuō)明:未做非空校驗(yàn),可能產(chǎn)生空指針
解決方案:加上非空校驗(yàn)
解決方式:先判斷或者先實(shí)例化,再訪問(wèn)里面的屬性或者成員。
2、Cast one of the operands of this multiplication operation to a “l(fā)ong”
說(shuō)明:int數(shù)運(yùn)算最終再把結(jié)果轉(zhuǎn)為long將有可能產(chǎn)生溢出
解決方案:轉(zhuǎn)換為long型預(yù)算
舉例:
long bigNum = Integer.MAX_VALUE + 2; // Noncompliant. Yields -2147483647
換為
long bigNum = Integer.MAX_VALUE + 2L;
3、Call “remove()” on “requestContainer”.
說(shuō)明:防止內(nèi)存泄露溢出,ThreadLocal字段【requestContainer】應(yīng)該至少調(diào)用一次remove()方法。
// 解決方案:定義刪除方法
public void removeRequest() {
requestContainer.remove();
}
4、Use try-with-resources or close this “FileInputStream” in a “finally” clause.
說(shuō)明:使用try-with-resources或在 “finally” 子句中關(guān)閉此 “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” 子句中關(guān)閉流
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);
}
5、Change this condition so that it does not always evaluate to “false”
說(shuō)明:checkInsertParam方法沒(méi)有返回false的情況一直是true,所以條件判斷后一直返回的結(jié)果為false。需要改條件判斷或者添加方案中異常false情況返回。
6、Use the “equals” method if value comparison was intended.
說(shuō)明:使用引用等式==或!=,比較java.lang.String或裝箱類型(如java.lang.Integer)的兩個(gè)實(shí)例幾乎總是一個(gè)錯(cuò)誤,因?yàn)樗皇潜容^實(shí)際值,而是比較內(nèi)存中的位置
解決:將 “==” 換成 equals 比較
7、Do something with the “boolean” value returned by “delete”.
//解決方案:增加false判斷
if (!csvFile.delete()) {
log.error("文件刪除失敗");
}
8、Either re-interrupt this method or rethrow the “InterruptedException” that can be caught here.
//解決方案
try(){
//業(yè)務(wù)代碼...
}catch (InterruptedException e){
? //拋出InterruptedException 異常需要重新清除線程的中斷狀態(tài),添加如下
Thread.currentThread().interrupt();
}
DQC
1、Use “BigDecimal.valueOf” instead.
說(shuō)明:由于浮點(diǎn)不精確,您不太可能從BigDecimal(double)構(gòu)造函數(shù)中獲得預(yù)期的值。
//解決方案
BigDecimal.valueOf((double) (Float) r)
2、Remove this “break” statement or make it conditional.
說(shuō)明:移除break;或者把它放在條件中
3、Remove this conditional structure or edit its code blocks so that they’re not all the same.
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-554519.html
說(shuō)明:寫if else的時(shí)候,卡條件卡得離散了一點(diǎn),本身可以合成一個(gè)的,結(jié)果寫成了多級(jí)if,增加了程序的復(fù)雜度。
解決:去掉if else語(yǔ)句文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-554519.html
到了這里,關(guān)于SonarQube掃描常見(jiàn)Bug、漏洞修復(fù)整理(持續(xù)更新中)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!