aboutsummaryrefslogtreecommitdiff
path: root/gcc/doc
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@kernel.org>2024-06-29 15:10:43 +0200
committerMartin Uecker <uecker@gcc.gnu.org>2024-07-14 11:41:00 +0200
commit44c9403ed1833ae71a59e84f9e37af3182be0df5 (patch)
treebf0649f1806bd7eb07f29516ca64e58ed894bef4 /gcc/doc
parent74595c778335f6c512dc38d310353dfc32c7ea95 (diff)
downloadgcc-44c9403ed1833ae71a59e84f9e37af3182be0df5.zip
gcc-44c9403ed1833ae71a59e84f9e37af3182be0df5.tar.gz
gcc-44c9403ed1833ae71a59e84f9e37af3182be0df5.tar.bz2
c, objc: Add -Wunterminated-string-initialization
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. Since Wc++-compat now includes this warning, the test has to be modified to expect the text of the new warning too, in <gcc.dg/Wcxx-compat-14.c>. 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-ec66160cf413@gmail.com/T/ PR c/115185 gcc/c-family/ChangeLog: * c.opt: Add -Wunterminated-string-initialization. gcc/c/ChangeLog: * c-typeck.cc (digest_init): Separate warnings about character arrays being initialized as unterminated character sequences with string literals, from -Wc++-compat, into a new warning, -Wunterminated-string-initialization. gcc/ChangeLog: * doc/invoke.texi: Document the new -Wunterminated-string-initialization. gcc/testsuite/ChangeLog: * gcc.dg/Wcxx-compat-14.c: Adapt the test to match the new text of the warning, which doesn't say anything about C++ anymore. * gcc.dg/Wunterminated-string-initialization.c: New test. Acked-by: Doug McIlroy <douglas.mcilroy@dartmouth.edu> Acked-by: Mike Stump <mikestump@comcast.net> Reviewed-by: Sandra Loosemore <sloosemore@baylibre.com> Reviewed-by: Martin Uecker <uecker@tugraz.at> Signed-off-by: Alejandro Colomar <alx@kernel.org> Reviewed-by: Marek Polacek <polacek@redhat.com>
Diffstat (limited to 'gcc/doc')
-rw-r--r--gcc/doc/invoke.texi20
1 files changed, 19 insertions, 1 deletions
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index d544984..403ea9d 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -415,7 +415,9 @@ Objective-C and Objective-C++ Dialects}.
-Wsystem-headers -Wtautological-compare -Wtrampolines -Wtrigraphs
-Wtrivial-auto-var-init -Wno-tsan -Wtype-limits -Wundef
-Wuninitialized -Wunknown-pragmas
--Wunsuffixed-float-constants -Wunused
+-Wunsuffixed-float-constants
+-Wunterminated-string-initialization
+-Wunused
-Wunused-but-set-parameter -Wunused-but-set-variable
-Wunused-const-variable -Wunused-const-variable=@var{n}
-Wunused-function -Wunused-label -Wunused-local-typedefs
@@ -6496,6 +6498,7 @@ name is still supported, but the newer name is more descriptive.)
-Wstring-compare
-Wtype-limits
-Wuninitialized
+-Wunterminated-string-initialization
-Wunused-parameter @r{(only with} @option{-Wunused} @r{or} @option{-Wall}@r{)}
-Wunused-but-set-parameter @r{(only with} @option{-Wunused} @r{or} @option{-Wall}@r{)}}
@@ -8664,6 +8667,21 @@ arithmetic that may yield out of bounds values. This warning level may
give a larger number of false positives and is deactivated by default.
@end table
+@opindex Wunterminated-string-initialization
+@opindex Wno-unterminated-string-initialization
+@item -Wunterminated-string-initialization @r{(C and Objective-C only)}
+Warn about character arrays
+initialized as unterminated character sequences
+with a string literal.
+For example:
+
+@smallexample
+char arr[3] = "foo";
+@end smallexample
+
+This warning is enabled by @option{-Wextra} and @option{-Wc++-compat}.
+In C++, such initializations are an error.
+
@opindex Warray-compare
@opindex Wno-array-compare
@item -Warray-compare