1*28c1279dSPiotr Zegar // RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-suspicious-stringview-data-usage %t -- -- -isystem %clang_tidy_headers
2*28c1279dSPiotr Zegar #include <string>
3*28c1279dSPiotr Zegar
4*28c1279dSPiotr Zegar struct View {
5*28c1279dSPiotr Zegar const char* str;
6*28c1279dSPiotr Zegar };
7*28c1279dSPiotr Zegar
8*28c1279dSPiotr Zegar struct Pair {
9*28c1279dSPiotr Zegar const char* begin;
10*28c1279dSPiotr Zegar const char* end;
11*28c1279dSPiotr Zegar };
12*28c1279dSPiotr Zegar
13*28c1279dSPiotr Zegar struct ViewWithSize {
14*28c1279dSPiotr Zegar const char* str;
15*28c1279dSPiotr Zegar std::string_view::size_type size;
16*28c1279dSPiotr Zegar };
17*28c1279dSPiotr Zegar
18*28c1279dSPiotr Zegar void something(const char*);
19*28c1279dSPiotr Zegar void something(const char*, unsigned);
20*28c1279dSPiotr Zegar void something(const char*, unsigned, const char*);
21*28c1279dSPiotr Zegar void something_str(std::string, unsigned);
22*28c1279dSPiotr Zegar
invalid(std::string_view sv,std::string_view sv2)23*28c1279dSPiotr Zegar void invalid(std::string_view sv, std::string_view sv2) {
24*28c1279dSPiotr Zegar std::string s(sv.data());
25*28c1279dSPiotr Zegar // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
26*28c1279dSPiotr Zegar std::string si{sv.data()};
27*28c1279dSPiotr Zegar // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
28*28c1279dSPiotr Zegar std::string_view s2(sv.data());
29*28c1279dSPiotr Zegar // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
30*28c1279dSPiotr Zegar something(sv.data());
31*28c1279dSPiotr Zegar // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
32*28c1279dSPiotr Zegar something(sv.data(), sv.size(), sv2.data());
33*28c1279dSPiotr Zegar // CHECK-MESSAGES: :[[@LINE-1]]:39: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
34*28c1279dSPiotr Zegar something_str(sv.data(), sv.size());
35*28c1279dSPiotr Zegar // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
36*28c1279dSPiotr Zegar View view{sv.data()};
37*28c1279dSPiotr Zegar // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: result of a `data()` call may not be null terminated, provide size information to the callee to prevent potential issues
38*28c1279dSPiotr Zegar }
39*28c1279dSPiotr Zegar
valid(std::string_view sv)40*28c1279dSPiotr Zegar void valid(std::string_view sv) {
41*28c1279dSPiotr Zegar std::string s1(sv.data(), sv.data() + sv.size());
42*28c1279dSPiotr Zegar std::string s2(sv.data(), sv.data() + sv.length());
43*28c1279dSPiotr Zegar std::string s3(sv.data(), sv.size() + sv.data());
44*28c1279dSPiotr Zegar std::string s4(sv.data(), sv.length() + sv.data());
45*28c1279dSPiotr Zegar std::string s5(sv.data(), sv.size());
46*28c1279dSPiotr Zegar std::string s6(sv.data(), sv.length());
47*28c1279dSPiotr Zegar something(sv.data(), sv.size());
48*28c1279dSPiotr Zegar something(sv.data(), sv.length());
49*28c1279dSPiotr Zegar ViewWithSize view1{sv.data(), sv.size()};
50*28c1279dSPiotr Zegar ViewWithSize view2{sv.data(), sv.length()};
51*28c1279dSPiotr Zegar Pair view3{sv.data(), sv.data() + sv.size()};
52*28c1279dSPiotr Zegar Pair view4{sv.data(), sv.data() + sv.length()};
53*28c1279dSPiotr Zegar Pair view5{sv.data(), sv.size() + sv.data()};
54*28c1279dSPiotr Zegar Pair view6{sv.data(), sv.length() + sv.data()};
55*28c1279dSPiotr Zegar const char* str{sv.data()};
56*28c1279dSPiotr Zegar }
57