blob: 7f1db8749c13f8ba003d6b034996ead7da555b73 [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -analyze -std=gnu99 -checker-simple -verify %s &&
2// RUN: clang-cc -analyze -std=gnu99 -checker-simple -verify %s -analyzer-constraints=range &&
3// RUN: clang-cc -analyze -std=gnu99 -checker-simple -analyzer-store=region -analyzer-purge-dead=false -verify %s &&
4// RUN: clang-cc -analyze -std=gnu99 -checker-cfref -analyzer-store=region -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}}
75}
76
77
Ted Kremeneka5488462008-04-22 21:39:21 +000078int f5() {
79
80 char *s = "hello world";
81 return s[0]; // no-warning
82}
83
Ted Kremenek7fb43c12008-09-01 19:57:52 +000084int bar(int* p, int q) __attribute__((nonnull));
Ted Kremenek584def72008-07-22 00:46:16 +000085
86int f6(int *p) {
Ted Kremenek7fb43c12008-09-01 19:57:52 +000087 return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
88 : bar(p, 0); // no-warning
89}
Ted Kremenek584def72008-07-22 00:46:16 +000090
Ted Kremeneka96ac062008-12-04 18:35:53 +000091int bar2(int* p, int q) __attribute__((nonnull(1)));
92
93int f6b(int *p) {
94 return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
95 : bar2(p, 0); // no-warning
96}
97
Ted Kremenek1e100112008-12-04 19:39:12 +000098int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3)));
Ted Kremeneka96ac062008-12-04 18:35:53 +000099
Ted Kremenek1e100112008-12-04 19:39:12 +0000100int f6c(int *p, int *q) {
Ted Kremeneka317e902008-12-04 19:44:23 +0000101 return !p ? bar3(q, 2, p) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
102 : bar3(p, 2, q); // no-warning
Ted Kremenek1e100112008-12-04 19:39:12 +0000103}
Ted Kremeneka96ac062008-12-04 18:35:53 +0000104
Ted Kremenek22bda882008-07-31 20:31:27 +0000105int* qux();
106
107int f7(int x) {
108
109 int* p = 0;
110
111 if (0 == x)
112 p = qux();
113
114 if (0 == x)
115 *p = 1; // no-warning
116
117 return x;
118}
119
Ted Kremenek935022a2009-05-02 00:41:02 +0000120int* f7b(int *x) {
121
122 int* p = 0;
123
124 if (((void*)0) == x)
125 p = qux();
126
127 if (((void*)0) == x)
128 *p = 1; // no-warning
129
130 return x;
131}
132
Ted Kremenek1308f572009-05-04 17:27:32 +0000133int* f7c(int *x) {
134
135 int* p = 0;
136
137 if (((void*)0) == x)
138 p = qux();
139
140 if (((void*)0) != x)
141 return x;
142
143 // THIS IS WRONG. THIS NEEDS TO BE FIXED.
144 *p = 1; // expected-warning{{null}}
145 return x;
146}
147
148int* f7c2(int *x) {
149
150 int* p = 0;
151
152 if (((void*)0) == x)
153 p = qux();
154
155 if (((void*)0) == x)
156 return x;
157
158 *p = 1; // expected-warning{{null}}
159 return x;
160}
161
Ted Kremenek935022a2009-05-02 00:41:02 +0000162
Ted Kremenekdd463b82008-08-16 00:45:40 +0000163int f8(int *p, int *q) {
164 if (!p)
165 if (p)
166 *p = 1; // no-warning
167
168 if (q)
169 if (!q)
170 *q = 1; // no-warning
171}
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000172
173int* qux();
174
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000175int f9(unsigned len) {
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000176 assert (len != 0);
177 int *p = 0;
Ted Kremenekcafd9082008-09-24 06:40:03 +0000178 unsigned i;
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000179
Ted Kremenekcafd9082008-09-24 06:40:03 +0000180 for (i = 0; i < len; ++i)
Ted Kremeneke2b00832008-09-16 23:25:28 +0000181 p = qux(i);
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000182
183 return *p++; // no-warning
184}
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000185
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000186int f9b(unsigned len) {
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000187 assert (len > 0); // note use of '>'
188 int *p = 0;
Ted Kremenekcafd9082008-09-24 06:40:03 +0000189 unsigned i;
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000190
Ted Kremenekcafd9082008-09-24 06:40:03 +0000191 for (i = 0; i < len; ++i)
Ted Kremenekf6e5ec42008-09-17 22:24:13 +0000192 p = qux(i);
193
194 return *p++; // no-warning
195}
196
Ted Kremenek973e72a2008-11-15 04:44:13 +0000197int* f10(int* p, signed char x, int y) {
198 // This line tests symbolication with compound assignments where the
199 // LHS and RHS have different bitwidths. The new symbolic value
200 // for 'x' should have a bitwidth of 8.
201 x &= y;
202
203 // This tests that our symbolication worked, and that we correctly test
204 // x against 0 (with the same bitwidth).
205 if (!x) {
Steve Naroff2c0ccd02009-04-30 16:01:26 +0000206 if (!p) return; // expected-warning {{non-void function 'f10' should return a value}}
Ted Kremenek973e72a2008-11-15 04:44:13 +0000207 *p = 10;
208 }
209 else p = 0;
210
211 if (!x)
212 *p = 5; // no-warning
213
214 return p;
215}
216
Ted Kremenek73abd132008-12-03 18:56:12 +0000217// Test case from <rdar://problem/6407949>
218void f11(unsigned i) {
219 int *x = 0;
220 if (i >= 0) {
221 // always true
222 } else {
223 *x = 42; // no-warning
224 }
225}
226
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000227void f11b(unsigned i) {
228 int *x = 0;
229 if (i <= ~(unsigned)0) {
230 // always true
231 } else {
232 *x = 42; // no-warning
233 }
234}
235
Ted Kremenek72afb372009-01-17 01:54:16 +0000236// Test case for switch statements with weird case arms.
237typedef int BOOL, *PBOOL, *LPBOOL;
238typedef long LONG_PTR, *PLONG_PTR;
239typedef unsigned long ULONG_PTR, *PULONG_PTR;
240typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR;
241typedef LONG_PTR LRESULT;
242typedef struct _F12ITEM *HF12ITEM;
243
244void f12(HF12ITEM i, char *q) {
245 char *p = 0;
246 switch ((DWORD_PTR) i) {
247 case 0 ... 10:
248 p = q;
249 break;
250 case (DWORD_PTR) ((HF12ITEM) - 65535):
251 return;
252 default:
253 return;
254 }
255
256 *p = 1; // no-warning
257}
258
Ted Kremenekefcfcc02009-03-05 02:42:32 +0000259// Test handling of translating between integer "pointers" and back.
260void f13() {
261 int *x = 0;
262 if (((((int) x) << 2) + 1) >> 1) *x = 1; // no-warning
263}
264
265