forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitialisationNotRun.ql
More file actions
43 lines (39 loc) · 1.13 KB
/
InitialisationNotRun.ql
File metadata and controls
43 lines (39 loc) · 1.13 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
39
40
41
42
43
/**
* @name Initialization code not run
* @description A global variable has initialization code, but that code is never run (i.e. is called directly or indirectly from main()). Accessing uninitialized variables leads to undefined results.
* @kind problem
* @id cpp/initialization-not-run
* @problem.severity warning
* @tags reliability
* security
* external/cwe/cwe-456
*/
import cpp
import semmle.code.cpp.pointsto.CallGraph
predicate global(GlobalVariable v)
{
not exists(v.getInitializer())
and not v.getType() instanceof ArrayType
and not v.getType() instanceof Class
and v.getAnAccess().isUsedAsLValue()
}
predicate mainCalled(Function f)
{
f.getQualifiedName() = "main"
or
exists(Function caller |
mainCalled(caller) and allCalls(caller, f))
}
predicate called(Function f)
{
mainCalled(f)
or
exists(FunctionAccess fa | fa.getTarget() = f)
}
from GlobalVariable v
where global(v)
and not exists(VariableAccess lval |
v.getAnAccess() = lval and lval.isUsedAsLValue() and
called(lval.getEnclosingFunction())
)
select v, "Initialization code for '" + v.getName() + "' is never run."