[clang] [-Wunterminated-string-initialization] Handle C string literals ending with explicit '\0' (PR #143487)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 10 07:48:26 PDT 2025


================
@@ -260,6 +260,11 @@ static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT,
              diag::ext_initializer_string_for_char_array_too_long)
           << Str->getSourceRange();
     else if (StrLength - 1 == ArrayLen) {
+      // If the string literal is null-terminated explicitly, e.g., `char a[4] =
+      // "ABC\0"`, there should be no warn:
+      if (const auto *SL = dyn_cast<StringLiteral>(Str->IgnoreParens()))
+        if (SL->isOrdinary() && SL->getBytes().back() == 0)
----------------
AaronBallman wrote:

Also, this means the functionality only works for `char` and not other string types. I would imagine we'd want to also support: `wchar_t foo[4] = { L'f', L'o', L'o', L'\0' };` and other variants.

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


More information about the cfe-commits mailing list