-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[-Wunterminated-string-initialization] Handle C string literals ending with explicit '\0' #143487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
5825b32
afb9093
fca602a
6cfbbb3
ebb8574
40d6543
5239320
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 (const auto *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) | |
| if (const auto *SL = dyn_cast<StringLiteral>(Str->IgnoreParens()); SL && SL->isOrdinary() && SL->getBytes().back() == 0) |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this encoding-aware? what about if this is a 2nd byte in a multi-byte?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,28 @@ | ||||||
| // RUN: %clang_cc1 -fsyntax-only -verify -Wunterminated-string-initialization %s -x c | ||||||
| // RUN: %clang_cc1 -fsyntax-only -verify -Wunterminated-string-initialization %s -x c++ | ||||||
|
|
||||||
|
|
||||||
| // In C, the following examples are fine: | ||||||
| #if __cplusplus | ||||||
|
||||||
| #if __cplusplus | |
| #ifdef __cplusplus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.