[clang] [-Wunterminated-string-initialization] Handle C string literals ending with explicit '\0' (PR #143487)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 10 06:22:10 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)
----------------
erichkeane wrote:
Is this encoding-aware? what about if this is a 2nd byte in a multi-byte?
https://github.com/llvm/llvm-project/pull/143487
More information about the cfe-commits
mailing list