public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* [whish] -Wunterminated-string-initialization: new warning
@ 2022-11-13 21:56 Alejandro Colomar
  2022-11-13 22:12 ` Andrew Pinski
  0 siblings, 1 reply; 6+ messages in thread
From: Alejandro Colomar @ 2022-11-13 21:56 UTC (permalink / raw)
  To: gcc


[-- Attachment #1.1: Type: text/plain, Size: 1453 bytes --]

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/>

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-11-14 13:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-13 21:56 [whish] -Wunterminated-string-initialization: new warning Alejandro Colomar
2022-11-13 22:12 ` Andrew Pinski
2022-11-14 11:37   ` Alejandro Colomar
2022-11-14 13:14     ` Jonathan Wakely
2022-11-14 13:41       ` unterminated string literals (was: [whish] -Wunterminated-string-initialization: new warning) Alejandro Colomar
2022-11-14 13:46       ` [whish] -Wunterminated-string-initialization: new warning Alejandro Colomar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).