https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116082

--- Comment #8 from Alejandro Colomar <alx at kernel dot org> ---
(In reply to Harald van Dijk from comment #7)
> (In reply to uecker from comment #6)
> > The warning is about truncation of the final NUL
> > character in the string literal. So I think the warning is correct. It does
> > not warn about the result not being NUL-terminated, but about truncating a
> > NUL from the initializer.
> 
> The warning is called -Wunterminated-string-initialization, but there is no
> unterminated string. That is what this bug is about. If people want a
> warning for truncated data, there should be a new warning for that, making
> -Wunterminated-string-initialization do something other than what its name
> clearly says it does is not the right way of handling that.

Not only there's no _unterminated string_; there's no _string_ at all.  That
string literal is not a string.

<https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#Hfootnote.75>

Quoting C23:

| 75)  A string literal may not be a string (see 7.1.1),
| because a null character can be embedded in it by a \0 escape sequence.


So, if you're using a non-string to initialize a string, you deserve a
diagnostic.  If you're being too clever about it, you should turn off the
diagnostic, IMO.  My recommendation, like Martin said, is that you remove that
extra \0 and rely on this diagnostic for making sure that there's a NUL
terminator.

Reply via email to