☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

if...else-if 分枝的条件不应被遮盖

9.1.3 ID_if_hiddenCondition
目录 › next › previous

if...else-if 分枝中,如果前面的条件被满足,后面的分枝就不会被执行,所以如果前面的条件是后面条件的一部分,或者前面的条件包含后面的条件,即使后面的条件可以被满足,其分枝也得不到执行机会。

示例:

if (condition1) {
    branch1
}
else if (condition2) {
    branch2
}
else if (condition1 && condition3) {  // Non-compliant, see the previous ‘condition1’
    branch3
}
else {
    branch4
}

如果 condition1 为 true,branch1 将得以执行,branch3 不会被执行,如果 condition1 为 false,branch3 还是不会被执行,称 branch3 被 condition1 遮盖了,branch3 永远不会得到执行机会。

如果前面的条件包含后面的条件,同样也会遮盖后面的条件,如:

if (condition1 || condition2) {
    branch1
}
else if (condition2) {  // Non-compliant, see the previous ‘condition2’
    branch2
}
else {
    branch3
}

同理,branch2 永远也不会被执行。

相关

ID_if_identicalCondition

参考

CWE-561 CWE-670
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.