-
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 6 commits
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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // RUN: %clang_cc1 -fsyntax-only -verify -Wunterminated-string-initialization %s -x c | ||
| // RUN: %clang_cc1 -fsyntax-only -verify=cxx,expected -Wunterminated-string-initialization %s -x c++ | ||
|
|
||
|
|
||
| #ifdef __cplusplus | ||
| // C++ is stricter so the following cases should be warned about: | ||
|
||
|
|
||
| char foo3[3] = "fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} | ||
| char foo1[1] = "\0"; // cxx-error {{initializer-string for char array is too long, array size is 1 but initializer has size 2 (including the null terminating character)}} | ||
|
|
||
| struct S { | ||
| char buf[3]; | ||
| char fub[3]; | ||
| } s = { "ba\0", "bo\0" }; // cxx-error 2{{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} | ||
|
|
||
AaronBallman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| signed char scfoo[3] = "fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} | ||
| unsigned char ucfoo[3] = "fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} | ||
| wchar_t wcfoo[3] = L"fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} | ||
| char16_t c16foo[3] = u"fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} | ||
| char32_t c32foo[3] = U"fo\0"; // cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}} | ||
| #else | ||
|
|
||
| // In C, the following examples are fine: | ||
| typedef unsigned short char16_t; | ||
| typedef unsigned int char32_t; | ||
| typedef __WCHAR_TYPE__ wchar_t; | ||
|
|
||
| char foo3[3] = "fo\0"; | ||
| char foo1[1] = "\0"; | ||
|
|
||
| struct S { | ||
| char buf[3]; | ||
| char fub[3]; | ||
| } s = { "ba\0", "bo\0" }; | ||
|
|
||
| // Test different encodings: | ||
| signed char scfoo[3] = "fo\0"; | ||
| unsigned char ucfoo[3] = "fo\0"; | ||
| wchar_t wcfoo[3] = L"fo\0"; | ||
| char16_t c16foo[3] = u"fo\0"; | ||
| char32_t c32foo[3] = U"fo\0"; | ||
|
|
||
| // Test list initializer: | ||
| signed char scfoo_lst[3] = {'f', 'o', '\0'}; | ||
| unsigned char ucfoo_lst[3] = {'f', 'o', '\0'}; | ||
| wchar_t wcfoo_lst[3] = {L'f', L'o', L'\0'}; | ||
| char16_t c16foo_lst[3] = {u'f', u'o', u'\0'}; | ||
| char32_t c32foo_lst[3] = {U'f', U'o', U'\0'}; | ||
|
|
||
| // Declaring an array of size 0 is invalid by C standard but compilers | ||
| // may allow it: | ||
| char a[0] = ""; // expected-warning {{initializer-string for character array is too long, array size is 0 but initializer has size 1 (including the null terminating character); did you mean to use the 'nonstring' attribute?}} | ||
| #endif | ||
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.
I think the early return suppresses warn_initializer_string_for_char_array_too_long_for_cpp warnings?