Skip to content

Commit 02bf800

Browse files
authored
Update FindIncorrectlyUsedSwitch.ql
1 parent 94bd2a3 commit 02bf800

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-561/FindIncorrectlyUsedSwitch.ql

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import cpp
1717
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
18-
import semmle.code.cpp.commons.Exclusions
1918

2019
/** Holds if the range contains no boundary values. */
2120
predicate isRealRange(Expr exp) {
@@ -36,13 +35,19 @@ predicate isRealRange(Expr exp) {
3635
upperBound(exp) != 32767 and
3736
upperBound(exp) != 255 and
3837
upperBound(exp) != 127 and
38+
upperBound(exp) != 63 and
39+
upperBound(exp) != 31 and
40+
upperBound(exp) != 15 and
41+
upperBound(exp) != 7 and
3942
lowerBound(exp) != -2147483648 and
4043
lowerBound(exp) != -268435456 and
4144
lowerBound(exp) != -33554432 and
4245
lowerBound(exp) != -8388608 and
4346
lowerBound(exp) != -65536 and
4447
lowerBound(exp) != -32768 and
45-
lowerBound(exp) != -128
48+
lowerBound(exp) != -128 and
49+
lowerBound(exp) != 0 and
50+
lowerBound(exp) != upperBound(exp)
4651
or
4752
lowerBound(exp) = 0 and
4853
upperBound(exp) = 1
@@ -124,7 +129,12 @@ predicate isCodeBeforeCase(SwitchStmt swtmp) {
124129
from SwitchStmt sw, string msg
125130
where
126131
isRealRange(sw.getExpr()) and
127-
isRealRange(sw.getExpr().getAChild*()) and
132+
not exists(Expr exptmp |
133+
exptmp = sw.getExpr().getAChild*() and
134+
not exptmp.isConstant() and
135+
not isRealRange(exptmp)
136+
) and
137+
(sw.getASwitchCase().terminatesInBreakStmt() or sw.getASwitchCase().terminatesInReturnStmt()) and
128138
(
129139
isNotAllSelected(sw) and msg = "The range of condition values is less than the selection."
130140
or

0 commit comments

Comments
 (0)