CWE-571 表达式永真

Expression is Always True

结构: Simple

Abstraction: Variant

状态: Draft

被利用可能性: unkown

基本描述

The software contains an expression that will always evaluate to true.

相关缺陷

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

  • cwe_Nature: CanPrecede cwe_CWE_ID: 561 cwe_View_ID: 1000

  • cwe_Nature: CanPrecede cwe_CWE_ID: 561 cwe_View_ID: 699

适用平台

Language: {'cwe_Class': 'Language-Independent', 'cwe_Prevalence': 'Undetermined'}

常见的影响

范围 影响 注释
Other ['Quality Degradation', 'Varies by Context']

可能的缓解方案

Testing

策略:

Use Static Analysis tools to spot such conditions.

示例代码

In the following Java example the updateInventory() method used within an e-business product ordering/inventory application will check if the input product number is in the store or in the warehouse. If the product is found, the method will update the store or warehouse database as well as the aggregate product database. If the product is not found, the method intends to do some special processing without updating any database.

bad Java


public void updateInventory(String productNumber) {
boolean isProductAvailable = false;
boolean isDelayed = false;

if (productInStore(productNumber)) {
isProductAvailable = true;
updateInStoreDatabase(productNumber);
}
else if (productInWarehouse(productNumber)) {
isProductAvailable = true;
updateInWarehouseDatabase(productNumber);
}
else {
isProductAvailable = true;
}

if ( isProductAvailable ) {
updateProductDatabase(productNumber);
}
else if ( isDelayed ) {

/ Warn customer about delay before order processing /
...
}
}

However, the method never sets the isDelayed variable and instead will always update the isProductAvailable variable to true. The result is that the predicate testing the isProductAvailable boolean will always evaluate to true and therefore always update the product database. Further, since the isDelayed variable is initialized to false and never changed, the expression always evaluates to false and the customer will never be warned of a delay on their product.

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
CERT C Secure Coding MSC00-C Compile cleanly at high warning levels
Software Fault Patterns SFP1 Glitch in computation