analyzer: further false leak fixes due to overzealous state merging [PR103217]
authorDavid Malcolm <dmalcolm@redhat.com>
Mon, 29 Nov 2021 16:47:47 +0000 (11:47 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Mon, 29 Nov 2021 23:50:56 +0000 (18:50 -0500)
commit132902177138c09803d639e12b1daebf2b9edddc
tree31a44f1e3537dd31c4500efa12338b38bc98d24d
parentca5667e867252db3c8642ee90f55427149cd92b6
analyzer: further false leak fixes due to overzealous state merging [PR103217]

Commit r12-5424-gf573d35147ca8433c102e1721d8c99fc432cb44b fixed a false
positive from -Wanalyzer-malloc-leak due to overzealous state merging,
erroneously merging two different svalues bound to a particular part
of the store when one has sm-state.

A further case was discovered by the reporter of PR analyzer/103217,
which this patch fixes.  In this variant, different states have set
different fields of a struct, and on attempting to merge them, the
states have a different set of binding keys, leading to one state
having an svalue with sm-state, and its peer state having a NULL value
for that binding key.  The state merger code was erroneously treating
them as mergeable to "UNKNOWN".  This followup patch fixes things by
rejecting such mergers if the non-NULL svalue is not mergeable with
"UNKNOWN".

gcc/analyzer/ChangeLog:
PR analyzer/103217
* store.cc (binding_cluster::can_merge_p): For the "key is bound"
vs "key is not bound" merger case, check that the bound svalue
is mergeable before merging it to "unknown", rejecting the merger
otherwise.

gcc/testsuite/ChangeLog:
PR analyzer/103217
* gcc.dg/analyzer/pr103217-2.c: New test.
* gcc.dg/analyzer/pr103217-3.c: New test.
* gcc.dg/analyzer/pr103217-4.c: New test.
* gcc.dg/analyzer/pr103217-5.c: New test.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/analyzer/store.cc
gcc/testsuite/gcc.dg/analyzer/pr103217-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/analyzer/pr103217-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/analyzer/pr103217-4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/analyzer/pr103217-5.c [new file with mode: 0644]