[Bug analyzer/95007] RFE: -fanalyzer should complain about writes to string literals

egallager at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon May 11 04:42:57 GMT 2020


Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=95000,
                   |                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=61579
           Keywords|                            |diagnostic
                 CC|                            |egallager at gcc dot gnu.org

--- Comment #1 from Eric Gallager <egallager at gcc dot gnu.org> ---
You don't even need the analyzer for this; -Wwrite-strings already catches it:

$ /usr/local/bin/gcc -c -Wall -Wextra -Wwrite-strings 95007.c
95007.c: In function 'test':
95007.c:3:12: warning: initialization discards 'const' qualifier from pointer
target type [-Wdiscarded-qualifiers]
    3 |  char *s = "foo";
      |            ^~~~~

If you edit the source code to make 's' const to fix the -Wdiscarded-qualifiers
warning, you then get this error instead:

$ /usr/local/bin/gcc -c -Wall -Wextra -Wwrite-strings 95007.c
95007.c: In function 'test':
95007.c:4:7: error: assignment of read-only location '*s'
    4 |  s[0] = 'g';
      |       ^

Although, I guess it is true that there are some drawbacks to using
-Wwrite-strings, for example those described in bug 61579, so maybe having a
separate analyzer warning for this could still be useful...

More information about the Gcc-bugs mailing list