-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathVariablesInsideSwitchStatement.ql
More file actions
38 lines (36 loc) · 1.47 KB
/
VariablesInsideSwitchStatement.ql
File metadata and controls
38 lines (36 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* @id c/cert/variables-inside-switch-statement
* @name DCL41-C: Do not declare variables inside a switch statement before the first case label
* @description Declaring a variable in a switch statement before the first case label can result in
* reading uninitialized memory which is undefined behaviour.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/cert/id/dcl41-c
* correctness
* maintainability
* readability
* external/cert/severity/medium
* external/cert/likelihood/unlikely
* external/cert/remediation-cost/medium
* external/cert/priority/p4
* external/cert/level/l3
* coding-standards/baseline/style
* external/cert/obligation/rule
*/
import cpp
import codingstandards.c.cert
from SwitchCase case, SwitchStmt stmt, VariableDeclarationEntry d
where
not isExcluded(d, Declarations2Package::variablesInsideSwitchStatementQuery()) and
case.getSwitchStmt() = stmt and
//first case
not exists(case.getPreviousSwitchCase()) and
exists(string filepath, int declarationLine, int caseLine, int stmtLine |
d.getLocation().hasLocationInfo(filepath, declarationLine, _, _, _) and
stmt.getLocation().hasLocationInfo(filepath, stmtLine, _, _, _) and
case.getLocation().hasLocationInfo(filepath, caseLine, _, _, _) and
declarationLine > stmtLine and
declarationLine < caseLine
)
select d, "Declaration is located in switch $@.", stmt, "statement"