98 lines · cpp
1// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions -verify %s2void bar(int * param) {}3 4void foo1a() {5 int *r = new int[7];6 int *p = new int[4]; // expected-warning{{'p' is an unsafe pointer used for buffer access}}7 p = r;8 int tmp = p[9]; // expected-note{{used in buffer access here}}9 int *q;10 q = r; // FIXME: we do not fix `q = r` here as the `.data()` fix-it is not generally correct11}12 13void uuc_if_body() {14 int *r = new int[7];15 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' to 'std::span' to propagate bounds information between them}}16 if (true)17 p = r;18 p[5] = 4; // expected-note{{used in buffer access here}}19}20 21void uuc_if_body1(bool flag) {22 int *r = new int[7];23 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' to 'std::span' to propagate bounds information between them}}24 if (flag) {25 p = r;26 }27 p[5] = 4; // expected-note{{used in buffer access here}}28}29 30void uuc_if_body2(bool flag) {31 int *r = new int[7];32 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' to 'std::span' to propagate bounds information between them}}33 if (flag) {34 } else {35 p = r;36 }37 38 p[5] = 4; // expected-note{{used in buffer access here}}39}40 41void uuc_if_body2_ptr_init(bool flag) {42 int *r = new int[7];43 if (flag) {44 } else {45 int* p = r; // 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' to 'std::span' to propagate bounds information between them}}46 p[5] = 4; // expected-note{{used in buffer access here}}47 }48}49 50void uuc_if_cond_no_unsafe_op() {51 int *r = new int[7];52 int *p = new int[4];53 if ((p = r)) {54 int x = 0;55 }56}57 58void uuc_if_cond_no_unsafe_op1() {59 int *r = new int[7];60 int *p = new int[4];61 if (true) {62 int x = 0;63 } else if ((p = r))64 int y = 10;65}66 67void uuc_if_cond_unsafe_op() {68 int *r = new int[7];69 int *p = new int[4]; //expected-warning{{'p' is an unsafe pointer used for buffer access}}70 if ((p = r)) {71 p[3] = 2; // expected-note{{used in buffer access here}}72 }73}74 75void uuc_if_cond_unsafe_op1() {76 int *r = new int[7]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}77 int *p = new int[4];78 if ((p = r)) {79 r[3] = 2; // expected-note{{used in buffer access here}}80 }81}82 83void uuc_if_cond_unsafe_op2() {84 int *r = new int[7]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}85 int *p = new int[4]; // expected-warning{{'p' is an unsafe pointer used for buffer access}}86 if ((p = r)) {87 r[3] = 2; // expected-note{{used in buffer access here}}88 }89 p[4] = 6; // expected-note{{used in buffer access here}}90}91 92void uuc_call1() {93 int *w = new int[4]; // expected-warning{{'w' is an unsafe pointer used for buffer access}}94 int *y = new int[4];95 bar(w = y);96 w[5] = 0; // expected-note{{used in buffer access here}}97}98