outofbound.c revision 9618b858e2b4f79aa2b8b0291e9c833cee0435f8
1// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-experimental-checks -analyzer-check-objc-mem -analyzer-store=region -verify %s 2 3typedef __typeof(sizeof(int)) size_t; 4void *malloc(size_t); 5 6char f1() { 7 char* s = "abcd"; 8 char c = s[4]; // no-warning 9 return s[5] + c; // expected-warning{{Access out-of-bound array element (buffer overflow)}} 10} 11 12void f2() { 13 int *p = malloc(12); 14 p[3] = 4; // expected-warning{{Access out-of-bound array element (buffer overflow)}} 15} 16 17struct three_words { 18 int c[3]; 19}; 20 21struct seven_words { 22 int c[7]; 23}; 24 25void 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 32void 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} 39