On Fri, 2023-03-24 at 14:39 +0100, Alejandro Colomar via Gcc-patches wrote: > Warn about the following: > > char s[3] = "foo"; > > Initializing a char array with a string literal of the same length as > the size of the array is usually a mistake. Rarely is the case where > one wants to create a non-terminated character sequence from a string > literal. > > In some cases, for writing faster code, one may want to use arrays > instead of pointers, since that removes the need for storing an array > of > pointers apart from the strings themselves. > > char *log_levels[] = { "info", "warning", "err" }; > vs. > char log_levels[][7] = { "info", "warning", "err" }; > > This forces the programmer to specify a size, which might change if a > new entry is later added. Having no way to enforce null termination > is > very dangerous, however, so it is useful to have a warning for this, > so > that the compiler can make sure that the programmer didn't make any > mistakes. This warning catches the bug above, so that the programmer > will be able to fix it and write: > > char log_levels[][8] = { "info", "warning", "err" }; > > This warning already existed as part of -Wc++-compat, but this patch > allows enabling it separately. It is also included in -Wextra, since > it may not always be desired (when unterminated character sequences > are > wanted), but it's likely to be desired in most cases. > > Link: > <https://lists.gnu.org/archive/html/groff/2022-11/msg00059.html> > Link: > <https://lists.gnu.org/archive/html/groff/2022-11/msg00063.html> > Link: > <https://inbox.sourceware.org/gcc/36da94eb-1cac-5ae8-7fea-ec66160cf41 > 3...@gmail.com/T/> > Acked-by: Doug McIlroy <douglas.mcil...@dartmouth.edu> > Cc: "G. Branden Robinson" <g.branden.robin...@gmail.com> > Cc: Ralph Corderoy <ra...@inputplus.co.uk> > Cc: Dave Kemper <saint.s...@gmail.com> > Cc: Larry McVoy <l...@mcvoy.com> > Cc: Andrew Pinski <pins...@gmail.com> > Cc: Jonathan Wakely <jwakely....@gmail.com> > Cc: Andrew Clayton <and...@digital-domain.net> > Cc: Martin Uecker <muec...@gwdg.de> > Signed-off-by: Alejandro Colomar <a...@kernel.org> > --- > > Hi,
Hi Alex, thanks for the patch. > > I sent v1 to the wrong list. This time I've made sure to write to > gcc-patches@. Note that right now we're deep in bug-fixing/stabilization for GCC 13 (and trunk is still tracking that effort), so your patch might be more suitable for GCC 14. > > v2 adds some draft of a test, as suggested by Martin. However, I > don't > know yet how to write those, so the test is just a draft. But I did > test the feature, by compiling GCC and compiling some small program > with > it. Unfortunately the answer to the question "how do I run just one testcase in GCC's testsuite" is rather non-trivial; FWIW I've written up some notes on working with the GCC testsuite here: https://gcc-newbies-guide.readthedocs.io/en/latest/working-with-the-testsuite.html Hope this is helpful Dave [...snip...]