[Bug c/115185] Missing "too long" warning when string-array size doesn't include NULL byte
slyfox at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Mon Jul 15 20:57:50 GMT 2024
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115185
Sergei Trofimovich <slyfox at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |ldv at sourceware dot org,
| |mark at gcc dot gnu.org,
| |slyfox at gcc dot gnu.org
--- Comment #14 from Sergei Trofimovich <slyfox at gcc dot gnu.org> ---
This generates warning on reasonably looking code.
On strace-6.9: https://github.com/strace/strace/blob/v6.9/src/print_utils.h#L16
In file included from v4l2.c:40:
print_utils.h:16:35: error: initializer-string for array of 'char' is too
long [-Werror=unterminated-string-initialization]
16 | static const char hex_chars[16] = "0123456789abcdef";
| ^~~~~~~~~~~~~~~~~~
/cc Dmitry
On elfutils-0.191:
https://sourceware.org/git/?p=elfutils.git;a=blob;f=backends/x86_64_regs.c;h=ef987daf43f01187c468d29e4d412ecafdcb2878;hb=18a015c0b0787ba5acb39801ab7c17dac50f584d#l85
x86_64_regs.c: In function 'x86_64_register_info':
x86_64_regs.c:85:11: error: initializer-string for array of 'char' is too
long [-Werror=unterminated-string-initialization]
85 | "ax", "dx", "cx", "bx", "si", "di", "bp", "sp
| ^~~~
/cc Mark
I would say both uses are reasonable even if they strip trailing '\0'.
Warning-free code that does the equivalent might be a bit ugly:
static const char hex_chars[17] = "0123456789abcdef"; // extra byte
or
static const char hex_chars[16] = { '0', '1', ..., 'f' }; // lots of
boilerplate
WDYT of relaxing gcc warning a bit and not complain if the only thing it strips
is a '\0'? Otherwise at least strace and elfutils would have to adapt.
More information about the Gcc-bugs
mailing list