You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[C] Add diagnostic + attr for unterminated strings (#137829)
This introduces three things related to intialization like:
char buf[3] = "foo";
where the array does not declare enough space for the null terminator
but otherwise can represent the array contents exactly.
1) An attribute named 'nonstring' which can be used to mark that a
field or variable is not intended to hold string data.
2) -Wunterminated-string-initialization, which is grouped under
-Wextra, and diagnoses the above construct unless the declaration
uses the 'nonstring' attribute.
3) -Wc++-unterminated-string-initialization, which is grouped under
-Wc++-compat, and diagnoses the above construct even if the
declaration uses the 'nonstring' attribute.
Fixes#137705
charfoo[3] ="foo"; // no-nonstring-warning {{initializer-string for character array is too long, array size is 3 but initializer has size 4 (including the null terminating character); did you mean to use the 'nonstring' attribute?}} \
10
+
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)}} \
11
+
cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}}
12
+
__attribute__((nonstring)) charbar[3] ="bar"; // 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)}} \
13
+
cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}}
14
+
15
+
structS {
16
+
charbuf[3];
17
+
__attribute__((nonstring)) charfub[3];
18
+
} s= { "baz", "bop" }; // no-nonstring-warning {{initializer-string for character array is too long, array size is 3 but initializer has size 4 (including the null terminating character); did you mean to use the 'nonstring' attribute?}} \
19
+
compat-warning 2 {{initializer-string for character array is too long for C++, array size is 3 but initializer has size 4 (including the null terminating character)}} \
20
+
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)}}
21
+
22
+
// Show that we also issue the diagnostic for the other character types as well.
23
+
signed charscfoo[3] ="foo"; // no-nonstring-warning {{initializer-string for character array is too long, array size is 3 but initializer has size 4 (including the null terminating character); did you mean to use the 'nonstring' attribute?}} \
24
+
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)}} \
25
+
cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}}
26
+
unsigned charucfoo[3] ="foo"; // no-nonstring-warning {{initializer-string for character array is too long, array size is 3 but initializer has size 4 (including the null terminating character); did you mean to use the 'nonstring' attribute?}} \
27
+
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)}} \
28
+
cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 4 (including the null terminating character)}}
29
+
30
+
// wchar_t (et al) are a bit funny in that it cannot do string init in C++ at
31
+
// all, so they are not tested.
32
+
33
+
// Show that we properly diagnose incorrect uses of nonstring.
34
+
__attribute__((nonstring)) voidfunc(void); // expected-warning {{'nonstring' attribute only applies to variables and non-static data members}}
35
+
__attribute__((nonstring("test"))) chareek1[2]; // expected-error {{'nonstring' attribute takes no arguments}}
36
+
__attribute__((nonstring)) inteek2; // expected-warning {{'nonstring' attribute only applies to fields or variables of character array type; type is 'int'}}
37
+
38
+
// Note, these diagnostics are separate from the "too many initializers"
39
+
// diagnostic when you overwrite more than just the null terminator.
40
+
chartoo_big[3] ="this is too big"; // noncxx-warning {{initializer-string for char array is too long}} \
41
+
cxx-error {{initializer-string for char array is too long, array size is 3 but initializer has size 16 (including the null terminating character)}}
0 commit comments