xref: /llvm-project/clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-uuc.cpp (revision 700baeb765cfe8628eb68bc24319b3db0209dd84)
1 // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions -verify %s
bar(int * param)2 void bar(int * param) {}
3 
foo1a()4 void 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 correct
11 }
12 
uuc_if_body()13 void 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 
uuc_if_body1(bool flag)21 void 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 
uuc_if_body2(bool flag)30 void 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 
uuc_if_body2_ptr_init(bool flag)41 void 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 
uuc_if_cond_no_unsafe_op()50 void 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 
uuc_if_cond_no_unsafe_op1()58 void 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 
uuc_if_cond_unsafe_op()67 void 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 
uuc_if_cond_unsafe_op1()75 void 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 
uuc_if_cond_unsafe_op2()83 void 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 
uuc_call1()92 void 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