You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[-Wunsafe-buffer-usage] Warn string_view constructions that not guarantee null-termination
One could use `string_view` as the safe view over strings in read-only
scenarios as it is hardened, preserving bounds info and guaranteeing
null-termination with the new warning.
If one finds the warning being too restrictive, turn it off with
`-Wno-unsafe-buffer-usage-in-string-view`. The rest of the warnings
will not assume null-termination guarantee of `string_view` as well
then.
foo(p); // expected-warning{{construct string_view from raw pointers does not guarantee null-termination, construct from std::string instead}}
51
+
#else
52
+
foo(p); // no warn
53
+
#endif
54
+
#ifndef _IGNORE_STRING_VIEW
55
+
foo({p, 10}); // expected-warning{{construct string_view from raw pointers does not guarantee null-termination, construct from std::string instead}}
56
+
#else
57
+
foo({p, 10}); // no warn
58
+
#endif
59
+
60
+
std::string_view SV{S};
61
+
std::string_view SV2{"S"};
62
+
std::string_view SV3 = S;
63
+
std::string_view SV4 = "S";
64
+
std::string_view SV5 = std::string_view(S);
65
+
#ifndef _IGNORE_STRING_VIEW
66
+
std::string_view SV10 = p; // expected-warning{{construct string_view from raw pointers does not guarantee null-termination, construct from std::string instead}}
67
+
#else
68
+
std::string_view SV10 = p; // no warn
69
+
#endif
70
+
#ifndef _IGNORE_STRING_VIEW
71
+
std::string_view SV11{p}; // expected-warning{{construct string_view from raw pointers does not guarantee null-termination, construct from std::string instead}}
72
+
#else
73
+
std::string_view SV11{p}; // no warn
74
+
#endif
75
+
#ifndef _IGNORE_STRING_VIEW
76
+
std::string_view SV12{V.begin(), V.end()}; // expected-warning{{construct string_view from raw pointers does not guarantee null-termination, construct from std::string instead}}
77
+
#else
78
+
std::string_view SV12{V.begin(), V.end()}; // no warn
79
+
#endif
80
+
}
81
+
82
+
voidg(int * p) {
83
+
// This warning is not affected:
84
+
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}}
0 commit comments