[clang] [Clang][UnsafeBufferUsage] Warn about two-arg string_view constructors. (PR #180471)

Rohan Jacob-Rao via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 10 10:47:11 PST 2026


================
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -verify %s
+
+namespace std {
+  template <typename T>
+  class basic_string_view {
+  public:
+    basic_string_view(T*, int);
+    basic_string_view(T*);
+  };
+  
+  using string_view = basic_string_view<char>;
+}
+
+void test(char *ptr, int size) {
+  // CASE 1: Unsafe (Ptr + Size) -> Should Warn
+  std::string_view sv1(ptr, size); // expected-warning{{the two-parameter std::string_view construction is unsafe as it can introduce mismatch between buffer size and the bound information}}
+  // CASE 2: Safe (Ptr only) -> Should NOT Warn
+  std::string_view sv2(ptr);
----------------
rohanjr wrote:

Should we test the [other string_view constructors](https://en.cppreference.com/w/cpp/string/basic_string_view/basic_string_view.html)? I'm specifically wondering about the `basic_string_view(It first, End last)`, which also has 2 parameters. Do we consider it safe?

https://github.com/llvm/llvm-project/pull/180471


More information about the cfe-commits mailing list