Skip to content

Commit c483a4b

Browse files
authored
Merge pull request #16527 from codeqlhelper/main
C++: Static variables are initialized to zero or null by compiler
2 parents bfc95c6 + 2f7766a commit c483a4b

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

cpp/ql/src/Critical/NotInitialised.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ predicate undefinedLocalUse(VariableAccess va) {
5454
// it is hard to tell when a struct or array has been initialized, so we
5555
// ignore them
5656
not isAggregateType(lv.getUnderlyingType()) and
57+
not lv.isStatic() and // static variables are initialized to zero or null by default
5758
not lv.getType().hasName("va_list") and
5859
va = lv.getAnAccess() and
5960
noDefPath(lv, va) and
@@ -70,7 +71,8 @@ predicate uninitialisedGlobal(GlobalVariable gv) {
7071
va = gv.getAnAccess() and
7172
va.isRValue() and
7273
not gv.hasInitializer() and
73-
not gv.hasSpecifier("extern")
74+
not gv.hasSpecifier("extern") and
75+
not gv.isStatic() // static variables are initialized to zero or null by default
7476
)
7577
}
7678

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The "Variable not initialized before use" query (`cpp/not-initialised`) no longer reports an alert on static variables.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:3:11:3:15 | local | Variable 'local' is not initialized. |
2+
| test.cpp:12:5:12:24 | uninitialised_global | Variable 'uninitialised_global' is not initialized. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Critical/NotInitialised.ql
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
void test1() {
2+
int local;
3+
int x = local; // BAD
4+
5+
static int static_local;
6+
int y = static_local; // GOOD
7+
8+
int initialised = 42;
9+
int z = initialised; // GOOD
10+
}
11+
12+
int uninitialised_global; // BAD
13+
static int uninitialised_static_global; // GOOD
14+
int initialized_global = 0; // GOOD
15+
16+
void test2() {
17+
int a = uninitialised_global;
18+
int b = uninitialised_static_global;
19+
int c = initialized_global;
20+
}

0 commit comments

Comments
 (0)