Skip to content

Commit c2d0a12

Browse files
authored
Create test for InitialisationNotRun
1 parent 13d9d8a commit c2d0a12

File tree

1 file changed

+32
-0
lines changed
  • cpp/ql/test/query-tests/Critical/InitialisationNotRun

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <string.h>
2+
3+
class GlobalStorage {
4+
public:
5+
char name[1000];
6+
};
7+
8+
GlobalStorage *g1; // BAD
9+
static GlobalStorage g2; // GOOD
10+
static GlobalStorage *g3; // BAD
11+
// static variables are initialized by compilers
12+
static int a; // GOOD
13+
static int b = 0; // GOOD
14+
15+
void init() { //initializes g_storage, but is never run from main
16+
g1 = new GlobalStorage();
17+
g3 = new GlobalStorage();
18+
}
19+
20+
void init2(int b) {
21+
for (int i = 0; i < b; ++i)
22+
a *= -1;
23+
}
24+
25+
int main(int argc, char *argv[]) {
26+
//init not called
27+
strcpy(g1->name, argv[1]); // g1 is used before init() is called
28+
strcpy(g2.name, argv[1]); // g2 is initialised by compiler
29+
strcpy(g3->name, argv[1]);
30+
b++;
31+
return 0;
32+
}

0 commit comments

Comments
 (0)