blob: 7ca22ada7da7fd83684ea0ef84dc12ca5abfd859 [file] [log] [blame]
Chris Lattner184aa4e2010-07-11 23:34:02 +00001// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-experimental-internal-checks -std=gnu99 -analyzer-check-objc-mem -verify %s -analyzer-constraints=basic -analyzer-store=basic -Wreturn-type
2// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-experimental-internal-checks -std=gnu99 -analyzer-check-objc-mem -verify %s -analyzer-constraints=range -analyzer-store=basic -Wreturn-type
3// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-experimental-internal-checks -std=gnu99 -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-no-purge-dead -verify %s -Wreturn-type
4// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-experimental-internal-checks -std=gnu99 -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -verify %s -Wreturn-type
Ted Kremenek2f54af42008-04-02 16:54:39 +00005
Ted Kremenekf8add9b2009-09-28 23:54:40 +00006typedef unsigned uintptr_t;
7
8extern void __assert_fail (__const char *__assertion, __const char *__file,
9 unsigned int __line, __const char *__function)
10 __attribute__ ((__noreturn__));
11
12#define assert(expr) \
13 ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
Ted Kremenek0fe33bc2008-04-22 21:10:18 +000014
Ted Kremenek2f54af42008-04-02 16:54:39 +000015void f1(int *p) {
16 if (p) *p = 1;
17 else *p = 0; // expected-warning{{ereference}}
18}
Ted Kremenekb9ab6902008-04-21 23:44:17 +000019
20struct foo_struct {
21 int x;
22};
23
24int f2(struct foo_struct* p) {
25
26 if (p)
27 p->x = 1;
28
Ted Kremenek452b84d2010-03-23 01:11:38 +000029 return p->x++; // expected-warning{{Field access results in a dereference of a null pointer (loaded from variable 'p')}}
Ted Kremenekb9ab6902008-04-21 23:44:17 +000030}
Ted Kremenek9704eac2008-04-22 04:56:55 +000031
32int f3(char* x) {
33
34 int i = 2;
35
36 if (x)
37 return x[i - 1];
38
Ted Kremeneke576af22009-11-24 01:33:10 +000039 return x[i+1]; // expected-warning{{Dereference of null pointer}}
Ted Kremenek9704eac2008-04-22 04:56:55 +000040}
41
Ted Kremeneke2013f52008-04-29 23:25:09 +000042int f3_b(char* x) {
43
44 int i = 2;
45
46 if (x)
47 return x[i - 1];
48
Ted Kremeneke576af22009-11-24 01:33:10 +000049 return x[i+1]++; // expected-warning{{Dereference of null pointer}}
Ted Kremeneke2013f52008-04-29 23:25:09 +000050}
51
Ted Kremenek0fe33bc2008-04-22 21:10:18 +000052int f4(int *p) {
53
Daniel Dunbar4489fe12008-08-05 00:07:51 +000054 uintptr_t x = (uintptr_t) p;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +000055
56 if (x)
57 return 1;
58
59 int *q = (int*) x;
Ted Kremenek452b84d2010-03-23 01:11:38 +000060 return *q; // expected-warning{{Dereference of null pointer (loaded from variable 'q')}}
Ted Kremeneka5488462008-04-22 21:39:21 +000061}
62
Ted Kremeneke1c2a672009-01-13 01:04:21 +000063int f4_b() {
64 short array[2];
Douglas Gregord4eea832010-04-09 00:35:39 +000065 uintptr_t x = array; // expected-warning{{incompatible pointer to integer conversion}}
66 short *p = x; // expected-warning{{incompatible integer to pointer conversion}}
Ted Kremeneke1c2a672009-01-13 01:04:21 +000067
68 // The following branch should be infeasible.
69 if (!(p = &array[0])) {
70 p = 0;
71 *p = 1; // no-warning
72 }
73
74 if (p) {
75 *p = 5; // no-warning
76 p = 0;
77 }
Steve Naroff2c0ccd02009-04-30 16:01:26 +000078 else return; // expected-warning {{non-void function 'f4_b' should return a value}}
Ted Kremeneke1c2a672009-01-13 01:04:21 +000079
80 *p += 10; // expected-warning{{Dereference of null pointer}}
Mike Stump339d52a2009-07-21 18:51:31 +000081 return 0;
Ted Kremeneke1c2a672009-01-13 01:04:21 +000082}
83
84
Ted Kremeneka5488462008-04-22 21:39:21 +000085int f5() {
86
87 char *s = "hello world";
88 return s[0]; // no-warning
89}
90
Ted Kremenek7fb43c12008-09-01 19:57:52 +000091int bar(int* p, int q) __attribute__((nonnull));
Ted Kremenek584def72008-07-22 00:46:16 +000092
93int f6(int *p) {
Ted Kremenek7fb43c12008-09-01 19:57:52 +000094 return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
95 : bar(p, 0); // no-warning
96}
Ted Kremenek584def72008-07-22 00:46:16 +000097
Ted Kremeneka96ac062008-12-04 18:35:53 +000098int bar2(int* p, int q) __attribute__((nonnull(1)));
99
100int f6b(int *p) {
101 return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
102 : bar2(p, 0); // no-warning
103}
104
Ted Kremenek1e100112008-12-04 19:39:12 +0000105int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3)));
Ted Kremeneka96ac062008-12-04 18:35:53 +0000106
Ted Kremenek1e100112008-12-04 19:39:12 +0000107int f6c(int *p, int *q) {
Ted Kremeneka317e902008-12-04 19:44:23 +0000108 return !p ? bar3(q, 2, p) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
109 : bar3(p, 2, q); // no-warning
Ted Kremenek1e100112008-12-04 19:39:12 +0000110}
Ted Kremeneka96ac062008-12-04 18:35:53 +0000111
Mike Stumpf0549e22009-07-22 22:55:09 +0000112void f6d(int *p) {
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000113 bar(p, 0);
114 // At this point, 'p' cannot be null.
115 if (!p) {
116 int *q = 0;
117 *q = 0xDEADBEEF; // no-warning
118 }
119}
120
Jordy Rose9a126852010-06-21 20:08:28 +0000121void f6e(int *p, int offset) {
122 // PR7406 - crash from treating an UnknownVal as defined, to see if it's 0.
123 bar((p+offset)+1, 0); // not crash
124}
125
Ted Kremenek22bda882008-07-31 20:31:27 +0000126int* qux();
127
128int f7(int x) {
129
130 int* p = 0;
131
132 if (0 == x)
133 p = qux();
134
135 if (0 == x)
136 *p = 1; // no-warning
137
138 return x;
139}
140
Ted Kremenek935022a2009-05-02 00:41:02 +0000141int* f7b(int *x) {
142
143 int* p = 0;
144
145 if (((void*)0) == x)
146 p = qux();
147
148 if (((void*)0) == x)
149 *p = 1; // no-warning
150
151 return x;
152}
153
Ted Kremenek1308f572009-05-04 17:27:32 +0000154int* f7c(int *x) {
155
156 int* p = 0;
157
158 if (((void*)0) == x)
159 p = qux();
160
161 if (((void*)0) != x)
162 return x;
Ted Kremenek65d80fd2009-05-04 17:53:11 +0000163
164 // If we reach here then 'p' is not null.
165 *p = 1; // no-warning
Ted Kremenek1308f572009-05-04 17:27:32 +0000166 return x;
167}
168
169int* f7c2(int *x) {
170
171 int* p = 0;
172
173 if (((void*)0) == x)
174 p = qux();
175
176 if (((void*)0) == x)
177 return x;
178
179 *p = 1; // expected-warning{{null}}
180 return x;
181}
182
Ted Kremenek935022a2009-05-02 00:41:02 +0000183
Mike Stump339d52a2009-07-21 18:51:31 +0000184void f8(int *p, int *q) {
Ted Kremenekdd463b82008-08-16 00:45:40 +0000185 if (!p)
186 if (p)
187 *p = 1; // no-warning
188
189 if (q)
190 if (!q)
191 *q = 1; // no-warning
192}
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000193
194int* qux();
195
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000196int f9(unsigned len) {
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000197 assert (len != 0);
198 int *p = 0;
Ted Kremenekcafd9082008-09-24 06:40:03 +0000199 unsigned i;
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000200
Ted Kremenekcafd9082008-09-24 06:40:03 +0000201 for (i = 0; i < len; ++i)
Ted Kremeneke2b00832008-09-16 23:25:28 +0000202 p = qux(i);
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000203
204 return *p++; // no-warning
205}
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000206
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000207int f9b(unsigned len) {
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000208 assert (len > 0); // note use of '>'
209 int *p = 0;
Ted Kremenekcafd9082008-09-24 06:40:03 +0000210 unsigned i;
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000211
Ted Kremenekcafd9082008-09-24 06:40:03 +0000212 for (i = 0; i < len; ++i)
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000213 p = qux(i);
214
215 return *p++; // no-warning
216}
217
Ted Kremenek973e72a2008-11-15 04:44:13 +0000218int* f10(int* p, signed char x, int y) {
219 // This line tests symbolication with compound assignments where the
220 // LHS and RHS have different bitwidths. The new symbolic value
221 // for 'x' should have a bitwidth of 8.
222 x &= y;
223
224 // This tests that our symbolication worked, and that we correctly test
225 // x against 0 (with the same bitwidth).
226 if (!x) {
Steve Naroff2c0ccd02009-04-30 16:01:26 +0000227 if (!p) return; // expected-warning {{non-void function 'f10' should return a value}}
Ted Kremenek973e72a2008-11-15 04:44:13 +0000228 *p = 10;
229 }
230 else p = 0;
231
232 if (!x)
233 *p = 5; // no-warning
234
235 return p;
236}
237
Ted Kremenek73abd132008-12-03 18:56:12 +0000238// Test case from <rdar://problem/6407949>
239void f11(unsigned i) {
240 int *x = 0;
241 if (i >= 0) {
242 // always true
243 } else {
244 *x = 42; // no-warning
245 }
246}
247
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000248void f11b(unsigned i) {
249 int *x = 0;
250 if (i <= ~(unsigned)0) {
251 // always true
252 } else {
253 *x = 42; // no-warning
254 }
255}
256
Ted Kremenek72afb372009-01-17 01:54:16 +0000257// Test case for switch statements with weird case arms.
258typedef int BOOL, *PBOOL, *LPBOOL;
259typedef long LONG_PTR, *PLONG_PTR;
260typedef unsigned long ULONG_PTR, *PULONG_PTR;
261typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR;
262typedef LONG_PTR LRESULT;
263typedef struct _F12ITEM *HF12ITEM;
264
265void f12(HF12ITEM i, char *q) {
266 char *p = 0;
267 switch ((DWORD_PTR) i) {
268 case 0 ... 10:
269 p = q;
270 break;
271 case (DWORD_PTR) ((HF12ITEM) - 65535):
272 return;
273 default:
274 return;
275 }
276
277 *p = 1; // no-warning
278}
279
Ted Kremenekefcfcc02009-03-05 02:42:32 +0000280// Test handling of translating between integer "pointers" and back.
281void f13() {
282 int *x = 0;
283 if (((((int) x) << 2) + 1) >> 1) *x = 1; // no-warning
284}
285
Ted Kremenekac502132009-08-24 22:56:32 +0000286// PR 4759 - Attribute non-null checking by the analyzer was not correctly
287// handling pointer values that were undefined.
288void pr4759_aux(int *p) __attribute__((nonnull));
289
290void pr4759() {
291 int *p;
292 pr4759_aux(p); // expected-warning{{undefined}}
293}
294
Ted Kremenekefcfcc02009-03-05 02:42:32 +0000295