|
| 1 | +// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage-in-container -verify %s |
| 2 | + |
| 3 | +namespace std { |
| 4 | + template <class T> class span { |
| 5 | + public: |
| 6 | + constexpr span(T *, unsigned){} |
| 7 | + |
| 8 | + template<class Begin, class End> |
| 9 | + constexpr span(Begin first, End last){} |
| 10 | + |
| 11 | + T * data(); |
| 12 | + |
| 13 | + constexpr span() {}; |
| 14 | + |
| 15 | + constexpr span(const std::span<T> &span) {}; |
| 16 | + |
| 17 | + template<class R> |
| 18 | + constexpr span(R && range){}; |
| 19 | + }; |
| 20 | + |
| 21 | + |
| 22 | + template< class T > |
| 23 | + T&& move( T&& t ) noexcept; |
| 24 | +} |
| 25 | + |
| 26 | +namespace irrelevant_constructors { |
| 27 | + void non_two_param_constructors() { |
| 28 | + class Array { |
| 29 | + } a; |
| 30 | + std::span<int> S; // no warn |
| 31 | + std::span<int> S1{}; // no warn |
| 32 | + std::span<int> S2{std::move(a)}; // no warn |
| 33 | + std::span<int> S3{S2}; // no warn |
| 34 | + } |
| 35 | +} // irrelevant_constructors |
| 36 | + |
| 37 | +namespace construct_wt_ptr_size { |
| 38 | + std::span<int> warnVarInit(int *p) { |
| 39 | + std::span<int> S{p, 10}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 40 | + std::span<int> S1(p, 10); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 41 | + std::span<int> S2 = std::span{p, 10}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 42 | + std::span<int> S3 = std::span(p, 10); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 43 | + std::span<int> S4 = std::span<int>{p, 10}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 44 | + std::span<int> S5 = std::span<int>(p, 10); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 45 | + std::span<int> S6 = {p, 10}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 46 | + auto S7 = std::span<int>{p, 10}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 47 | + auto S8 = std::span<int>(p, 10); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 48 | + const auto &S9 = std::span<int>{p, 10}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 49 | + auto &&S10 = std::span<int>(p, 10); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 50 | + |
| 51 | +#define Ten 10 |
| 52 | + |
| 53 | + std::span S11 = std::span<int>{p, Ten}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 54 | + |
| 55 | + if (auto X = std::span<int>{p, Ten}; S10.data()) { // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 56 | + } |
| 57 | + |
| 58 | + auto X = warnVarInit(p); // function return is fine |
| 59 | + return S; |
| 60 | + } |
| 61 | + |
| 62 | + template<typename T> |
| 63 | + void foo(const T &, const T &&, T); |
| 64 | + |
| 65 | + std::span<int> warnTemp(int *p) { |
| 66 | + foo(std::span<int>{p, 10}, // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 67 | + std::move(std::span<int>{p, 10}), // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 68 | + std::span<int>{p, 10}); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 69 | + |
| 70 | + std::span<int> Arr[1] = {std::span<int>{p, 10}}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 71 | + |
| 72 | + if (std::span<int>{p, 10}.data()) { // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 73 | + } |
| 74 | + return std::span<int>{p, 10}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 75 | + } |
| 76 | +} // namespace construct_wt_ptr_size |
| 77 | + |
| 78 | +namespace construct_wt_begin_end { |
| 79 | + class It {}; |
| 80 | + |
| 81 | + std::span<int> warnVarInit(It &First, It &Last) { |
| 82 | + std::span<int> S{First, Last}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 83 | + std::span<int> S1(First, Last); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 84 | + std::span<int> S2 = std::span<int>{First, Last}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 85 | + std::span<int> S3 = std::span<int>(First, Last); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 86 | + std::span<int> S4 = std::span<int>{First, Last}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 87 | + std::span<int> S5 = std::span<int>(First, Last); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 88 | + std::span<int> S6 = {First, Last}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 89 | + auto S7 = std::span<int>{First, Last}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 90 | + auto S8 = std::span<int>(First, Last); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 91 | + const auto &S9 = std::span<int>{First, Last}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 92 | + auto &&S10 = std::span<int>(First, Last); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 93 | + |
| 94 | + if (auto X = std::span<int>{First, Last}; S10.data()) { // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 95 | + } |
| 96 | + |
| 97 | + auto X = warnVarInit(First, Last); // function return is fine |
| 98 | + return S; |
| 99 | + } |
| 100 | + |
| 101 | + template<typename T> |
| 102 | + void foo(const T &, const T &&, T); |
| 103 | + |
| 104 | + std::span<int> warnTemp(It &First, It &Last) { |
| 105 | + foo(std::span<int>{First, Last}, // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 106 | + std::move(std::span<int>{First, Last}), // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 107 | + std::span<int>{First, Last}); // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 108 | + |
| 109 | + std::span<int> Arr[1] = {std::span<int>{First, Last}}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 110 | + |
| 111 | + if (std::span<int>{First, Last}.data()) { // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 112 | + } |
| 113 | + return std::span<int>{First, Last}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 114 | + } |
| 115 | +} // namespace construct_wt_begin_end |
| 116 | + |
| 117 | +namespace test_flag { |
| 118 | + void f(int *p) { |
| 119 | +#pragma clang diagnostic push |
| 120 | +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" // this flag turns off every unsafe-buffer warning |
| 121 | + std::span<int> S{p, 10}; // no-warning |
| 122 | + p++; // no-warning |
| 123 | +#pragma clang diagnostic pop |
| 124 | + |
| 125 | +#pragma clang diagnostic push |
| 126 | +#pragma clang diagnostic warning "-Wunsafe-buffer-usage" |
| 127 | +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-container" |
| 128 | + // turn on all unsafe-buffer warnings except for the ones under `-Wunsafe-buffer-usage-in-container` |
| 129 | + std::span<int> S2{p, 10}; // no-warning |
| 130 | + |
| 131 | + p++; // expected-warning{{unsafe pointer arithmetic}}\ |
| 132 | + expected-note{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}} |
| 133 | +#pragma clang diagnostic pop |
| 134 | + |
| 135 | + } |
| 136 | +} //namespace test_flag |
0 commit comments