Ted Kremenek | 033a07e | 2011-08-03 23:14:55 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core,experimental.unix,experimental.security.ArrayBound -analyzer-store=region -verify %s |
Ted Kremenek | 2f08991 | 2009-07-10 21:48:10 +0000 | [diff] [blame] | 2 | |
| 3 | //===----------------------------------------------------------------------===// |
| 4 | // This file tests cases where we should not flag out-of-bounds warnings. |
| 5 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 0b9ad89 | 2009-05-04 14:31:19 +0000 | [diff] [blame] | 6 | |
Ted Kremenek | 20bd746 | 2009-05-04 07:04:36 +0000 | [diff] [blame] | 7 | void f() { |
| 8 | long x = 0; |
| 9 | char *y = (char*) &x; |
| 10 | char c = y[0] + y[1] + y[2]; // no-warning |
Zhongxing Xu | 9618b85 | 2010-04-01 08:20:27 +0000 | [diff] [blame] | 11 | short *z = (short*) &x; |
| 12 | short s = z[0] + z[1]; // no-warning |
Ted Kremenek | 20bd746 | 2009-05-04 07:04:36 +0000 | [diff] [blame] | 13 | } |
Jordy Rose | 4d912b2 | 2010-06-25 23:23:04 +0000 | [diff] [blame] | 14 | |
| 15 | void g() { |
| 16 | int a[2]; |
| 17 | char *b = (char*)a; |
| 18 | b[3] = 'c'; // no-warning |
| 19 | } |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 20 | |
| 21 | typedef typeof(sizeof(int)) size_t; |
| 22 | void *malloc(size_t); |
| 23 | void free(void *); |
| 24 | |
| 25 | void field() { |
| 26 | struct vec { size_t len; int data[0]; }; |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 27 | // FIXME: Not warn for this. |
| 28 | struct vec *a = malloc(sizeof(struct vec) + 10); // expected-warning {{Cast a region whose size is not a multiple of the destination type size}} |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 29 | a->len = 10; |
| 30 | a->data[1] = 5; // no-warning |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 31 | free(a); |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 32 | } |