2011-08-04 07:14:55 +08:00
|
|
|
// RUN: %clang_cc1 -Wno-array-bounds -analyze -analyzer-checker=core,experimental.unix,experimental.security.ArrayBound -analyzer-store=region -verify %s
|
2010-01-18 16:54:31 +08:00
|
|
|
|
|
|
|
typedef __typeof(sizeof(int)) size_t;
|
|
|
|
void *malloc(size_t);
|
2010-06-01 11:01:33 +08:00
|
|
|
void *calloc(size_t, size_t);
|
2008-11-24 10:19:49 +08:00
|
|
|
|
|
|
|
char f1() {
|
|
|
|
char* s = "abcd";
|
2009-01-23 04:36:33 +08:00
|
|
|
char c = s[4]; // no-warning
|
2009-11-11 20:33:27 +08:00
|
|
|
return s[5] + c; // expected-warning{{Access out-of-bound array element (buffer overflow)}}
|
2008-11-24 10:19:49 +08:00
|
|
|
}
|
2010-01-18 16:54:31 +08:00
|
|
|
|
|
|
|
void f2() {
|
|
|
|
int *p = malloc(12);
|
|
|
|
p[3] = 4; // expected-warning{{Access out-of-bound array element (buffer overflow)}}
|
|
|
|
}
|
2010-04-01 16:20:27 +08:00
|
|
|
|
|
|
|
struct three_words {
|
|
|
|
int c[3];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct seven_words {
|
|
|
|
int c[7];
|
|
|
|
};
|
|
|
|
|
|
|
|
void f3() {
|
|
|
|
struct three_words a, *p;
|
|
|
|
p = &a;
|
|
|
|
p[0] = a; // no-warning
|
|
|
|
p[1] = a; // expected-warning{{Access out-of-bound array element (buffer overflow)}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f4() {
|
|
|
|
struct seven_words c;
|
|
|
|
struct three_words a, *p = (struct three_words *)&c;
|
|
|
|
p[0] = a; // no-warning
|
|
|
|
p[1] = a; // no-warning
|
|
|
|
p[2] = a; // expected-warning{{Access out-of-bound array element (buffer overflow)}}
|
|
|
|
}
|
2010-06-01 11:01:33 +08:00
|
|
|
|
|
|
|
void f5() {
|
|
|
|
char *p = calloc(2,2);
|
|
|
|
p[3] = '.'; // no-warning
|
|
|
|
p[4] = '!'; // expected-warning{{out-of-bound}}
|
|
|
|
}
|
2010-06-26 07:23:04 +08:00
|
|
|
|
|
|
|
void f6() {
|
|
|
|
char a[2];
|
|
|
|
int *b = (int*)a;
|
|
|
|
b[1] = 3; // expected-warning{{out-of-bound}}
|
|
|
|
}
|
2010-07-04 08:00:41 +08:00
|
|
|
|
|
|
|
void f7() {
|
|
|
|
struct three_words a;
|
|
|
|
a.c[3] = 1; // expected-warning{{out-of-bound}}
|
|
|
|
}
|
2010-07-05 08:50:15 +08:00
|
|
|
|
|
|
|
void vla(int a) {
|
|
|
|
if (a == 5) {
|
|
|
|
int x[a];
|
|
|
|
x[4] = 4; // no-warning
|
|
|
|
x[5] = 5; // expected-warning{{out-of-bound}}
|
|
|
|
}
|
|
|
|
}
|
2010-07-05 12:42:43 +08:00
|
|
|
|
2010-08-15 04:46:10 +08:00
|
|
|
void alloca_region(int a) {
|
|
|
|
if (a == 5) {
|
|
|
|
char *x = __builtin_alloca(a);
|
|
|
|
x[4] = 4; // no-warning
|
|
|
|
x[5] = 5; // expected-warning{{out-of-bound}}
|
|
|
|
}
|
|
|
|
}
|
2010-08-16 09:15:17 +08:00
|
|
|
|
|
|
|
int symbolic_index(int a) {
|
|
|
|
int x[2] = {1, 2};
|
|
|
|
if (a == 2) {
|
|
|
|
return x[a]; // expected-warning{{out-of-bound}}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int symbolic_index2(int a) {
|
|
|
|
int x[2] = {1, 2};
|
|
|
|
if (a < 0) {
|
|
|
|
return x[a]; // expected-warning{{out-of-bound}}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|