Skip to content

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

Merged
merged 9 commits into from
Aug 4, 2025
Next Next commit
Create test for InitialisationNotRun
  • Loading branch information
codeqlhelper authored Jul 27, 2025
commit c2d0a12e1e0b458657835be36671291cbb651569
32 changes: 32 additions & 0 deletions cpp/ql/test/query-tests/Critical/InitialisationNotRun/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <string.h>

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;
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy