CWE-561 死代码

Dead Code

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: unkown

基本描述

The software contains dead code, which can never be executed.

扩展描述

Dead code is source code that can never be executed in a running program. The surrounding code makes it impossible for a section of code to ever be executed.

相关缺陷

  • cwe_Nature: ChildOf cwe_CWE_ID: 1164 cwe_View_ID: 1000 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 1164 cwe_View_ID: 699 cwe_Ordinal: Primary

常见的影响

范围 影响 注释
Other Quality Degradation Dead code that results from code that can never be executed is an indication of problems with the source code that needs to be fixed and is an indication of poor quality.
Other Reduce Maintainability

检测方法

Architecture or Design Review

According to SOAR, the following detection techniques may be useful:

Highly cost effective:
  • Inspection (IEEE 1028 standard) (can apply to requirements, design, source code, etc.)
  • Formal Methods / Correct-By-Construction
Cost effective for partial coverage:
  • Attack Modeling

Automated Static Analysis - Binary or Bytecode

According to SOAR, the following detection techniques may be useful:

Highly cost effective:
  • Binary / Bytecode Quality Analysis
  • Compare binary / bytecode to application permission manifest

Dynamic Analysis with Manual Results Interpretation

According to SOAR, the following detection techniques may be useful:

Cost effective for partial coverage:
  • Automated Monitored Execution

Automated Static Analysis

According to SOAR, the following detection techniques may be useful:

Cost effective for partial coverage:
  • Permission Manifest Analysis

Automated Static Analysis - Source Code

According to SOAR, the following detection techniques may be useful:

Highly cost effective:
  • Source Code Quality Analyzer
Cost effective for partial coverage:
  • Warning Flags
  • Source code Weakness Analyzer
  • Context-configured Source Code Weakness Analyzer

Dynamic Analysis with Automated Results Interpretation

According to SOAR, the following detection techniques may be useful:

Cost effective for partial coverage:
  • Web Application Scanner
  • Web Services Scanner
  • Database Scanners

Manual Static Analysis - Source Code

According to SOAR, the following detection techniques may be useful:

Highly cost effective:
  • Manual Source Code Review (not inspections)
Cost effective for partial coverage:
  • Focused Manual Spotcheck - Focused manual analysis of source

可能的缓解方案

Implementation

策略:

Remove dead code before deploying the application.

Testing

策略:

Use a static analysis tool to spot dead code.

示例代码

The condition for the second if statement is impossible to satisfy. It requires that the variables be non-null, while on the only path where s can be assigned a non-null value there is a return statement.

bad C++

String s = null;
if (b) {
s = "Yes";
return;
}

if (s != null) {
Dead();
}

In the following class, two private methods call each other, but since neither one is ever invoked from anywhere else, they are both dead code.

bad Java

public class DoubleDead {
private void doTweedledee() {
doTweedledumb();
}
private void doTweedledumb() {
doTweedledee();
}
public static void main(String[] args) {
System.out.println("running DoubleDead");
}
}

(In this case it is a good thing that the methods are dead: invoking either one would cause an infinite loop.)

The field named glue is not used in the following class. The author of the class has accidentally put quotes around the field name, transforming it into a string constant.

bad Java

public class Dead {
String glue;

public String getGlue() {
return "glue";
}
}

分析过的案例

标识 说明 链接

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
CERT C Secure Coding MSC07-C Detect and remove dead code
SEI CERT Perl Coding Standard MSC00-PL Exact Detect and remove dead code
Software Fault Patterns SFP2 Unused Entities
OMG ASCMM ASCMM-MNT-20

引用