-
Notifications
You must be signed in to change notification settings - Fork 14.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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5825b32
[-Wunterminated-string-initialization] Handle C string literals endin…
ziqingluo-90 afb9093
address comments
ziqingluo-90 fca602a
add release notes
ziqingluo-90 6cfbbb3
remove '#include <stddef.h>'
ziqingluo-90 ebb8574
fix test
ziqingluo-90 40d6543
fix test
ziqingluo-90 5239320
address comments
ziqingluo-90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -verify=compat,expected -Wunterminated-string-initialization %s -x c | ||
// RUN: %clang_cc1 -fsyntax-only -verify=cxx -Wunterminated-string-initialization %s -x c++ | ||
|
||
#ifndef __cplusplus | ||
typedef unsigned short char16_t; | ||
typedef unsigned int char32_t; | ||
typedef __WCHAR_TYPE__ wchar_t; | ||
#endif | ||
|
||
// C++ is stricter so the following cases should be warned about. In | ||
// C, the following examples are fine. | ||
|
||
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)}} | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic warning "-Wc++-compat" | ||
// Test different encodings: | ||
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)}} \ | ||
compat-warning {{initializer-string for character array is too long for C++, 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)}} \ | ||
compat-warning {{initializer-string for character array is too long for C++, 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)}} \ | ||
compat-warning {{initializer-string for character array is too long for C++, array size is 3 but initializer has size 4 (including the null terminating character)}} \ | ||
compat-warning {{identifier 'wchar_t' conflicts with a C++ keyword}} | ||
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)}} \ | ||
compat-warning {{initializer-string for character array is too long for C++, array size is 3 but initializer has size 4 (including the null terminating character)}} \ | ||
compat-warning {{identifier 'char16_t' conflicts with a C++ keyword}} | ||
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)}} \ | ||
compat-warning {{initializer-string for character array is too long for C++, array size is 3 but initializer has size 4 (including the null terminating character)}} \ | ||
compat-warning {{identifier 'char32_t' conflicts with a C++ keyword}} | ||
#pragma clang diagnostic pop | ||
|
||
// 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?}} \ | ||
cxx-error {{initializer-string for char array is too long, array size is 0 but initializer has size 1 (including the null terminating character)}} | ||
char b[1] = ""; // no warn |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.