blob: f3591a3bbde90a5e2795a0bfa46f54ff96186e36 [file] [log] [blame]
Ted Kremenek6c07bdb2009-06-26 00:05:51 +00001// RUN: clang-cc -analyze -std=gnu99 -checker-cfref -verify %s -analyzer-constraints=basic -analyzer-store=basic &&
2// RUN: clang-cc -analyze -std=gnu99 -checker-cfref -verify %s -analyzer-constraints=range -analyzer-store=basic &&
3// RUN: clang-cc -analyze -std=gnu99 -checker-cfref -analyzer-store=region -analyzer-constraints=range -analyzer-purge-dead=false -verify %s &&
4// RUN: clang-cc -analyze -std=gnu99 -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s
Ted Kremenek2f54af42008-04-02 16:54:39 +00005
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00006#include<stdint.h>
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +00007#include <assert.h>
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00008
Ted Kremenek2f54af42008-04-02 16:54:39 +00009void f1(int *p) {
10 if (p) *p = 1;
11 else *p = 0; // expected-warning{{ereference}}
12}
Ted Kremenekb9ab6902008-04-21 23:44:17 +000013
14struct foo_struct {
15 int x;
16};
17
18int f2(struct foo_struct* p) {
19
20 if (p)
21 p->x = 1;
22
Ted Kremenek3603d732008-04-21 23:45:26 +000023 return p->x++; // expected-warning{{Dereference of null pointer.}}
Ted Kremenekb9ab6902008-04-21 23:44:17 +000024}
Ted Kremenek9704eac2008-04-22 04:56:55 +000025
26int f3(char* x) {
27
28 int i = 2;
29
30 if (x)
31 return x[i - 1];
32
33 return x[i+1]; // expected-warning{{Dereference of null pointer.}}
34}
35
Ted Kremeneke2013f52008-04-29 23:25:09 +000036int f3_b(char* x) {
37
38 int i = 2;
39
40 if (x)
41 return x[i - 1];
42
43 return x[i+1]++; // expected-warning{{Dereference of null pointer.}}
44}
45
Ted Kremenek0fe33bc2008-04-22 21:10:18 +000046int f4(int *p) {
47
Daniel Dunbar4489fe12008-08-05 00:07:51 +000048 uintptr_t x = (uintptr_t) p;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +000049
50 if (x)
51 return 1;
52
53 int *q = (int*) x;
54 return *q; // expected-warning{{Dereference of null pointer.}}
Ted Kremeneka5488462008-04-22 21:39:21 +000055}
56
Ted Kremeneke1c2a672009-01-13 01:04:21 +000057int f4_b() {
58 short array[2];
59 uintptr_t x = array; // expected-warning{{incompatible pointer to integer conversion initializing}}
60 short *p = x; // expected-warning{{incompatible integer to pointer conversion initializing}}
61
62 // The following branch should be infeasible.
63 if (!(p = &array[0])) {
64 p = 0;
65 *p = 1; // no-warning
66 }
67
68 if (p) {
69 *p = 5; // no-warning
70 p = 0;
71 }
Steve Naroff2c0ccd02009-04-30 16:01:26 +000072 else return; // expected-warning {{non-void function 'f4_b' should return a value}}
Ted Kremeneke1c2a672009-01-13 01:04:21 +000073
74 *p += 10; // expected-warning{{Dereference of null pointer}}
Mike Stump339d52a2009-07-21 18:51:31 +000075 return 0;
Ted Kremeneke1c2a672009-01-13 01:04:21 +000076}
77
78
Ted Kremeneka5488462008-04-22 21:39:21 +000079int f5() {
80
81 char *s = "hello world";
82 return s[0]; // no-warning
83}
84
Ted Kremenek7fb43c12008-09-01 19:57:52 +000085int bar(int* p, int q) __attribute__((nonnull));
Ted Kremenek584def72008-07-22 00:46:16 +000086
87int f6(int *p) {
Ted Kremenek7fb43c12008-09-01 19:57:52 +000088 return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
89 : bar(p, 0); // no-warning
90}
Ted Kremenek584def72008-07-22 00:46:16 +000091
Ted Kremeneka96ac062008-12-04 18:35:53 +000092int bar2(int* p, int q) __attribute__((nonnull(1)));
93
94int f6b(int *p) {
95 return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
96 : bar2(p, 0); // no-warning
97}
98
Ted Kremenek1e100112008-12-04 19:39:12 +000099int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3)));
Ted Kremeneka96ac062008-12-04 18:35:53 +0000100
Ted Kremenek1e100112008-12-04 19:39:12 +0000101int f6c(int *p, int *q) {
Ted Kremeneka317e902008-12-04 19:44:23 +0000102 return !p ? bar3(q, 2, p) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
103 : bar3(p, 2, q); // no-warning
Ted Kremenek1e100112008-12-04 19:39:12 +0000104}
Ted Kremeneka96ac062008-12-04 18:35:53 +0000105
Mike Stumpf0549e22009-07-22 22:55:09 +0000106void f6d(int *p) {
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000107 bar(p, 0);
108 // At this point, 'p' cannot be null.
109 if (!p) {
110 int *q = 0;
111 *q = 0xDEADBEEF; // no-warning
112 }
113}
114
Ted Kremenek22bda882008-07-31 20:31:27 +0000115int* qux();
116
117int f7(int x) {
118
119 int* p = 0;
120
121 if (0 == x)
122 p = qux();
123
124 if (0 == x)
125 *p = 1; // no-warning
126
127 return x;
128}
129
Ted Kremenek935022a2009-05-02 00:41:02 +0000130int* f7b(int *x) {
131
132 int* p = 0;
133
134 if (((void*)0) == x)
135 p = qux();
136
137 if (((void*)0) == x)
138 *p = 1; // no-warning
139
140 return x;
141}
142
Ted Kremenek1308f572009-05-04 17:27:32 +0000143int* f7c(int *x) {
144
145 int* p = 0;
146
147 if (((void*)0) == x)
148 p = qux();
149
150 if (((void*)0) != x)
151 return x;
Ted Kremenek65d80fd2009-05-04 17:53:11 +0000152
153 // If we reach here then 'p' is not null.
154 *p = 1; // no-warning
Ted Kremenek1308f572009-05-04 17:27:32 +0000155 return x;
156}
157
158int* f7c2(int *x) {
159
160 int* p = 0;
161
162 if (((void*)0) == x)
163 p = qux();
164
165 if (((void*)0) == x)
166 return x;
167
168 *p = 1; // expected-warning{{null}}
169 return x;
170}
171
Ted Kremenek935022a2009-05-02 00:41:02 +0000172
Mike Stump339d52a2009-07-21 18:51:31 +0000173void f8(int *p, int *q) {
Ted Kremenekdd463b82008-08-16 00:45:40 +0000174 if (!p)
175 if (p)
176 *p = 1; // no-warning
177
178 if (q)
179 if (!q)
180 *q = 1; // no-warning
181}
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000182
183int* qux();
184
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000185int f9(unsigned len) {
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000186 assert (len != 0);
187 int *p = 0;
Ted Kremenekcafd9082008-09-24 06:40:03 +0000188 unsigned i;
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000189
Ted Kremenekcafd9082008-09-24 06:40:03 +0000190 for (i = 0; i < len; ++i)
Ted Kremeneke2b00832008-09-16 23:25:28 +0000191 p = qux(i);
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000192
193 return *p++; // no-warning
194}
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000195
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000196int f9b(unsigned len) {
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000197 assert (len > 0); // note use of '>'
198 int *p = 0;
Ted Kremenekcafd9082008-09-24 06:40:03 +0000199 unsigned i;
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000200
Ted Kremenekcafd9082008-09-24 06:40:03 +0000201 for (i = 0; i < len; ++i)
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000202 p = qux(i);
203
204 return *p++; // no-warning
205}
206
Ted Kremenek973e72a2008-11-15 04:44:13 +0000207int* f10(int* p, signed char x, int y) {
208 // This line tests symbolication with compound assignments where the
209 // LHS and RHS have different bitwidths. The new symbolic value
210 // for 'x' should have a bitwidth of 8.
211 x &= y;
212
213 // This tests that our symbolication worked, and that we correctly test
214 // x against 0 (with the same bitwidth).
215 if (!x) {
Steve Naroff2c0ccd02009-04-30 16:01:26 +0000216 if (!p) return; // expected-warning {{non-void function 'f10' should return a value}}
Ted Kremenek973e72a2008-11-15 04:44:13 +0000217 *p = 10;
218 }
219 else p = 0;
220
221 if (!x)
222 *p = 5; // no-warning
223
224 return p;
225}
226
Ted Kremenek73abd132008-12-03 18:56:12 +0000227// Test case from <rdar://problem/6407949>
228void f11(unsigned i) {
229 int *x = 0;
230 if (i >= 0) {
231 // always true
232 } else {
233 *x = 42; // no-warning
234 }
235}
236
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000237void f11b(unsigned i) {
238 int *x = 0;
239 if (i <= ~(unsigned)0) {
240 // always true
241 } else {
242 *x = 42; // no-warning
243 }
244}
245
Ted Kremenek72afb372009-01-17 01:54:16 +0000246// Test case for switch statements with weird case arms.
247typedef int BOOL, *PBOOL, *LPBOOL;
248typedef long LONG_PTR, *PLONG_PTR;
249typedef unsigned long ULONG_PTR, *PULONG_PTR;
250typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR;
251typedef LONG_PTR LRESULT;
252typedef struct _F12ITEM *HF12ITEM;
253
254void f12(HF12ITEM i, char *q) {
255 char *p = 0;
256 switch ((DWORD_PTR) i) {
257 case 0 ... 10:
258 p = q;
259 break;
260 case (DWORD_PTR) ((HF12ITEM) - 65535):
261 return;
262 default:
263 return;
264 }
265
266 *p = 1; // no-warning
267}
268
Ted Kremenekefcfcc02009-03-05 02:42:32 +0000269// Test handling of translating between integer "pointers" and back.
270void f13() {
271 int *x = 0;
272 if (((((int) x) << 2) + 1) >> 1) *x = 1; // no-warning
273}
274
Ted Kremenekac502132009-08-24 22:56:32 +0000275// PR 4759 - Attribute non-null checking by the analyzer was not correctly
276// handling pointer values that were undefined.
277void pr4759_aux(int *p) __attribute__((nonnull));
278
279void pr4759() {
280 int *p;
281 pr4759_aux(p); // expected-warning{{undefined}}
282}
283
Ted Kremenekefcfcc02009-03-05 02:42:32 +0000284