| 1 |
/* PR tree-optimization/83776: missing -Warray-bounds indexing past the end |
| 2 |
of a string literal |
| 3 |
Test to exercise detection of out-of-bounds indices into narrow string |
| 4 |
literals. |
| 5 |
{ dg-do compile } |
| 6 |
{ dg-options "-O2 -Warray-bounds -ftrack-macro-expansion=0" } */ |
| 7 |
|
| 8 |
#include "range.h" |
| 9 |
|
| 10 |
#define MAX DIFF_MAX |
| 11 |
#define MIN DIFF_MIN |
| 12 |
|
| 13 |
#define S1 "1" |
| 14 |
#define S3 "123" |
| 15 |
#define S7 "1234567" |
| 16 |
#define S8 "12345678" |
| 17 |
#define S9 "123456789" |
| 18 |
|
| 19 |
void sink (int, ...); |
| 20 |
|
| 21 |
#define T(expr) sink (0, expr) |
| 22 |
|
| 23 |
|
| 24 |
void narrow_direct_cst (void) |
| 25 |
{ |
| 26 |
T (S1[MIN]); /* { dg-warning "array subscript -\[0-9\]+ is below array bounds of .char\\\[2]" "bug 86611" { xfail lp64 } } */ |
| 27 |
T (S1[-1]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[2]" } */ |
| 28 |
T (S1[0]); |
| 29 |
T (S1[1]); |
| 30 |
T (S1[2]); /* { dg-warning "array subscript 2 is above array bounds of .char\\\[2]" } */ |
| 31 |
T (S1[MAX]); /* { dg-warning "array subscript \[0-9\]+ is above array bounds of .char\\\[2]" } */ |
| 32 |
|
| 33 |
T (&S1[MIN]); /* { dg-warning "array subscript -\[0-9\]+ is below array bounds of .char\\\[2]" } */ |
| 34 |
T (&S1[-1]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[2]" } */ |
| 35 |
T (&S1[0]); |
| 36 |
T (&S1[2]); |
| 37 |
T (&S1[3]); /* { dg-warning "array subscript 3 is above array bounds of .char\\\[2]" } */ |
| 38 |
T (&S1[MAX]); /* { dg-warning "array subscript \[0-9\]+ is above array bounds of .char\\\[2]" } */ |
| 39 |
|
| 40 |
T (S9[MIN]); /* { dg-warning "array subscript -\[0-9\]+ is below array bounds of .char\\\[10]" "xfail lp64" { xfail lp64 } } */ |
| 41 |
T (S9[-1]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[10]" } */ |
| 42 |
T (S9[9]); |
| 43 |
T (S9[10]); /* { dg-warning "array subscript 10 is above array bounds of .char\\\[10]" } */ |
| 44 |
T (S9[11]); /* { dg-warning "array subscript 11 is above array bounds of .char\\\[10]" } */ |
| 45 |
T (S9[MAX]); /* { dg-warning "array subscript \[0-9\]+ is above array bounds of .char\\\[10]" } */ |
| 46 |
|
| 47 |
T (&S9[MIN]); /* { dg-warning "array subscript -\[0-9\]+ is below array bounds of .char\\\[10]" } */ |
| 48 |
T (&S9[-1]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[10]" } */ |
| 49 |
T (&S9[9]); |
| 50 |
T (&S9[10]); |
| 51 |
T (&S9[11]); /* { dg-warning "array subscript 11 is above array bounds of .char\\\[10]" } */ |
| 52 |
T (&S9[MAX]); /* { dg-warning "array subscript \[0-9\]+ is above array bounds of .char\\\[10]" } */ |
| 53 |
} |
| 54 |
|
| 55 |
void narrow_ptr_deref_cst (void) |
| 56 |
{ |
| 57 |
const char *p = S8 + 9; |
| 58 |
|
| 59 |
T (*(p + MIN)); /* { dg-warning "array subscript -\[0-9\]+ is outside array bounds of .char\\\[9]." } */ |
| 60 |
T (*(p - 10)); /* { dg-warning "array subscript -1 is outside array bounds of .char\\\[9]." } */ |
| 61 |
T (*(p - 9)); |
| 62 |
T (*(p - 1)); |
| 63 |
T (*p); /* { dg-warning "array subscript 9 is outside array bounds of .char\\\[9]." } */ |
| 64 |
T (*(p + 1)); /* { dg-warning "array subscript 10 is outside array bounds of .char\\\[9]." } */ |
| 65 |
T (*(p + 2)); /* { dg-warning "array subscript 11 is outside array bounds of .char\\\[9]." } */ |
| 66 |
} |
| 67 |
|
| 68 |
void narrow_ptr_index_cst (void) |
| 69 |
{ |
| 70 |
const char *p = S7; |
| 71 |
|
| 72 |
T (p[MIN + 1]); /* { dg-warning "array subscript -\[0-9\]+ is outside array bounds of .char\\\[8]." "bug 86611" { xfail lp64 } } */ |
| 73 |
T (p[-1]); /* { dg-warning "array subscript -1 is outside array bounds of .char\\\[8]." } */ |
| 74 |
T (p[0]); |
| 75 |
T (p[1]); |
| 76 |
T (p[8]); /* { dg-warning "array subscript 8 is outside array bounds of .char\\\[8]." } */ |
| 77 |
T (p[99]); /* { dg-warning "array subscript 99 is outside array bounds of .char\\\[8]." } */ |
| 78 |
T (p[MAX]); /* { dg-warning "array subscript \[0-9\]+ is outside array bounds of .char\\\[8]." } */ |
| 79 |
|
| 80 |
T (&p[MIN + 1]); /* { dg-warning "array subscript -\[0-9\]+ is \(below|outside\) array bounds of .char\\\[8]." } */ |
| 81 |
T (&p[-1]); /* { dg-warning "array subscript -1 is \(below|outside\) array bounds of .char\\\[8]." } */ |
| 82 |
T (&p[0]); |
| 83 |
T (&p[1]); |
| 84 |
T (&p[8]); |
| 85 |
T (&p[9]); /* { dg-warning "array subscript 9 is \(above|outside\) array bounds of .char\\\[8]." } */ |
| 86 |
T (&p[99]); /* { dg-warning "array subscript 99 is \(above|outside\) array bounds of .char\\\[8]." } */ |
| 87 |
T (&p[MAX]); /* { dg-warning "array subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[8]." } */ |
| 88 |
|
| 89 |
const char *q = S8 + 4; |
| 90 |
T (q[MIN + 1]); /* { dg-warning "array subscript -?\[0-9\]+ is outside array bounds of .char\\\[9]." "bug 86611" { xfail lp64 } } */ |
| 91 |
T (q[-5]); /* { dg-warning "array subscript -1 is outside array bounds of .char\\\[9]." } */ |
| 92 |
T (q[-4]); |
| 93 |
T (q[0]); |
| 94 |
T (q[1]); |
| 95 |
T (q[3]); |
| 96 |
T (q[4]); |
| 97 |
T (q[5]); /* { dg-warning "array subscript 9 is outside array bounds of .char\\\[9]." } */ |
| 98 |
T (q[99]); /* { dg-warning "array subscript 103 is outside array bounds of .char\\\[9]." } */ |
| 99 |
T (q[MAX - 4]); /* { dg-warning "array subscript \[0-9\]+ is outside array bounds of .char\\\[9]." } */ |
| 100 |
T (q[MAX - 3]); /* { dg-warning "array subscript -?\[0-9\]+ is outside array bounds of .char\\\[9]." "bug 86611" { xfail lp64 } } */ |
| 101 |
|
| 102 |
T (&q[MIN + 1]); /* { dg-warning "array subscript -?\[0-9\]+ is \(below|outside\) array bounds of .char\\\[9]." } */ |
| 103 |
T (&q[-5]); /* { dg-warning "array subscript -1 is \(below|outside\) array bounds of .char\\\[9]." } */ |
| 104 |
T (&q[-4]); |
| 105 |
T (&q[0]); |
| 106 |
T (&q[1]); |
| 107 |
T (&q[5]); |
| 108 |
T (&q[6]); /* { dg-warning "array subscript 10 is \(above|outside\) array bounds of .char\\\[9]." } */ |
| 109 |
T (&q[99]); /* { dg-warning "array subscript 103 is \(above|outside\) array bounds of .char\\\[9]." } */ |
| 110 |
T (&q[MAX - 4]); /* { dg-warning "array subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[9]." } */ |
| 111 |
T (&q[MAX - 3]); /* { dg-warning "array subscript -?\[0-9\]+ is \(below|outside\) array bounds of .char\\\[9]." } */ |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
void narrow_direct_range (ptrdiff_t i, size_t j) |
| 116 |
{ |
| 117 |
T (S3[i]); |
| 118 |
T (S9[j]); |
| 119 |
|
| 120 |
T (S3[SR (MIN, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[4]" } */ |
| 121 |
T (S3[SR (MIN, 0)]); |
| 122 |
T (S3[SR (-2, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[4]" } */ |
| 123 |
T (S3[SR (1, 2)]); |
| 124 |
T (S3[SR (1, 999)]); |
| 125 |
T (S3[SR (2, 999)]); |
| 126 |
T (S3[SR (3, 999)]); |
| 127 |
T (S3[SR (4, 999)]); /* { dg-warning "array subscript 4 is above array bounds of .char\\\[4]" } */ |
| 128 |
|
| 129 |
T (&S3[SR (MIN, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[4]" } */ |
| 130 |
T (&S3[SR (MIN, 0)]); |
| 131 |
T (&S3[SR (-2, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[4]" } */ |
| 132 |
T (&S3[SR (1, 2)]); |
| 133 |
T (&S3[SR (1, 999)]); |
| 134 |
T (&S3[SR (2, 999)]); |
| 135 |
T (&S3[SR (4, 999)]); |
| 136 |
T (&S3[SR (5, 999)]); /* { dg-warning "array subscript 5 is above array bounds of .char\\\[4]" } */ |
| 137 |
|
| 138 |
T (S9[SR (MIN, -9)]); /* { dg-warning "array subscript -9 is below array bounds of .char\\\[10]" } */ |
| 139 |
T (S9[SR (MIN, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[10]" } */ |
| 140 |
T (S9[SR (MIN, 0)]); |
| 141 |
T (S9[SR (-2, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[10]" } */ |
| 142 |
T (S9[SR (1, 2)]); |
| 143 |
T (S9[SR (1, 9)]); |
| 144 |
T (S9[SR (1, 999)]); |
| 145 |
T (S9[SR (9, 999)]); |
| 146 |
T (S9[SR (10, 999)]); /* { dg-warning "array subscript 10 is above array bounds of .char\\\[10]" } */ |
| 147 |
T (S9[SR (99, MAX)]); /* { dg-warning "array subscript 99 is above array bounds of .char\\\[10]" } */ |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
void narrow_ptr_deref_range (ptrdiff_t i, size_t j) |
| 152 |
{ |
| 153 |
const char *p; |
| 154 |
|
| 155 |
p = S1 + i; |
| 156 |
T (*p); |
| 157 |
|
| 158 |
p = S1 + j; |
| 159 |
T (*p); |
| 160 |
|
| 161 |
p = S1 + SR (-999, 999); |
| 162 |
T (*p); |
| 163 |
|
| 164 |
p = S1 + SR (-1, 1); |
| 165 |
T (*p); |
| 166 |
|
| 167 |
p = S1 + SR (-1, 0); |
| 168 |
T (*p); |
| 169 |
|
| 170 |
p = S1 + SR (0, 1); |
| 171 |
T (*p); |
| 172 |
|
| 173 |
p = S1 + SR (1, 2); |
| 174 |
T (*p); |
| 175 |
|
| 176 |
p = S1 + SR (2, 3); |
| 177 |
T (*p); /* { dg-warning "array subscript 2 is outside array bounds of .char\\\[2]." } */ |
| 178 |
|
| 179 |
p = S1 + SR (9, 99); |
| 180 |
T (*p); /* { dg-warning "array subscript \\\[9, 99] is outside array bounds of .char\\\[2]." } */ |
| 181 |
|
| 182 |
p = S8 + SR (-999, 999); |
| 183 |
T (*p); |
| 184 |
|
| 185 |
p = S8 + SR (-9, -1); |
| 186 |
T (*p); /* { dg-warning "array subscript \\\[-9, -1] is outside array bounds of .char\\\[9]." } */ |
| 187 |
|
| 188 |
p = S8 + SR (-9, 0); |
| 189 |
T (*p); |
| 190 |
|
| 191 |
p = S8 + SR (-9, 9); |
| 192 |
T (*p); |
| 193 |
|
| 194 |
p = S8 + SR (-9, 123); |
| 195 |
T (*p); |
| 196 |
|
| 197 |
p = S8 + SR (8, 123); |
| 198 |
T (*p); |
| 199 |
|
| 200 |
p = S8 + SR (9, 123); |
| 201 |
T (*p); /* { dg-warning "array subscript 9 is outside array bounds of .char\\\[9]." } */ |
| 202 |
|
| 203 |
{ |
| 204 |
const char *p1 = S3 + i; |
| 205 |
const char *p2 = p1 + i; |
| 206 |
const char *p3 = p2 + i; |
| 207 |
const char *p4 = p3 + i; |
| 208 |
const char *p5 = p4 + i; |
| 209 |
|
| 210 |
T (*p1); |
| 211 |
T (*p2); |
| 212 |
T (*p3); |
| 213 |
T (*p4); |
| 214 |
T (*p5); |
| 215 |
} |
| 216 |
|
| 217 |
{ |
| 218 |
i = SR (1, 2); |
| 219 |
|
| 220 |
const char *p1 = S3 + SR (1, DIFF_MAX - 1); |
| 221 |
const char *p2 = p1 + i; |
| 222 |
const char *p3 = p2 + i; |
| 223 |
const char *p4 = p3 + i; |
| 224 |
const char *p5 = p4 + i; |
| 225 |
|
| 226 |
T (*p1); |
| 227 |
T (*p2); |
| 228 |
T (*p3); |
| 229 |
T (*p4); /* { dg-warning "array subscript 4 is outside array bounds of .char\\\[4]." } */ |
| 230 |
T (*p5); /* { dg-warning "array subscript \\\[5, \[0-9\]+] is outside array bounds of .char\\\[4]." } */ |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
void narrow_ptr_index_range (void) |
| 236 |
{ |
| 237 |
const char *p; |
| 238 |
|
| 239 |
p = S7; |
| 240 |
T (p[SR (-9, -1)]); /* { dg-warning "array subscript \\\[-9, -1] is outside array bounds of .char\\\[8]." } */ |
| 241 |
T (p[SR (-8, 0)]); |
| 242 |
T (p[SR (0, MAX)]); |
| 243 |
T (p[SR (1, 9)]); |
| 244 |
T (p[SR (8, 9)]); /* { dg-warning "array subscript 8 is outside array bounds of .char\\\[8]." } */ |
| 245 |
|
| 246 |
p = S7 + SR (4, 6); |
| 247 |
T (p[5]); /* { dg-warning "array subscript \\\[9, 11] is outside array bounds of .char\\\[8]." } */ |
| 248 |
} |