[whish] -Wunterminated-string-initialization: new warning
Alejandro Colomar
alx.manpages@gmail.com
Sun Nov 13 21:56:07 GMT 2022
Hi!
I'd like to get warnings if I write the following code:
char foo[3] = "foo";
It's hard to keep track of sizes to make sure that the string literals always
initialize to terminated strings. It seems something that should be easy to
implement in the compiler.
A morecomplex case where it's harder to keep track of sizes is:
static const char log_levels[][8] = {
"alert",
"error",
"warn",
"notice",
"info",
"debug",
};
Here, 8 works now (and 7 too, but for aligmnent reasons I chose 8). If tomorrow
we add or change an entry, It'll be hard to keep it safe. Such a warning would
help a lot.
An example program is:
$ cat str.c
char two[2] = "foo"; // 'f' 'o'
char three[3] = "foo"; // 'f' 'o' 'o'
char four[4] = "foo"; // 'f' 'o' 'o' '\0'
char five[5] = "foo"; // 'f' 'o' 'o' '\0' '\0'
char implicit[] = "foo"; // 'f' 'o' 'o' '\0'
$ cc -Wall -Wextra str.c
str.c:1:19: warning: initializer-string for array of ‘char’ is too long
1 | char two[2] = "foo"; // 'f' 'o'
| ^~~~~
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o:
in function `_start':
(.text+0x17): undefined reference to `main'
collect2: error: ld returned 1 exit status
Here, I'd like that with the new warning, 'three' would also get warned.
Cheers,
Alex
--
<http://www.alejandro-colomar.es/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20221113/58b69a19/attachment.sig>
More information about the Gcc
mailing list