blob: f3591a3bbde90a5e2795a0bfa46f54ff96186e36 [file] [log] [blame]
Ted Kremenek1642bda2009-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 Kremenek018ba602008-04-02 16:54:39 +00005
Ted Kremenekeccf3e52008-04-22 21:10:18 +00006#include<stdint.h>
Ted Kremenek87827162008-09-16 23:24:45 +00007#include <assert.h>
Ted Kremenekeccf3e52008-04-22 21:10:18 +00008
Ted Kremenek018ba602008-04-02 16:54:39 +00009void f1(int *p) {
10 if (p) *p = 1;
11 else *p = 0; // expected-warning{{ereference}}
12}
Ted Kremenek503924b2008-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 Kremenekefa92f12008-04-21 23:45:26 +000023 return p->x++; // expected-warning{{Dereference of null pointer.}}
Ted Kremenek503924b2008-04-21 23:44:17 +000024}
Ted Kremenek6fdd3b32008-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 Kremenek10bad412008-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 Kremenekeccf3e52008-04-22 21:10:18 +000046int f4(int *p) {
47
Daniel Dunbar81f7f292008-08-05 00:07:51 +000048 uintptr_t x = (uintptr_t) p;
Ted Kremenekeccf3e52008-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 Kremenekc79c0592008-04-22 21:39:21 +000055}
56
Ted Kremenekeb68db22009-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 Naroff62e0cb02009-04-30 16:01:26 +000072 else return; // expected-warning {{non-void function 'f4_b' should return a value}}
Ted Kremenekeb68db22009-01-13 01:04:21 +000073
74 *p += 10; // expected-warning{{Dereference of null pointer}}
Mike Stump9832fcb2009-07-21 18:51:31 +000075 return 0;
Ted Kremenekeb68db22009-01-13 01:04:21 +000076}
77
78
Ted Kremenekc79c0592008-04-22 21:39:21 +000079int f5() {
80
81 char *s = "hello world";
82 return s[0]; // no-warning
83}
84
Ted Kremenekc4f6d902008-09-01 19:57:52 +000085int bar(int* p, int q) __attribute__((nonnull));
Ted Kremenek98f6e582008-07-22 00:46:16 +000086
87int f6(int *p) {
Ted Kremenekc4f6d902008-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 Kremenek98f6e582008-07-22 00:46:16 +000091
Ted Kremenek3f0e5c82008-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 Kremenekd58e7412008-12-04 19:39:12 +000099int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3)));
Ted Kremenek3f0e5c82008-12-04 18:35:53 +0000100
Ted Kremenekd58e7412008-12-04 19:39:12 +0000101int f6c(int *p, int *q) {
Ted Kremenek561370c2008-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 Kremenekd58e7412008-12-04 19:39:12 +0000104}
Ted Kremenek3f0e5c82008-12-04 18:35:53 +0000105
Mike Stump24cbfc32009-07-22 22:55:09 +0000106void f6d(int *p) {
Ted Kremenek915c3512009-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 Kremeneka7045d62008-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 Kremenek2bfed982009-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 Kremenek0b0ee3c2009-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 Kremenek250d59f2009-05-04 17:53:11 +0000152
153 // If we reach here then 'p' is not null.
154 *p = 1; // no-warning
Ted Kremenek0b0ee3c2009-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 Kremenek2bfed982009-05-02 00:41:02 +0000172
Mike Stump9832fcb2009-07-21 18:51:31 +0000173void f8(int *p, int *q) {
Ted Kremenek3b977552008-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 Kremenek87827162008-09-16 23:24:45 +0000182
183int* qux();
184
Ted Kremenek34bfd8a2008-09-19 18:00:36 +0000185int f9(unsigned len) {
Ted Kremenek87827162008-09-16 23:24:45 +0000186 assert (len != 0);
187 int *p = 0;
Ted Kremenek1f58ec62008-09-24 06:40:03 +0000188 unsigned i;
Ted Kremenek87827162008-09-16 23:24:45 +0000189
Ted Kremenek1f58ec62008-09-24 06:40:03 +0000190 for (i = 0; i < len; ++i)
Ted Kremenek025f8352008-09-16 23:25:28 +0000191 p = qux(i);
Ted Kremenek87827162008-09-16 23:24:45 +0000192
193 return *p++; // no-warning
194}
Ted Kremenek24bef312008-09-17 22:24:13 +0000195
Ted Kremenek34bfd8a2008-09-19 18:00:36 +0000196int f9b(unsigned len) {
Ted Kremenek24bef312008-09-17 22:24:13 +0000197 assert (len > 0); // note use of '>'
198 int *p = 0;
Ted Kremenek1f58ec62008-09-24 06:40:03 +0000199 unsigned i;
Ted Kremenek24bef312008-09-17 22:24:13 +0000200
Ted Kremenek1f58ec62008-09-24 06:40:03 +0000201 for (i = 0; i < len; ++i)
Ted Kremenek24bef312008-09-17 22:24:13 +0000202 p = qux(i);
203
204 return *p++; // no-warning
205}
206
Ted Kremenek3ebd7de2008-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 Naroff62e0cb02009-04-30 16:01:26 +0000216 if (!p) return; // expected-warning {{non-void function 'f10' should return a value}}
Ted Kremenek3ebd7de2008-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 Kremenekf935cfe2008-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 Kremenekfff9f4a2008-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 Kremenek1ab188f2009-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 Kremenekeba836a42009-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 Kremenek6bc04bc2009-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 Kremenekeba836a42009-03-05 02:42:32 +0000284