1 // RUN: %clang_cc1 -Wno-array-bounds -analyze -analyzer-check-objc-mem -analyzer-check-buffer-overflows -verify %s
3 // Tests doing an out-of-bounds access after the end of an array using:
4 // - constant integer index
5 // - constant integer size for buffer
8 buf
[100] = 1; // expected-warning{{Out of bound memory access}}
11 void test1_ok(int x
) {
13 buf
[99] = 1; // no-warning
16 const char test1_strings_underrun(int x
) {
17 const char *mystr
= "mary had a little lamb";
18 return mystr
[-1]; // expected-warning{{Out of bound memory access}}
21 const char test1_strings_overrun(int x
) {
22 const char *mystr
= "mary had a little lamb";
23 return mystr
[1000]; // expected-warning{{Out of bound memory access}}
26 const char test1_strings_ok(int x
) {
27 const char *mystr
= "mary had a little lamb";
28 return mystr
[5]; // no-warning
31 // Tests doing an out-of-bounds access after the end of an array using:
32 // - indirect pointer to buffer
33 // - constant integer index
34 // - constant integer size for buffer
35 void test1_ptr(int x
) {
38 p
[101] = 1; // expected-warning{{Out of bound memory access}}
41 void test1_ptr_ok(int x
) {
44 p
[99] = 1; // no-warning
47 // Tests doing an out-of-bounds access before the start of an array using:
48 // - indirect pointer to buffer, manipulated using simple pointer arithmetic
49 // - constant integer index
50 // - constant integer size for buffer
51 void test1_ptr_arith(int x
) {
55 p
[0] = 1; // expected-warning{{Out of bound memory access}}
58 void test1_ptr_arith_ok(int x
) {
62 p
[0] = 1; // no-warning
65 void test1_ptr_arith_bad(int x
) {
69 p
[1] = 1; // expected-warning{{Out of bound memory access}}
72 void test1_ptr_arith_ok2(int x
) {
76 p
[-1] = 1; // no-warning
79 // Tests doing an out-of-bounds access before the start of an array using:
80 // - constant integer index
81 // - constant integer size for buffer
84 buf
[-1] = 1; // expected-warning{{Out of bound memory access}}
87 // Tests doing an out-of-bounds access before the start of an array using:
88 // - indirect pointer to buffer
89 // - constant integer index
90 // - constant integer size for buffer
91 void test2_ptr(int x
) {
94 p
[-1] = 1; // expected-warning{{Out of bound memory access}}
97 // ** FIXME ** Doesn't work yet because we don't support pointer arithmetic.
98 // Tests doing an out-of-bounds access before the start of an array using:
99 // - indirect pointer to buffer, manipulated using simple pointer arithmetic
100 // - constant integer index
101 // - constant integer size for buffer
102 void test2_ptr_arith(int x
) {
106 p
[0] = 1; // no-warning
109 // Tests doing an out-of-bounds access before the start of a multi-dimensional
111 // - constant integer indices
112 // - constant integer sizes for the array
113 void test2_multi(int x
) {
115 buf
[0][-1] = 1; // expected-warning{{Out of bound memory access}}
118 // Tests doing an out-of-bounds access before the start of a multi-dimensional
120 // - constant integer indices
121 // - constant integer sizes for the array
122 void test2_multi_b(int x
) {
124 buf
[-1][0] = 1; // expected-warning{{Out of bound memory access}}
127 void test2_multi_ok(int x
) {
129 buf
[0][0] = 1; // no-warning
133 // We don't get a warning here yet because our symbolic constraint solving
134 // doesn't handle: (symbol * constant) < constant
142 // We don't get a warning here yet because our symbolic constraint solving
143 // doesn't handle: (symbol * constant) < constant