[clang-tools-extra] [clang-tidy] Also check string_view in bugprone-string-literal-with-embedded-nul (PR #234722)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 22 11:57:10 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: maflcko

<details>
<summary>Changes</summary>

Currently, the check will only consider basic string constructors and not string view constructors. So detection of a bug can be missed.

Fix it by checking basic string views as well.

---
Full diff: https://github.com/llvm/llvm-project/pull/234722.diff


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp (+10-8) 
- (modified) clang-tools-extra/docs/ReleaseNotes.md (+4) 
- (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/string-literal-with-embedded-nul.cpp (+34-1) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp
index b3e0673ea61034..95640e252e490e 100644
--- a/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/StringLiteralWithEmbeddedNulCheck.cpp
@@ -36,14 +36,16 @@ void StringLiteralWithEmbeddedNulCheck::registerMatchers(MatchFinder *Finder) {
       ignoringParenImpCasts(stringLiteral(containsNul()).bind("truncated"));
 
   // Match string constructor.
-  const auto StringConstructorExpr = expr(anyOf(
-      cxxConstructExpr(argumentCountIs(1),
-                       hasDeclaration(cxxMethodDecl(hasName("basic_string")))),
-      // If present, the second argument is the alloc object which must not
-      // be present explicitly.
-      cxxConstructExpr(argumentCountIs(2),
-                       hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
-                       hasArgument(1, cxxDefaultArgExpr()))));
+  const auto StringConstructorExpr =
+      expr(anyOf(cxxConstructExpr(argumentCountIs(1),
+                                  hasDeclaration(cxxMethodDecl(hasAnyName(
+                                      "basic_string", "basic_string_view")))),
+                 // If present, the second argument is the alloc object which
+                 // must not be present explicitly.
+                 cxxConstructExpr(argumentCountIs(2),
+                                  hasDeclaration(cxxMethodDecl(hasAnyName(
+                                      "basic_string", "basic_string_view"))),
+                                  hasArgument(1, cxxDefaultArgExpr()))));
 
   // Detect passing a suspicious string literal to a string constructor.
   // example: std::string str = "abc\0def";
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 57ed93042aff2c..399f4b6357e091 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -226,6 +226,10 @@ infrastructure are described first, followed by tool-specific sections.
   <clang-tidy/checks/bugprone/std-namespace-modification>` when checking
   lambda closure types used as template arguments.
 
+- Improved {doc}`bugprone-string-literal-with-embedded-nul
+  <clang-tidy/checks/bugprone/string-literal-with-embedded-nul>` to also check
+  `std::basic_string_view` construction from string literals with embedded NULs.
+
 - Improved {doc}`bugprone-unchecked-optional-access
   <clang-tidy/checks/bugprone/unchecked-optional-access>` by fixing false
   positives on `bsl::optional` and `bdlb::NullableValue` constructed from a
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/string-literal-with-embedded-nul.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/string-literal-with-embedded-nul.cpp
index f939f5282eb11d..52f389bdbeb2c8 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/string-literal-with-embedded-nul.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/string-literal-with-embedded-nul.cpp
@@ -1,6 +1,8 @@
-// RUN: %check_clang_tidy %s bugprone-string-literal-with-embedded-nul %t
+// RUN: %check_clang_tidy %s bugprone-string-literal-with-embedded-nul -std=c++11,c++14 %t
+// RUN: %check_clang_tidy %s bugprone-string-literal-with-embedded-nul -check-suffix=,CXX17 -std=c++17-or-later %t
 
 namespace std {
+using size_t = unsigned long;
 template <typename T>
 class allocator {};
 template <typename T>
@@ -10,17 +12,27 @@ struct basic_string {
   typedef basic_string<C, T, A> _Type;
   basic_string();
   basic_string(const C *p, const A &a = A());
+  basic_string(const C *p, size_t n);
 
   _Type& operator+=(const C* s);
   _Type& operator=(const C* s);
 };
 
+template <typename C, typename T>
+struct basic_string_view {
+  basic_string_view(const C *p);
+  basic_string_view(const C *p, size_t n);
+};
+
 typedef basic_string<char, std::char_traits<char>, std::allocator<char>> string;
 typedef basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>> wstring;
+typedef basic_string_view<char, std::char_traits<char>> string_view;
+typedef basic_string_view<wchar_t, std::char_traits<wchar_t>> wstring_view;
 }
 
 bool operator==(const std::string&, const char*);
 bool operator==(const char*, const std::string&);
+bool operator==(const std::string_view&, const std::string_view&);
 
 
 const char Valid[] = "This is valid \x12.";
@@ -60,6 +72,7 @@ void TestA() {
   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: truncated string literal
   std::string str4{"\x00\x01\x02\x03"};
   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: truncated string literal
+  std::string str_ok{"abc\0def", 7};
 
   std::string str;
   str += "abc\0def";
@@ -83,3 +96,23 @@ void TestW() {
   std::wstring str4{L"\x00\x01\x02\x03"};
   // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: truncated string literal
 }
+
+#if __cplusplus >= 201703L
+void TestView() {
+  std::string_view str1 = "abc\0def";
+  // CHECK-MESSAGES-CXX17: :[[@LINE-1]]:27: warning: truncated string literal
+  std::string_view str2{"abc\0def"};
+  // CHECK-MESSAGES-CXX17: :[[@LINE-1]]:25: warning: truncated string literal
+  std::string_view str_ok{"abc\0def", 7};
+
+  std::string_view view{"abc"};
+  if (view == "abc\0def") return;
+  // CHECK-MESSAGES-CXX17: :[[@LINE-1]]:15: warning: truncated string literal
+  if ("abc\0def" == view) return;
+  // CHECK-MESSAGES-CXX17: :[[@LINE-1]]:7: warning: truncated string literal
+
+  std::wstring_view wstr1 = L"abc\0def";
+  // CHECK-MESSAGES-CXX17: :[[@LINE-1]]:29: warning: truncated string literal
+  std::wstring_view wstr2{L"abc\0def", 7};
+}
+#endif

``````````

</details>


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


More information about the cfe-commits mailing list