Logically dead code , 逻辑死代码, 也就是永远也不会被执行到的代码。在Coverity扫描种属于中风险代码漏洞。
示例代码
void deadcode(Object o) throws Exception {
if(o == null) {
throw new Exception();
}
if(o == null) {
// This line cannot be reached!
System.out.println("o is null"); // 死代码, 永远不会执行
}
}
这里的代码逻辑是: 首先判断如果 o 为空则抛出异常, 但是后面的代码有判断如果o为空则干其他的事情, 所以后面的if(o == null) {
里面的代码时永远也执行不了的。
扫描方式和结果
这个弱点通过以下三种方式都可以扫描出来。
- Eclipse IDE
- Eclipse 插件 Synopsys Code Sight
- Coverity 命令工具
Eclipse IDE提示信息
也Eclipse IDE种提示错误很简单
Dead code