[clang] [-Wunsafe-buffer-usage] Support span creation from std::initializer_list and begin/end pairs (PR #145311)
Ziqing Luo via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 24 02:58:41 PDT 2025
================
@@ -232,3 +268,27 @@ struct HoldsStdSpanAndNotInitializedInCtor {
: Ptr(P), Size(S)
{}
};
+
+namespace test_begin_end {
+ struct Object {
+ int * begin();
+ int * end();
+ };
+ void safe_cases(std::span<int> Sp, std::array<int, 10> Arr, std::string Str, std::initializer_list<Object> Il) {
+ std::span<int>{Sp.begin(), Sp.end()};
+ std::span<int>{Arr.begin(), Arr.end()};
+ std::span<char>{Str.begin(), Str.end()};
+ std::span<Object>{Il.begin(), Il.end()};
+ }
+
+ void unsafe_cases(std::span<int> Sp, std::array<int, 10> Arr, std::string Str, std::initializer_list<Object> Il,
+ Object Obj) {
+ std::span<int>{Obj.begin(), Obj.end()}; // expected-warning {{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}}
+ std::span<int>{Sp.end(), Sp.begin()}; // expected-warning {{the two-parameter std::span construction is unsafe as it can introduce mismatch between buffer size and the bound information}}
----------------
ziqingluo-90 wrote:
My understanding is that it warns about the mismatch between **the bound information of the constructed span** and the actual buffer size. So I think the message is accurate regardless of how the span is constructed.
https://github.com/llvm/llvm-project/pull/145311
More information about the cfe-commits
mailing list