public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Pinski <pinskia@gmail.com>
To: Alejandro Colomar <alx.manpages@gmail.com>
Cc: gcc@gcc.gnu.org
Subject: Re: [whish] -Wunterminated-string-initialization: new warning
Date: Sun, 13 Nov 2022 14:12:22 -0800	[thread overview]
Message-ID: <CA+=Sn1nwa9Sbo5_E8gaHfg9_-e71Y=w_pz8bQATTWxTb53QVQw@mail.gmail.com> (raw)
In-Reply-To: <36da94eb-1cac-5ae8-7fea-ec66160cf413@gmail.com>

On Sun, Nov 13, 2022 at 1:57 PM Alejandro Colomar via Gcc
<gcc@gcc.gnu.org> wrote:
>
> Hi!
>
> I'd like to get warnings if I write the following code:
>
> char foo[3] = "foo";

This should be easy to add as it is already part of the -Wc++-compat
option as for C++ it is invalid code.

<source>:2:19: warning: initializer-string for array of 'char' is too long
    2 | char     two[2] = "foo";   // 'f' 'o'
      |                   ^~~~~
<source>:3:19: warning: initializer-string for array of 'char' is too
long for C++ [-Wc++-compat]
    3 | char   three[3] = "foo";   // 'f' 'o' 'o'
      |                   ^~~~~


... (for your more complex case [though I needed to modify one of the
strings to exactly 8]

<source>:5:7: warning: initializer-string for array of 'char' is too
long for C++ [-Wc++-compat]
    5 |       "01234567",
      |       ^~~~~~~~~~

              else if (warn_cxx_compat
                       && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
                warning_at (init_loc, OPT_Wc___compat,
                            ("initializer-string for array of %qT "
                             "is too long for C++"), typ1);

That is the current code which does this warning even so it is just a
matter of adding an option to c-family/c.opt and then having
c++-compat enable it and using that new option here.

Thanks,
Andrew Pinski

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

  reply	other threads:[~2022-11-13 22:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-13 21:56 Alejandro Colomar
2022-11-13 22:12 ` Andrew Pinski [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+=Sn1nwa9Sbo5_E8gaHfg9_-e71Y=w_pz8bQATTWxTb53QVQw@mail.gmail.com' \
    --to=pinskia@gmail.com \
    --cc=alx.manpages@gmail.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).