| 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 |
|
| 77 |
void notWarnSafeCases(unsigned n, int *p) { |
| 78 |
int X; |
| 79 |
unsigned Y = 10; |
| 80 |
std::span<int> S = std::span{&X, 1}; // no-warning |
| 81 |
int Arr[10]; |
| 82 |
|
| 83 |
S = std::span{&X, 2}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 84 |
S = std::span{new int[10], 10}; // no-warning |
| 85 |
S = std::span{new int[n], n}; // no-warning |
| 86 |
S = std::span{new int, 1}; // no-warning |
| 87 |
S = std::span{new int, X}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 88 |
S = std::span{new int[n--], n--}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 89 |
S = std::span{new int[10], 11}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} |
| 90 |
S = std::span{new int[10], 9}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} // not smart enough to tell its safe |
| 91 |
S = std::span{new int[10], Y}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} // not smart enough to tell its safe |
| 92 |
S = std::span{Arr, 10}; // no-warning |
| 93 |
S = std::span{Arr, Y}; // expected-warning{{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}} // not smart enough to tell its safe |
| 94 |
S = std::span{p, 0}; // no-warning |
| 95 |
} |
| 96 |
} // namespace construct_wt_ptr_size |
| 97 |
|
| 98 |
namespace construct_wt_begin_end { |
| 99 |
class It {}; |
| 100 |
|
| 101 |
std::span<int> warnVarInit(It &First, It &Last) { |
| 102 |
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}} |
| 103 |
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}} |
| 104 |
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}} |
| 105 |
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}} |
| 106 |
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}} |
| 107 |
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}} |
| 108 |
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}} |
| 109 |
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}} |
| 110 |
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}} |
| 111 |
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}} |
| 112 |
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}} |
| 113 |
|
| 114 |
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}} |
| 115 |
} |
| 116 |
|
| 117 |
auto X = warnVarInit(First, Last); // function return is fine |
| 118 |
return S; |
| 119 |
} |
| 120 |
|
| 121 |
template<typename T> |
| 122 |
void foo(const T &, const T &&, T); |
| 123 |
|
| 124 |
std::span<int> warnTemp(It &First, It &Last) { |
| 125 |
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}} |
| 126 |
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}} |
| 127 |
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}} |
| 128 |
|
| 129 |
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}} |
| 130 |
|
| 131 |
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}} |
| 132 |
} |
| 133 |
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}} |
| 134 |
} |
| 135 |
} // namespace construct_wt_begin_end |
| 136 |
|
| 137 |
namespace test_flag { |
| 138 |
void f(int *p) { |
| 139 |
#pragma clang diagnostic push |
| 140 |
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" // this flag turns off every unsafe-buffer warning |
| 141 |
std::span<int> S{p, 10}; // no-warning |
| 142 |
p++; // no-warning |
| 143 |
#pragma clang diagnostic pop |
| 144 |
|
| 145 |
#pragma clang diagnostic push |
| 146 |
#pragma clang diagnostic warning "-Wunsafe-buffer-usage" |
| 147 |
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-container" |
| 148 |
// turn on all unsafe-buffer warnings except for the ones under `-Wunsafe-buffer-usage-in-container` |
| 149 |
std::span<int> S2{p, 10}; // no-warning |
| 150 |
|
| 151 |
p++; // expected-warning{{unsafe pointer arithmetic}}\ |
| 152 |
expected-note{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}} |
| 153 |
#pragma clang diagnostic pop |
| 154 |
|
| 155 |
} |
| 156 |
} //namespace test_flag |