[Bug c/61579] -Wwrite-strings does not behave as a warning option

manu at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Jul 22 13:42:46 GMT 2020


--- Comment #8 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
(In reply to David Brown from comment #7)
> Could "-Wwrite-strings" be split into two options?  The warning could remain
> (and become part of -Wall for C as well as C++) if the compiler can spot and
> warn about attempts to write to string literals, while keeping these of type
> "char[len]" as required by C.

I think the issue is that the warning is implemented by changing the type of
string literals and then using the already existing warning for passing const
char* to char*. Someone would need to reimplement the warning to explicitly
detect string literals instead of changing their type, probably somewhere in
this function:

> A new option "-fconst-strings" could be put under "Code Gen Options" which
> makes C string literals be type "const char[len]" for those that want it,
> encouraging a slightly safer code style that is not standard C.

I am not sure this is a good solution, since the only benefit of
-fconst-strings would be to trigger the warnings. I'm pretty sure it will not
affect optimization, so it is useless as a codegen option. It doesn't solve all
the issues described in comment #0.

The fix is the one outlined above: detect that the argument or assigment is a
literal string and special case the "discarding const qualifier" to trigger in
that case with -Write-strings.

But, the only way to find out for sure is to submit a patch to gcc-patches and
start the discussion.

More information about the Gcc-bugs mailing list