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