Ted Kremenek | 565e465 | 2010-02-05 02:06:54 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-experimental-checks -analyzer-check-objc-mem -analyzer-store=region -verify %s |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 2 | |
| 3 | typedef __typeof(sizeof(int)) size_t; |
| 4 | void *malloc(size_t); |
Zhongxing Xu | 20f0178 | 2008-11-24 02:19:49 +0000 | [diff] [blame] | 5 | |
| 6 | char f1() { |
| 7 | char* s = "abcd"; |
Ted Kremenek | f9e9684 | 2009-01-22 20:36:33 +0000 | [diff] [blame] | 8 | char c = s[4]; // no-warning |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 9 | return s[5] + c; // expected-warning{{Access out-of-bound array element (buffer overflow)}} |
Zhongxing Xu | 20f0178 | 2008-11-24 02:19:49 +0000 | [diff] [blame] | 10 | } |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 11 | |
| 12 | void f2() { |
| 13 | int *p = malloc(12); |
| 14 | p[3] = 4; // expected-warning{{Access out-of-bound array element (buffer overflow)}} |
| 15 | } |
Zhongxing Xu | 9618b85 | 2010-04-01 08:20:27 +0000 | [diff] [blame^] | 16 | |
| 17 | struct three_words { |
| 18 | int c[3]; |
| 19 | }; |
| 20 | |
| 21 | struct seven_words { |
| 22 | int c[7]; |
| 23 | }; |
| 24 | |
| 25 | void f3() { |
| 26 | struct three_words a, *p; |
| 27 | p = &a; |
| 28 | p[0] = a; // no-warning |
| 29 | p[1] = a; // expected-warning{{Access out-of-bound array element (buffer overflow)}} |
| 30 | } |
| 31 | |
| 32 | void f4() { |
| 33 | struct seven_words c; |
| 34 | struct three_words a, *p = (struct three_words *)&c; |
| 35 | p[0] = a; // no-warning |
| 36 | p[1] = a; // no-warning |
| 37 | p[2] = a; // expected-warning{{Access out-of-bound array element (buffer overflow)}} |
| 38 | } |