-
Notifications
You must be signed in to change notification settings - Fork 1.7k
C++: Static variables are initialized to zero or null by compiler #20129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
codeqlhelper
wants to merge
9
commits into
github:main
Choose a base branch
from
codeqlhelper:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c2d0a12
Create test for InitialisationNotRun
codeqlhelper 89dcad4
Create InitialisationNotRun.qlref
codeqlhelper cf21997
Reduce false alarms raised by static variables
codeqlhelper 75e545a
Create 2025-07-27-avoid-reporting-static-global-variable.md
codeqlhelper 43bca84
CPP: Convert test to use a stub rather than a library include.
geoffw0 d6fddde
CPP: Add .expected (results before query changes here).
geoffw0 c0638a5
CPP: Update .expected for the changes here.
geoffw0 ee3e7e3
Merge pull request #1 from geoffw0/initnotrun .expected
codeqlhelper 4323e68
Update cpp/ql/src/change-notes/2025-07-27-avoid-reporting-static-glob…
codeqlhelper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
cpp/ql/src/change-notes/2025-07-27-avoid-reporting-static-global-variable.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* The "Initialization code not run" query (`cpp/initialization-not-run`) no longer reports an alert on static global variables that has no dereference. |
2 changes: 2 additions & 0 deletions
2
cpp/ql/test/query-tests/Critical/InitialisationNotRun/InitialisationNotRun.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
| test.cpp:12:16:12:17 | g1 | Initialization code for 'g1' is never run. | | ||
| test.cpp:14:23:14:24 | g3 | Initialization code for 'g3' is never run. | |
1 change: 1 addition & 0 deletions
1
cpp/ql/test/query-tests/Critical/InitialisationNotRun/InitialisationNotRun.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Critical/InitialisationNotRun.ql | ||
|
36 changes: 36 additions & 0 deletions
36
cpp/ql/test/query-tests/Critical/InitialisationNotRun/test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// --- stubs --- | ||
|
||
char *strcpy(char *dest, const char *src); | ||
|
||
// --- tests --- | ||
|
||
class GlobalStorage { | ||
public: | ||
char name[1000]; | ||
}; | ||
|
||
GlobalStorage *g1; // BAD | ||
static GlobalStorage g2; // GOOD | ||
static GlobalStorage *g3; // BAD | ||
// static variables are initialized by compilers | ||
static int a; // GOOD | ||
static int b = 0; // GOOD | ||
|
||
void init() { //initializes g_storage, but is never run from main | ||
g1 = new GlobalStorage(); | ||
g3 = new GlobalStorage(); | ||
} | ||
|
||
void init2(int b) { | ||
for (int i = 0; i < b; ++i) | ||
a *= -1; | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
//init not called | ||
strcpy(g1->name, argv[1]); // g1 is used before init() is called | ||
strcpy(g2.name, argv[1]); // g2 is initialised by compiler | ||
strcpy(g3->name, argv[1]); | ||
b++; | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.