blob: d74be0ffce5e631271d67760970f93d507e28f83 [file] [log] [blame]
Argyrios Kyrtzidis8a285ae2011-04-26 17:41:22 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,cplusplus.experimental.CString -analyzer-store=region -Wno-null-dereference -verify %s
2// RUN: %clang_cc1 -analyze -DUSE_BUILTINS -analyzer-checker=core,cplusplus.experimental.CString -analyzer-store=region -Wno-null-dereference -verify %s
3// RUN: %clang_cc1 -analyze -DVARIANT -analyzer-checker=core,cplusplus.experimental.CString -analyzer-store=region -Wno-null-dereference -verify %s
4// RUN: %clang_cc1 -analyze -DUSE_BUILTINS -DVARIANT -analyzer-checker=core,cplusplus.experimental.CString -analyzer-store=region -Wno-null-dereference -verify %s
Jordy Roseccbf7ee2010-07-06 23:11:01 +00005
6//===----------------------------------------------------------------------===
7// Declarations
8//===----------------------------------------------------------------------===
9
Jordy Rosea6b808c2010-07-07 07:48:06 +000010// Some functions are so similar to each other that they follow the same code
Jordy Rosebc56d1f2010-07-07 08:15:01 +000011// path, such as memcpy and __memcpy_chk, or memcmp and bcmp. If VARIANT is
12// defined, make sure to use the variants instead to make sure they are still
13// checked by the analyzer.
Jordy Roseccbf7ee2010-07-06 23:11:01 +000014
15// Some functions are implemented as builtins. These should be #defined as
16// BUILTIN(f), which will prepend "__builtin_" if USE_BUILTINS is defined.
17
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000018// Functions that have variants and are also available as builtins should be
Jordy Rosea6b808c2010-07-07 07:48:06 +000019// declared carefully! See memcpy() for an example.
Jordy Roseccbf7ee2010-07-06 23:11:01 +000020
21#ifdef USE_BUILTINS
22# define BUILTIN(f) __builtin_ ## f
23#else /* USE_BUILTINS */
24# define BUILTIN(f) f
25#endif /* USE_BUILTINS */
26
27typedef typeof(sizeof(int)) size_t;
28
29//===----------------------------------------------------------------------===
30// memcpy()
31//===----------------------------------------------------------------------===
32
Jordy Rosea6b808c2010-07-07 07:48:06 +000033#ifdef VARIANT
Jordy Roseccbf7ee2010-07-06 23:11:01 +000034
35#define __memcpy_chk BUILTIN(__memcpy_chk)
36void *__memcpy_chk(void *restrict s1, const void *restrict s2, size_t n,
37 size_t destlen);
38
39#define memcpy(a,b,c) __memcpy_chk(a,b,c,(size_t)-1)
40
Jordy Rosea6b808c2010-07-07 07:48:06 +000041#else /* VARIANT */
Jordy Roseccbf7ee2010-07-06 23:11:01 +000042
43#define memcpy BUILTIN(memcpy)
44void *memcpy(void *restrict s1, const void *restrict s2, size_t n);
45
Jordy Rosea6b808c2010-07-07 07:48:06 +000046#endif /* VARIANT */
Jordy Roseccbf7ee2010-07-06 23:11:01 +000047
48
49void memcpy0 () {
50 char src[] = {1, 2, 3, 4};
Jordy Rosee64f3112010-08-16 07:51:42 +000051 char dst[4] = {0};
Jordy Roseccbf7ee2010-07-06 23:11:01 +000052
53 memcpy(dst, src, 4); // no-warning
54
55 if (memcpy(dst, src, 4) != dst) {
Tom Care7bce3a12010-07-27 23:30:21 +000056 (void)*(char*)0; // no-warning
Jordy Roseccbf7ee2010-07-06 23:11:01 +000057 }
Jordy Rosee64f3112010-08-16 07:51:42 +000058
59 if (dst[0] != 0)
60 (void)*(char*)0; // expected-warning{{null}}
Jordy Roseccbf7ee2010-07-06 23:11:01 +000061}
62
63void memcpy1 () {
64 char src[] = {1, 2, 3, 4};
65 char dst[10];
66
Jordy Rosee64f3112010-08-16 07:51:42 +000067 memcpy(dst, src, 5); // expected-warning{{Byte string function accesses out-of-bound array element}}
Jordy Roseccbf7ee2010-07-06 23:11:01 +000068}
69
70void memcpy2 () {
71 char src[] = {1, 2, 3, 4};
72 char dst[1];
73
Jordy Rosee64f3112010-08-16 07:51:42 +000074 memcpy(dst, src, 4); // expected-warning{{Byte string function overflows destination buffer}}
Jordy Roseccbf7ee2010-07-06 23:11:01 +000075}
76
77void memcpy3 () {
78 char src[] = {1, 2, 3, 4};
79 char dst[3];
80
81 memcpy(dst+1, src+2, 2); // no-warning
82}
83
84void memcpy4 () {
85 char src[] = {1, 2, 3, 4};
86 char dst[10];
87
Jordy Rosee64f3112010-08-16 07:51:42 +000088 memcpy(dst+2, src+2, 3); // expected-warning{{Byte string function accesses out-of-bound array element}}
Jordy Roseccbf7ee2010-07-06 23:11:01 +000089}
90
91void memcpy5() {
92 char src[] = {1, 2, 3, 4};
93 char dst[3];
94
Jordy Rosee64f3112010-08-16 07:51:42 +000095 memcpy(dst+2, src+2, 2); // expected-warning{{Byte string function overflows destination buffer}}
Jordy Roseccbf7ee2010-07-06 23:11:01 +000096}
97
98void memcpy6() {
99 int a[4] = {0};
100 memcpy(a, a, 8); // expected-warning{{overlapping}}
101}
102
103void memcpy7() {
104 int a[4] = {0};
105 memcpy(a+2, a+1, 8); // expected-warning{{overlapping}}
106}
107
108void memcpy8() {
109 int a[4] = {0};
110 memcpy(a+1, a+2, 8); // expected-warning{{overlapping}}
111}
112
113void memcpy9() {
114 int a[4] = {0};
115 memcpy(a+2, a+1, 4); // no-warning
116 memcpy(a+1, a+2, 4); // no-warning
117}
118
Jordy Rosea6b808c2010-07-07 07:48:06 +0000119void memcpy10() {
120 char a[4] = {0};
121 memcpy(0, a, 4); // expected-warning{{Null pointer argument in call to byte string function}}
122}
123
124void memcpy11() {
125 char a[4] = {0};
126 memcpy(a, 0, 4); // expected-warning{{Null pointer argument in call to byte string function}}
127}
128
129void memcpy12() {
130 char a[4] = {0};
131 memcpy(0, a, 0); // no-warning
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000132}
133
134void memcpy13() {
135 char a[4] = {0};
Jordy Rosea6b808c2010-07-07 07:48:06 +0000136 memcpy(a, 0, 0); // no-warning
137}
138
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000139//===----------------------------------------------------------------------===
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000140// mempcpy()
141//===----------------------------------------------------------------------===
142
Jordy Rosebe460d82011-06-03 23:42:56 +0000143#ifdef VARIANT
144
145#define __mempcpy_chk BUILTIN(__mempcpy_chk)
146void *__mempcpy_chk(void *restrict s1, const void *restrict s2, size_t n,
147 size_t destlen);
148
149#define mempcpy(a,b,c) __mempcpy_chk(a,b,c,(size_t)-1)
150
151#else /* VARIANT */
152
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000153#define mempcpy BUILTIN(mempcpy)
154void *mempcpy(void *restrict s1, const void *restrict s2, size_t n);
155
Jordy Rosebe460d82011-06-03 23:42:56 +0000156#endif /* VARIANT */
157
158
Lenny Maioranib8b875b2011-03-31 21:36:53 +0000159void mempcpy0 () {
160 char src[] = {1, 2, 3, 4};
161 char dst[5] = {0};
162
163 mempcpy(dst, src, 4); // no-warning
164
165 if (mempcpy(dst, src, 4) != &dst[4]) {
166 (void)*(char*)0; // no-warning
167 }
168
169 if (dst[0] != 0)
170 (void)*(char*)0; // expected-warning{{null}}
171}
172
173void mempcpy1 () {
174 char src[] = {1, 2, 3, 4};
175 char dst[10];
176
177 mempcpy(dst, src, 5); // expected-warning{{Byte string function accesses out-of-bound array element}}
178}
179
180void mempcpy2 () {
181 char src[] = {1, 2, 3, 4};
182 char dst[1];
183
184 mempcpy(dst, src, 4); // expected-warning{{Byte string function overflows destination buffer}}
185}
186
187void mempcpy3 () {
188 char src[] = {1, 2, 3, 4};
189 char dst[3];
190
191 mempcpy(dst+1, src+2, 2); // no-warning
192}
193
194void mempcpy4 () {
195 char src[] = {1, 2, 3, 4};
196 char dst[10];
197
198 mempcpy(dst+2, src+2, 3); // expected-warning{{Byte string function accesses out-of-bound array element}}
199}
200
201void mempcpy5() {
202 char src[] = {1, 2, 3, 4};
203 char dst[3];
204
205 mempcpy(dst+2, src+2, 2); // expected-warning{{Byte string function overflows destination buffer}}
206}
207
208void mempcpy6() {
209 int a[4] = {0};
210 mempcpy(a, a, 8); // expected-warning{{overlapping}}
211}
212
213void mempcpy7() {
214 int a[4] = {0};
215 mempcpy(a+2, a+1, 8); // expected-warning{{overlapping}}
216}
217
218void mempcpy8() {
219 int a[4] = {0};
220 mempcpy(a+1, a+2, 8); // expected-warning{{overlapping}}
221}
222
223void mempcpy9() {
224 int a[4] = {0};
225 mempcpy(a+2, a+1, 4); // no-warning
226 mempcpy(a+1, a+2, 4); // no-warning
227}
228
229void mempcpy10() {
230 char a[4] = {0};
231 mempcpy(0, a, 4); // expected-warning{{Null pointer argument in call to byte string function}}
232}
233
234void mempcpy11() {
235 char a[4] = {0};
236 mempcpy(a, 0, 4); // expected-warning{{Null pointer argument in call to byte string function}}
237}
238
239void mempcpy12() {
240 char a[4] = {0};
241 mempcpy(0, a, 0); // no-warning
242}
243
244void mempcpy13() {
245 char a[4] = {0};
246 mempcpy(a, 0, 0); // no-warning
247}
248
249//===----------------------------------------------------------------------===
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000250// memmove()
251//===----------------------------------------------------------------------===
252
Jordy Rosea6b808c2010-07-07 07:48:06 +0000253#ifdef VARIANT
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000254
255#define __memmove_chk BUILTIN(__memmove_chk)
256void *__memmove_chk(void *s1, const void *s2, size_t n, size_t destlen);
257
258#define memmove(a,b,c) __memmove_chk(a,b,c,(size_t)-1)
259
Jordy Rosea6b808c2010-07-07 07:48:06 +0000260#else /* VARIANT */
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000261
262#define memmove BUILTIN(memmove)
263void *memmove(void *s1, const void *s2, size_t n);
264
Jordy Rosea6b808c2010-07-07 07:48:06 +0000265#endif /* VARIANT */
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000266
267
268void memmove0 () {
269 char src[] = {1, 2, 3, 4};
Jordy Rosee64f3112010-08-16 07:51:42 +0000270 char dst[4] = {0};
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000271
272 memmove(dst, src, 4); // no-warning
273
274 if (memmove(dst, src, 4) != dst) {
Tom Care7bce3a12010-07-27 23:30:21 +0000275 (void)*(char*)0; // no-warning
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000276 }
Jordy Rosee64f3112010-08-16 07:51:42 +0000277
278 if (dst[0] != 0)
279 (void)*(char*)0; // expected-warning{{null}}
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000280}
281
282void memmove1 () {
283 char src[] = {1, 2, 3, 4};
284 char dst[10];
285
286 memmove(dst, src, 5); // expected-warning{{out-of-bound}}
287}
288
289void memmove2 () {
290 char src[] = {1, 2, 3, 4};
291 char dst[1];
292
Jordy Rosee64f3112010-08-16 07:51:42 +0000293 memmove(dst, src, 4); // expected-warning{{overflow}}
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000294}
295
296//===----------------------------------------------------------------------===
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000297// memcmp()
298//===----------------------------------------------------------------------===
299
300#ifdef VARIANT
301
302#define bcmp BUILTIN(bcmp)
303// __builtin_bcmp is not defined with const in Builtins.def.
304int bcmp(/*const*/ void *s1, /*const*/ void *s2, size_t n);
305#define memcmp bcmp
306
307#else /* VARIANT */
308
309#define memcmp BUILTIN(memcmp)
310int memcmp(const void *s1, const void *s2, size_t n);
311
312#endif /* VARIANT */
313
314
315void memcmp0 () {
316 char a[] = {1, 2, 3, 4};
317 char b[4] = { 0 };
318
319 memcmp(a, b, 4); // no-warning
320}
321
322void memcmp1 () {
323 char a[] = {1, 2, 3, 4};
324 char b[10] = { 0 };
325
326 memcmp(a, b, 5); // expected-warning{{out-of-bound}}
327}
328
329void memcmp2 () {
330 char a[] = {1, 2, 3, 4};
331 char b[1] = { 0 };
332
333 memcmp(a, b, 4); // expected-warning{{out-of-bound}}
334}
335
336void memcmp3 () {
337 char a[] = {1, 2, 3, 4};
338
339 if (memcmp(a, a, 4))
Tom Care7bce3a12010-07-27 23:30:21 +0000340 (void)*(char*)0; // no-warning
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000341}
342
343void memcmp4 (char *input) {
344 char a[] = {1, 2, 3, 4};
345
346 if (memcmp(a, input, 4))
347 (void)*(char*)0; // expected-warning{{null}}
348}
349
350void memcmp5 (char *input) {
351 char a[] = {1, 2, 3, 4};
352
353 if (memcmp(a, 0, 0)) // no-warning
Tom Care7bce3a12010-07-27 23:30:21 +0000354 (void)*(char*)0; // no-warning
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000355 if (memcmp(0, a, 0)) // no-warning
Tom Care7bce3a12010-07-27 23:30:21 +0000356 (void)*(char*)0; // no-warning
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000357 if (memcmp(a, input, 0)) // no-warning
Tom Care7bce3a12010-07-27 23:30:21 +0000358 (void)*(char*)0; // no-warning
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000359}
360
Jordy Rosed325ffb2010-07-08 23:57:29 +0000361void memcmp6 (char *a, char *b, size_t n) {
362 int result = memcmp(a, b, n);
363 if (result != 0)
364 return;
365 if (n == 0)
366 (void)*(char*)0; // expected-warning{{null}}
367}
368
Jordy Roseb6a40262010-08-05 23:11:30 +0000369int memcmp7 (char *a, size_t x, size_t y, size_t n) {
370 // We used to crash when either of the arguments was unknown.
371 return memcmp(a, &a[x*y], n) +
372 memcmp(&a[x*y], a, n);
373}
374
Jordy Rosebc56d1f2010-07-07 08:15:01 +0000375//===----------------------------------------------------------------------===
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000376// bcopy()
377//===----------------------------------------------------------------------===
378
379#define bcopy BUILTIN(bcopy)
380// __builtin_bcopy is not defined with const in Builtins.def.
381void bcopy(/*const*/ void *s1, void *s2, size_t n);
382
383
384void bcopy0 () {
385 char src[] = {1, 2, 3, 4};
Jordy Rosee64f3112010-08-16 07:51:42 +0000386 char dst[4] = {0};
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000387
388 bcopy(src, dst, 4); // no-warning
Jordy Rosee64f3112010-08-16 07:51:42 +0000389
390 if (dst[0] != 0)
391 (void)*(char*)0; // expected-warning{{null}}
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000392}
393
394void bcopy1 () {
395 char src[] = {1, 2, 3, 4};
396 char dst[10];
397
398 bcopy(src, dst, 5); // expected-warning{{out-of-bound}}
399}
400
401void bcopy2 () {
402 char src[] = {1, 2, 3, 4};
403 char dst[1];
404
Jordy Rosee64f3112010-08-16 07:51:42 +0000405 bcopy(src, dst, 4); // expected-warning{{overflow}}
Jordy Roseccbf7ee2010-07-06 23:11:01 +0000406}