Debian Codegraph @

clang/test/Analysis/global-region-invalidation.c

raw
1 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -disable-free -verify %s \
2 // RUN: -analyzer-checker=core,deadcode,optin.taint,debug.TaintTest,debug.ExprInspection
3
4 void clang_analyzer_eval(int);
5
6 // Note, we do need to include headers here, since the analyzer checks if the function declaration is located in a system header.
7 #include "Inputs/system-header-simulator.h"
8
9 // Test that system header does not invalidate the internal global.
10 int size_rdar9373039 = 1;
11 int rdar9373039(void) {
12 int x;
13 int j = 0;
14
15 for (int i = 0 ; i < size_rdar9373039 ; ++i)
16 x = 1;
17
18 // strlen doesn't invalidate the value of 'size_rdar9373039'.
19 int extra = (2 + strlen ("Clang") + ((4 - ((unsigned int) (2 + strlen ("Clang")) % 4)) % 4)) + (2 + strlen ("1.0") + ((4 - ((unsigned int) (2 + strlen ("1.0")) % 4)) % 4));
20
21 for (int i = 0 ; i < size_rdar9373039 ; ++i)
22 j += x; // no-warning
23
24 return j;
25 }
26
27 // Test stdin does not get invalidated by a system call nor by an internal call.
28 void foo(void);
29 int stdinTest(void) {
30 int i = 0;
31 fscanf(stdin, "%d", &i);
32 foo();
33 int m = i; // expected-warning + {{tainted}}
34 fscanf(stdin, "%d", &i);
35 int j = i; // expected-warning + {{tainted}}
36 return m + j; // expected-warning + {{tainted}}
37 }
38
39 // Test that const integer does not get invalidated.
40 const int x = 0;
41 int constIntGlob(void) {
42 const int *m = &x;
43 foo();
44 return 3 / *m; // expected-warning {{Division by zero}}
45 }
46
47 extern const int y;
48 int constIntGlobExtern(void) {
49 if (y == 0) {
50 foo();
51 return 5 / y; // expected-warning {{Division by zero}}
52 }
53 return 0;
54 }
55
56 static void * const ptr = 0;
57 void constPtrGlob(void) {
58 clang_analyzer_eval(ptr == 0); // expected-warning{{TRUE}}
59 foo();
60 clang_analyzer_eval(ptr == 0); // expected-warning{{TRUE}}
61 }
62
63 static const int x2 = x;
64 void constIntGlob2(void) {
65 clang_analyzer_eval(x2 == 0); // expected-warning{{TRUE}}
66 foo();
67 clang_analyzer_eval(x2 == 0); // expected-warning{{TRUE}}
68 }
69
70 void testAnalyzerEvalIsPure(void) {
71 extern int someGlobal;
72 if (someGlobal == 0) {
73 clang_analyzer_eval(someGlobal == 0); // expected-warning{{TRUE}}
74 clang_analyzer_eval(someGlobal == 0); // expected-warning{{TRUE}}
75 }
76 }
77
78 // Test that static variables with initializers do not get reinitialized on
79 // recursive calls.
80 void Function2(void);
81 int *getPtr(void);
82 void Function1(void) {
83 static unsigned flag;
84 static int *p = 0;
85 if (!flag) {
86 flag = 1;
87 p = getPtr();
88 }
89 int m = *p; // no-warning: p is never null.
90 m++;
91 Function2();
92 }
93 void Function2(void) {
94 Function1();
95 }
96
97 void SetToNonZero(void) {
98 static int g = 5;
99 clang_analyzer_eval(g == 5); // expected-warning{{TRUE}}
100 }