1 // RUN: %clang_analyze_cc1 -Wno-array-bounds -analyzer-checker=core,alpha.security.ArrayBoundV2,debug.ExprInspection -verify %s
3 void clang_analyzer_eval(int);
5 // Tests doing an out-of-bounds access after the end of an array using:
6 // - constant integer index
7 // - constant integer size for buffer
10 buf
[100] = 1; // expected-warning{{Out of bound memory access}}
13 void test1_ok(int x
) {
15 buf
[99] = 1; // no-warning
18 const char test1_strings_underrun(int x
) {
19 const char *mystr
= "mary had a little lamb";
20 return mystr
[-1]; // expected-warning{{Out of bound memory access}}
23 const char test1_strings_overrun(int x
) {
24 const char *mystr
= "mary had a little lamb";
25 return mystr
[1000]; // expected-warning{{Out of bound memory access}}
28 const char test1_strings_ok(int x
) {
29 const char *mystr
= "mary had a little lamb";
30 return mystr
[5]; // no-warning
33 // Tests doing an out-of-bounds access after the end of an array using:
34 // - indirect pointer to buffer
35 // - constant integer index
36 // - constant integer size for buffer
37 void test1_ptr(int x
) {
40 p
[101] = 1; // expected-warning{{Out of bound memory access}}
43 void test1_ptr_ok(int x
) {
46 p
[99] = 1; // no-warning
49 // Tests doing an out-of-bounds access before the start of an array using:
50 // - indirect pointer to buffer, manipulated using simple pointer arithmetic
51 // - constant integer index
52 // - constant integer size for buffer
53 void test1_ptr_arith(int x
) {
57 p
[0] = 1; // expected-warning{{Out of bound memory access}}
60 void test1_ptr_arith_ok(int x
) {
64 p
[0] = 1; // no-warning
67 void test1_ptr_arith_bad(int x
) {
71 p
[1] = 1; // expected-warning{{Out of bound memory access}}
74 void test1_ptr_arith_ok2(int x
) {
78 p
[-1] = 1; // no-warning
81 // Tests doing an out-of-bounds access before the start of an array using:
82 // - constant integer index
83 // - constant integer size for buffer
86 buf
[-1] = 1; // expected-warning{{Out of bound memory access}}
89 // Tests doing an out-of-bounds access before the start of an array using:
90 // - indirect pointer to buffer
91 // - constant integer index
92 // - constant integer size for buffer
93 void test2_ptr(int x
) {
96 p
[-1] = 1; // expected-warning{{Out of bound memory access}}
99 // Tests doing an out-of-bounds access before the start of an array using:
100 // - indirect pointer to buffer, manipulated using simple pointer arithmetic
101 // - constant integer index
102 // - constant integer size for buffer
103 void test2_ptr_arith(int x
) {
107 p
[0] = 1; // expected-warning {{Out of bound memory access (accessed memory precedes memory block)}}
110 // Tests doing an out-of-bounds access before the start of a multi-dimensional
112 // - constant integer indices
113 // - constant integer sizes for the array
114 void test2_multi(int x
) {
116 buf
[0][-1] = 1; // expected-warning{{Out of bound memory access}}
119 // Tests doing an out-of-bounds access before the start of a multi-dimensional
121 // - constant integer indices
122 // - constant integer sizes for the array
123 void test2_multi_b(int x
) {
125 buf
[-1][0] = 1; // expected-warning{{Out of bound memory access}}
128 void test2_multi_ok(int x
) {
130 buf
[0][0] = 1; // no-warning
136 buf
[x
] = 1; // expected-warning{{Out of bound memory access}}
142 buf
[x
] = 1; // expected-warning{{Out of bound memory access}}
145 void test_assume_after_access(unsigned long x
) {
148 clang_analyzer_eval(x
<= 99); // expected-warning{{TRUE}}
151 // Don't warn when indexing below the start of a symbolic region's whose
152 // base extent we don't know.
153 int *get_symbolic(void);
154 void test_index_below_symboloc(void) {
155 int *buf
= get_symbolic();
156 buf
[-1] = 0; // no-warning;
159 void test_incomplete_struct(void) {
160 extern struct incomplete incomplete
;
161 int *p
= (int *)&incomplete
;
162 p
[1] = 42; // no-warning
165 void test_extern_void(void) {
168 p
[1] = 42; // no-warning
171 void test_assume_after_access2(unsigned long x
) {
174 clang_analyzer_eval(x
<= 99); // expected-warning{{TRUE}}