@@ -2592,10 +2592,14 @@ String:
p = alloc_expression(e->pos, EXPR_STRING);
*p = *e;
type = evaluate_expression(p);
- if (ctype->bit_size != -1 &&
- ctype->bit_size + bits_in_char < type->bit_size) {
- warning(e->pos,
- "too long initializer-string for array of char");
+ if (ctype->bit_size != -1) {
+ if (ctype->bit_size + bits_in_char < type->bit_size)
+ warning(e->pos,
+ "too long initializer-string for array of char");
+ else if (Winit_cstring && ctype->bit_size + bits_in_char == type->bit_size) {
+ warning(e->pos,
+ "too long initializer-string for array of char(no space for nul char)");
+ }
}
*ep = p;
return 1;
@@ -199,6 +199,7 @@ int Wdecl = 1;
int Wdefault_bitfield_sign = 0;
int Wdesignated_init = 1;
int Wdo_while = 0;
+int Winit_cstring = 0;
int Wenum_mismatch = 1;
int Wnon_pointer_null = 1;
int Wold_initializer = 1;
@@ -410,6 +411,7 @@ static const struct warning {
{ "designated-init", &Wdesignated_init },
{ "do-while", &Wdo_while },
{ "enum-mismatch", &Wenum_mismatch },
+ { "init-cstring", &Winit_cstring },
{ "non-pointer-null", &Wnon_pointer_null },
{ "old-initializer", &Wold_initializer },
{ "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
@@ -95,6 +95,7 @@ extern int Wdefault_bitfield_sign;
extern int Wdesignated_init;
extern int Wdo_while;
extern int Wenum_mismatch;
+extern int Winit_cstring;
extern int Wnon_pointer_null;
extern int Wold_initializer;
extern int Wone_bit_signed_bitfield;
This patch adds new option -Winit-cstring to sparse. With the option sparse can Warn about initialization of a char array with a too long constant C string. If the size of the char array and the length of the string is the same, there is no space for the last nul char of the string in the array. char s[3] = "abc"; If the array is used as just a byte array, not as C string, this warning is just noise. However, if the array is passed to functions dealing with C string like printf(%s) and strcmp, it may cause a trouble. Here is a example of such trouble: http://www.spinics.net/lists/netdev/msg229765.html http://www.spinics.net/lists/netdev/msg229870.html Signed-off-by: Masatake YAMATO <yamato@redhat.com> --- evaluate.c | 12 ++++++++---- lib.c | 2 ++ lib.h | 1 + 3 files changed, 11 insertions(+), 4 deletions(-)