analyzer: add warnings about writes to constant regions [PR95007]
authorDavid Malcolm <dmalcolm@redhat.com>
Wed, 7 Oct 2020 22:34:09 +0000 (18:34 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Mon, 12 Oct 2020 16:03:07 +0000 (12:03 -0400)
commit3175d40fc52fb8eb3c3b18cc343d773da24434fb
treed1c8e7c2e09a91ed75f0e5476c648c2e745aa2de
parent1be51a3a9ac3409561223c8058d4943f9b574d15
analyzer: add warnings about writes to constant regions [PR95007]

This patch adds two new warnings:
  -Wanalyzer-write-to-const
  -Wanalyzer-write-to-string-literal
for code paths where the analyzer detects a write to a constant region.

As noted in the documentation part of the patch, the analyzer doesn't
prioritize detection of such writes, in that the state-merging logic
will blithely lose the distinction between const and non-const regions.
Hence false negatives are likely to arise due to state-merging.

However, if the analyzer does happen to spot such a write, it seems worth
reporting, hence this patch.

gcc/analyzer/ChangeLog:
* analyzer.opt (Wanalyzer-write-to-const): New.
(Wanalyzer-write-to-string-literal): New.
* region-model-impl-calls.cc (region_model::impl_call_memcpy):
Call check_for_writable_region.
(region_model::impl_call_memset): Likewise.
(region_model::impl_call_strcpy): Likewise.
* region-model.cc (class write_to_const_diagnostic): New.
(class write_to_string_literal_diagnostic): New.
(region_model::check_for_writable_region): New.
(region_model::set_value): Call check_for_writable_region.
* region-model.h (region_model::check_for_writable_region): New
decl.

gcc/ChangeLog:
* doc/invoke.texi: Document -Wanalyzer-write-to-const and
-Wanalyzer-write-to-string-literal.

gcc/testsuite/ChangeLog:
PR c/83347
PR middle-end/90404
PR analyzer/95007
* gcc.dg/analyzer/write-to-const-1.c: New test.
* gcc.dg/analyzer/write-to-string-literal-1.c: New test.
gcc/analyzer/analyzer.opt
gcc/analyzer/region-model-impl-calls.cc
gcc/analyzer/region-model.cc
gcc/analyzer/region-model.h
gcc/doc/invoke.texi
gcc/testsuite/gcc.dg/analyzer/write-to-const-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/analyzer/write-to-string-literal-1.c [new file with mode: 0644]