CWE-484 在Switch语句中省略Break语句

admin 2021年12月16日15:50:22评论55 views字数 2789阅读9分17秒阅读模式

CWE-484 在Switch语句中省略Break语句

Omitted Break Statement in Switch

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: Medium

基本描述

The program omits a break statement within a switch or similar construct, causing code associated with multiple conditions to execute. This can cause problems when the programmer only intended to execute code associated with one condition.

扩展描述

This can lead to critical code executing in situations where it should not.

相关缺陷

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

  • cwe_Nature: ChildOf cwe_CWE_ID: 670 cwe_View_ID: 1000

适用平台

Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'Java', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C#', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'PHP', 'cwe_Prevalence': 'Undetermined'}]

常见的影响

范围 影响 注释
Other Alter Execution Logic This weakness can cause unintended logic to be executed and other unexpected application behavior.

检测方法

White Box

Omission of a break statement might be intentional, in order to support fallthrough. Automated detection methods might therefore be erroneous. Semantic understanding of expected program behavior is required to interpret whether the code is correct.

Black Box

Since this weakness is associated with a code construct, it would be indistinguishable from other errors that produce the same behavior.

可能的缓解方案

Implementation

策略:

Omitting a break statement so that one may fall through is often indistinguishable from an error, and therefore should be avoided. If you need to use fall-through capabilities, make sure that you have clearly documented this within the switch statement, and ensure that you have examined all the logical possibilities.

Implementation

策略:

The functionality of omitting a break statement could be clarified with an if statement. This method is much safer.

示例代码

In both of these examples, a message is printed based on the month passed into the function:

bad Java

public void printMessage(int month){

switch (month) {


case 1: print("January");
case 2: print("February");
case 3: print("March");
case 4: print("April");
case 5: print("May");
case 6: print("June");
case 7: print("July");
case 8: print("August");
case 9: print("September");
case 10: print("October");
case 11: print("November");
case 12: print("December");

}
println(" is a great month");

}

bad C

void printMessage(int month){

switch (month) {


case 1: printf("January");
case 2: printf("February");
case 3: printf("March");
case 4: printf("April");
case 5: printff("May");
case 6: printf("June");
case 7: printf("July");
case 8: printf("August");
case 9: printf("September");
case 10: printf("October");
case 11: printf("November");
case 12: printf("December");

}
printf(" is a great month");

}

Both examples do not use a break statement after each case, which leads to unintended fall-through behavior. For example, calling "printMessage(10)" will result in the text "OctoberNovemberDecember is a great month" being printed.

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
CLASP Omitted break statement
Software Fault Patterns SFP4 Unchecked Status Condition

引用

文章来源于互联网:scap中文网

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年12月16日15:50:22
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   CWE-484 在Switch语句中省略Break语句http://cn-sec.com/archives/613248.html

发表评论

匿名网友 填写信息