blob: dd3308713ac1c6fcbbfb4e5798fe4dccbf88e04d [file] [log] [blame]
Jordy Roseba0f61c2010-06-18 22:49:11 +00001// RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-experimental-checks -verify -analyzer-constraints=basic %s
2// RUN: %clang_cc1 -analyze -analyzer-check-objc-mem -analyzer-experimental-checks -verify -analyzer-constraints=range %s
3#include <limits.h>
4
5// These are used to trigger warnings.
6typedef typeof(sizeof(int)) size_t;
7void *malloc(size_t);
8void free(void *);
9#define NULL ((void*)0)
10
11//---------------
12// Plus/minus
13//---------------
14
15void separateExpressions (int a) {
16 int b = a + 1;
17 --b;
18
19 char* buf = malloc(1);
20 if (a != 0 && b == 0)
21 return; // no-warning
22 free(buf);
23}
24
25void oneLongExpression (int a) {
26 // Expression canonicalization should still allow this to work, even though
27 // the first term is on the left.
28 int b = 15 + a + 15 - 10 - 20;
29
30 char* buf = malloc(1);
31 if (a != 0 && b == 0)
32 return; // no-warning
33 free(buf);
34}
35
Jordy Roseb4954a42010-06-21 20:15:15 +000036void mixedTypes (int a) {
37 char* buf = malloc(1);
38
39 // Different additive types should not cause crashes when constant-folding.
40 // This is part of PR7406.
41 int b = a + 1LL;
42 if (a != 0 && (b-1) == 0) // not crash
43 return; // no warning
44
45 int c = a + 1U;
46 if (a != 0 && (c-1) == 0) // not crash
47 return; // no warning
48
49 free(buf);
50}
51
Jordy Roseba0f61c2010-06-18 22:49:11 +000052//---------------
53// Comparisons
54//---------------
55
56// Equality and inequality only
57void eq_ne (unsigned a) {
58 char* b = NULL;
59 if (a == UINT_MAX)
60 b = malloc(1);
61 if (a+1 != 0)
62 return; // no-warning
63 if (a-1 != UINT_MAX-1)
64 return; // no-warning
65 free(b);
66}
67
68void ne_eq (unsigned a) {
69 char* b = NULL;
70 if (a != UINT_MAX)
71 b = malloc(1);
72 if (a+1 == 0)
73 return; // no-warning
74 if (a-1 == UINT_MAX-1)
75 return; // no-warning
76 free(b);
77}
78
Jordy Roseb4954a42010-06-21 20:15:15 +000079// Mixed typed inequalities (part of PR7406)
80// These should not crash.
81void mixed_eq_ne (int a) {
82 char* b = NULL;
83 if (a == 1)
84 b = malloc(1);
85 if (a+1U != 2)
86 return; // no-warning
87 if (a-1U != 0)
88 return; // no-warning
89 free(b);
90}
91
92void mixed_ne_eq (int a) {
93 char* b = NULL;
94 if (a != 1)
95 b = malloc(1);
96 if (a+1U == 2)
97 return; // no-warning
98 if (a-1U == 0)
99 return; // no-warning
100 free(b);
101}
102
Jordy Roseba0f61c2010-06-18 22:49:11 +0000103
104// Simple order comparisons with no adjustment
105void baselineGT (unsigned a) {
106 char* b = NULL;
107 if (a > 0)
108 b = malloc(1);
109 if (a == 0)
110 return; // no-warning
111 free(b);
112}
113
114void baselineGE (unsigned a) {
115 char* b = NULL;
116 if (a >= UINT_MAX)
117 b = malloc(1);
118 if (a == UINT_MAX)
119 free(b);
120 return; // no-warning
121}
122
123void baselineLT (unsigned a) {
124 char* b = NULL;
125 if (a < UINT_MAX)
126 b = malloc(1);
127 if (a == UINT_MAX)
128 return; // no-warning
129 free(b);
130}
131
132void baselineLE (unsigned a) {
133 char* b = NULL;
134 if (a <= 0)
135 b = malloc(1);
136 if (a == 0)
137 free(b);
138 return; // no-warning
139}
140
141
142// Adjustment gives each of these an extra solution!
143void adjustedGT (unsigned a) {
144 char* b = NULL;
145 if (a-1 > UINT_MAX-1)
146 b = malloc(1);
147 return; // expected-warning{{leak}}
148}
149
150void adjustedGE (unsigned a) {
151 char* b = NULL;
152 if (a-1 >= UINT_MAX-1)
153 b = malloc(1);
154 if (a == UINT_MAX)
155 free(b);
156 return; // expected-warning{{leak}}
157}
158
159void adjustedLT (unsigned a) {
160 char* b = NULL;
161 if (a+1 < 1)
162 b = malloc(1);
163 return; // expected-warning{{leak}}
164}
165
166void adjustedLE (unsigned a) {
167 char* b = NULL;
168 if (a+1 <= 1)
169 b = malloc(1);
170 if (a == 0)
171 free(b);
172 return; // expected-warning{{leak}}
173}
174
175
176// Tautologies
177void tautologyGT (unsigned a) {
178 char* b = malloc(1);
179 if (a > UINT_MAX)
180 return; // no-warning
181 free(b);
182}
183
184void tautologyGE (unsigned a) {
185 char* b = malloc(1);
186 if (a >= 0)
187 free(b);
188 return; // no-warning
189}
190
191void tautologyLT (unsigned a) {
192 char* b = malloc(1);
193 if (a < 0)
194 return; // no-warning
195 free(b);
196}
197
198void tautologyLE (unsigned a) {
199 char* b = malloc(1);
200 if (a <= UINT_MAX)
201 free(b);
202 return; // no-warning
203}