]> git.ipfire.org Git - thirdparty/gcc.git/commit - gcc/analyzer/region-model.cc
analyzer: fix missing -Wanalyzer-write-to-const [PR102695]
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 16 Nov 2021 15:36:49 +0000 (10:36 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Wed, 17 Nov 2021 02:01:14 +0000 (21:01 -0500)
commit111fd515f2894d7cddf62f80c69765c43ae18577
tree525f35425a21be411304a53e4904071ebec2fe50
parenta80d4e098b10d5cd161f55e4fce64a6be9683ed3
analyzer: fix missing -Wanalyzer-write-to-const [PR102695]

This patch fixes -Wanalyzer-write-to-const so that it will complain
about attempts to write to functions, to labels.
It also "teaches" the analyzer about strchr, in that strchr can either
return a pointer into the input area (and thus -Wanalyzer-write-to-const
can now complain about writes into a string literal seen this way),
or return NULL (and thus the analyzer can complain about NULL
dereferences if the result is used without a check).

gcc/analyzer/ChangeLog:
PR analyzer/102695
* region-model-impl-calls.cc (region_model::impl_call_strchr): New.
* region-model-manager.cc
(region_model_manager::maybe_fold_unaryop): Simplify cast to
pointer type of an existing pointer to a region.
* region-model.cc (region_model::on_call_pre): Handle
BUILT_IN_STRCHR and "strchr".
(write_to_const_diagnostic::emit): Add auto_diagnostic_group.  Add
alternate wordings for functions and labels.
(write_to_const_diagnostic::describe_final_event): Add alternate
wordings for functions and labels.
(region_model::check_for_writable_region): Handle RK_FUNCTION and
RK_LABEL.
* region-model.h (region_model::impl_call_strchr): New decl.

gcc/testsuite/ChangeLog:
PR analyzer/102695
* gcc.dg/analyzer/pr102695.c: New test.
* gcc.dg/analyzer/strchr-1.c: New test.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/analyzer/region-model-impl-calls.cc
gcc/analyzer/region-model-manager.cc
gcc/analyzer/region-model.cc
gcc/analyzer/region-model.h
gcc/testsuite/gcc.dg/analyzer/pr102695.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/analyzer/strchr-1.c [new file with mode: 0644]