1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions -verify %s
namespace std {
class type_info { };
}
void local_assign_both_span() {
int tmp;
int* p = new int[10]; // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'q' to 'std::span' to propagate bounds information between them}}
tmp = p[4]; // expected-note{{used in buffer access here}}
int* q = new int[10]; // expected-warning{{'q' is an unsafe pointer used for buffer access}} expected-note{{change type of 'q' to 'std::span' to preserve bounds information, and change 'p' to 'std::span' to propagate bounds information between them}}
tmp = q[4]; // expected-note{{used in buffer access here}}
q = p;
}
void local_assign_rhs_span() {
int tmp;
int* p = new int[10];
int* q = new int[10]; // expected-warning{{'q' is an unsafe pointer used for buffer access}}
tmp = q[4]; // expected-note{{used in buffer access here}}
p = q; // FIXME: we do not fix `p = q` here as the `.data()` fix-it is not generally correct
}
void local_assign_no_span() {
int tmp;
int* p = new int[10];
int* q = new int[10];
p = q;
}
void local_assign_lhs_span() {
int tmp;
int* p = new int[10]; // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'q' to 'std::span' to propagate bounds information between them}}
tmp = p[4]; // expected-note{{used in buffer access here}}
int* q = new int[10];
p = q;
}
void lhs_span_multi_assign() {
int *a = new int[2];
int *b = a;
int *c = b;
int *d = c; // expected-warning{{'d' is an unsafe pointer used for buffer access}} expected-note{{change type of 'd' to 'std::span' to preserve bounds information, and change 'c', 'b', and 'a' to 'std::span' to propagate bounds information between them}}
int tmp = d[2]; // expected-note{{used in buffer access here}}
}
void rhs_span() {
int *x = new int[3];
int *y; // expected-warning{{'y' is an unsafe pointer used for buffer access}}
y[5] = 10; // expected-note{{used in buffer access here}}
x = y; // FIXME: we do not fix `x = y` here as the `.data()` fix-it is not generally correct
}
void rhs_span1() {
int *q = new int[12];
int *p = q; // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'q' and 'r' to 'std::span' to propagate bounds information between them}}
p[5] = 10; // expected-note{{used in buffer access here}}
int *r = q; // expected-warning{{'r' is an unsafe pointer used for buffer access}} expected-note{{change type of 'r' to 'std::span' to preserve bounds information, and change 'p' and 'q' to 'std::span' to propagate bounds information between them}}
r[10] = 5; // expected-note{{used in buffer access here}}
}
void rhs_span2() {
int *q = new int[6];
int *p = q; // expected-warning{{'p' is an unsafe pointer used for buffer access}}
p[5] = 10; // expected-note{{used in buffer access here}}
int *r = q; // FIXME: we do not fix `int *r = q` here as the `.data()` fix-it is not generally correct
}
void test_grouping() {
int *z = new int[8];
int tmp;
int *y = new int[10]; // expected-warning{{'y' is an unsafe pointer used for buffer access}}
tmp = y[5]; // expected-note{{used in buffer access here}}
int *x = new int[10];
x = y; // FIXME: we do not fix `x = y` here as the `.data()` fix-it is not generally correct
int *w = z;
}
void test_grouping1() {
int tmp;
int *y = new int[10]; // expected-warning{{'y' is an unsafe pointer used for buffer access}}
tmp = y[5]; // expected-note{{used in buffer access here}}
int *x = new int[10];
x = y; // FIXME: we do not fix `x = y` here as the `.data()` fix-it is not generally correct
int *w = new int[10]; // expected-warning{{'w' is an unsafe pointer used for buffer access}}
tmp = w[5]; // expected-note{{used in buffer access here}}
int *z = new int[10];
z = w; // FIXME: we do not fix `z = w` here as the `.data()` fix-it is not generally correct
}
void foo1a() {
int *r = new int[7];
int *p = new int[4]; // expected-warning{{'p' is an unsafe pointer used for buffer access}}
p = r;
int tmp = p[9]; // expected-note{{used in buffer access here}}
int *q;
q = r; // FIXME: we do not fix `q = r` here as the `.data()` fix-it is not generally correct
}
void foo1b() {
int *r = new int[7];
int *p = new int[4]; // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'r' and 'q' to 'std::span' to propagate bounds information between them}}
p = r;
int tmp = p[9]; // expected-note{{used in buffer access here}}
int *q; // expected-warning{{'q' is an unsafe pointer used for buffer access}} expected-note{{change type of 'q' to 'std::span' to preserve bounds information, and change 'p' and 'r' to 'std::span' to propagate bounds information between them}}
q = r;
tmp = q[9]; // expected-note{{used in buffer access here}}
}
void foo1c() {
int *r = new int[7]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
int *p = new int[4];
p = r; // FIXME: we do not fix `p = r` here as the `.data()` fix-it is not generally correct
int tmp = r[9]; // expected-note{{used in buffer access here}}
int *q; // expected-warning{{'q' is an unsafe pointer used for buffer access}}
q = r; // FIXME: we do not fix `q = r` here as the `.data()` fix-it is not generally correct
tmp = q[9]; // expected-note{{used in buffer access here}}
}
void foo2a() {
int *r = new int[7];
int *p = new int[5]; // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'q' and 'r' to 'std::span' to propagate bounds information between them}}
int *q = new int[4];
p = q;
int tmp = p[8]; // expected-note{{used in buffer access here}}
q = r;
}
void foo2b() {
int *r = new int[7];
int *p = new int[5];
int *q = new int[4]; // expected-warning{{'q' is an unsafe pointer used for buffer access}}
p = q; // FIXME: we do not fix `p = q` here as the `.data()` fix-it is not generally correct
int tmp = q[8]; // expected-note{{used in buffer access here}}
q = r;
}
void foo2c() {
int *r = new int[7];
int *p = new int[5]; // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'q' and 'r' to 'std::span' to propagate bounds information between them}}
int *q = new int[4]; // expected-warning{{'q' is an unsafe pointer used for buffer access}} expected-note{{change type of 'q' to 'std::span' to preserve bounds information, and change 'p' and 'r' to 'std::span' to propagate bounds information between them}}
p = q;
int tmp = p[8]; // expected-note{{used in buffer access here}}
q = r;
tmp = q[8]; // expected-note{{used in buffer access here}}
}
void foo3a() {
int *r = new int[7];
int *p = new int[5]; // expected-warning{{'p' is an unsafe pointer used for buffer access}}
int *q = new int[4];
q = p; // FIXME: we do not fix `q = p` here as the `.data()` fix-it is not generally correct
int tmp = p[8]; // expected-note{{used in buffer access here}}
q = r;
}
void foo3b() {
int *r = new int[7];
int *p = new int[5];
int *q = new int[4]; // expected-warning{{'q' is an unsafe pointer used for buffer access}} //expected-note{{change type of 'q' to 'std::span' to preserve bounds information, and change 'r' and 'p' to 'std::span' to propagate bounds information between them}}
q = p;
int tmp = q[8]; // expected-note{{used in buffer access here}}
q = r;
}
void test_crash() {
int *r = new int[8];
int *q = r;
int *p; // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'q' and 'r' to 'std::span' to propagate bounds information between them}}
p = q;
int tmp = p[9]; // expected-note{{used in buffer access here}}
}
void foo_uuc() {
int *ptr;
int *local; // expected-warning{{'local' is an unsafe pointer used for buffer access}}
local = ptr;
local++; // expected-note{{used in pointer arithmetic here}}
(local = ptr) += 5; // expected-warning{{unsafe pointer arithmetic}}
}
void check_rhs_fix() {
int *r = new int[8]; // expected-warning{{'r' is an unsafe pointer used for buffer access}} // expected-note{{change type of 'r' to 'std::span' to preserve bounds information, and change 'x' to 'std::span' to propagate bounds information between them}}
int *x;
r[7] = 9; // expected-note{{used in buffer access here}}
r = x;
}
void check_rhs_nofix() {
int *r = new int[8]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
int *x; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
r[7] = 9; // expected-note{{used in buffer access here}}
r = x;
x++; // expected-note{{used in pointer arithmetic here}}
}
void check_rhs_nofix_order() {
int *r = new int[8]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
int *x; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
x++; // expected-note{{used in pointer arithmetic here}}
r[7] = 9; // expected-note{{used in buffer access here}}
r = x;
}
void check_rhs_nofix_order1() {
int *r = new int[8]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
r[7] = 9; // expected-note{{used in buffer access here}}
int *x; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
x++; // expected-note{{used in pointer arithmetic here}}
r = x;
}
void check_rhs_nofix_order2() {
int *x; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
int *r = new int[8]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
r[7] = 9; // expected-note{{used in buffer access here}}
x++; // expected-note{{used in pointer arithmetic here}}
r = x;
}
void check_rhs_nofix_order3() {
int *x; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
int *r = new int[8]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
r = x;
r[7] = 9; // expected-note{{used in buffer access here}}
x++; // expected-note{{used in pointer arithmetic here}}
}
void check_rhs_nofix_order4() {
int *x; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
int *r = new int[8]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
r[7] = 9; // expected-note{{used in buffer access here}}
r = x;
x++; // expected-note{{used in pointer arithmetic here}}
}
void no_unhandled_lhs() {
int *r = new int[8]; // expected-warning{{'r' is an unsafe pointer used for buffer access}} // expected-note{{change type of 'r' to 'std::span' to preserve bounds information, and change 'x' to 'std::span' to propagate bounds information between them}}
r[7] = 9; // expected-note{{used in buffer access here}}
int *x;
r = x;
}
const std::type_info unhandled_lhs() {
int *r = new int[8]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
r[7] = 9; // expected-note{{used in buffer access here}}
int *x;
r = x;
return typeid(*r);
}
const std::type_info unhandled_rhs() {
int *r = new int[8]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
r[7] = 9; // expected-note{{used in buffer access here}}
int *x;
r = x;
return typeid(*x);
}
void test_negative_index() {
int *x = new int[4]; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
int *p; // expected-warning{{'p' is an unsafe pointer used for buffer access}}
p = &x[1]; // expected-note{{used in buffer access here}}
p[-1] = 9; // expected-note{{used in buffer access here}}
}
void test_unfixable() {
int *r = new int[8]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
int *x; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
x[7] = 9; // expected-note{{used in buffer access here}}
r = x;
r++; // expected-note{{used in pointer arithmetic here}}
}
void test_cyclic_deps() {
int *r = new int[10]; // expected-warning{{'r' is an unsafe pointer used for buffer access}} expected-note{{change type of 'r' to 'std::span' to preserve bounds information, and change 'p' and 'q' to 'std::span' to propagate bounds information between them}}
int *q;
q = r;
int *p;
p = q;
r[3] = 9; // expected-note{{used in buffer access here}}
r = p;
}
void test_cyclic_deps_a() {
int *r = new int[10]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
int *q;
q = r;
int *p; // expected-warning{{'p' is an unsafe pointer used for buffer access}}
p = q;
r[3] = 9; // expected-note{{used in buffer access here}}
r = p;
p++; // expected-note{{used in pointer arithmetic here}}
}
void test_cyclic_deps1() {
int *r = new int[10];
int *q;
q = r;
int *p; // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'q' and 'r' to 'std::span' to propagate bounds information between them}}
p = q;
p[3] = 9; // expected-note{{used in buffer access here}}
r = p;
}
void test_cyclic_deps2() {
int *r = new int[10];
int *q; // expected-warning{{'q' is an unsafe pointer used for buffer access}} expected-note{{change type of 'q' to 'std::span' to preserve bounds information, and change 'r' and 'p' to 'std::span' to propagate bounds information between them}}
q = r;
int *p;
p = q;
q[3] = 9; // expected-note{{used in buffer access here}}
r = p;
}
void test_cyclic_deps3() {
int *r = new int[10];
int *q; // expected-warning{{'q' is an unsafe pointer used for buffer access}} expected-note{{change type of 'q' to 'std::span' to preserve bounds information, and change 'r' and 'p' to 'std::span' to propagate bounds information between them}}
q = r;
int *p; // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'q' and 'r' to 'std::span' to propagate bounds information between them}}
p = q;
q[3] = 9; // expected-note{{used in buffer access here}}
p[4] = 7; // expected-note{{used in buffer access here}}
r = p;
}
void test_cyclic_deps4() {
int *r = new int[10]; // expected-warning{{'r' is an unsafe pointer used for buffer access}} expected-note{{change type of 'r' to 'std::span' to preserve bounds information, and change 'p' and 'q' to 'std::span' to propagate bounds information between them}}
int *q; // expected-warning{{'q' is an unsafe pointer used for buffer access}} expected-note{{change type of 'q' to 'std::span' to preserve bounds information, and change 'r' and 'p' to 'std::span' to propagate bounds information between them}}
q = r;
int *p; // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'r' and 'q' to 'std::span' to propagate bounds information between them}}
p = q;
q[3] = 9; // expected-note{{used in buffer access here}}
p[4] = 7; // expected-note{{used in buffer access here}}
r[1] = 5; // expected-note{{used in buffer access here}}
r = p;
}
|