warn-access: Fix up check_pointer_uses [PR104715]
authorJakub Jelinek <jakub@redhat.com>
Tue, 1 Mar 2022 20:05:31 +0000 (21:05 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 1 Mar 2022 20:05:31 +0000 (21:05 +0100)
commit1a0e3bba4b735fa8e4463d52950d0ce9c30c16c7
tree03f123bdb7160873acc9930fdd7724de9ed18973
parent4a1c20df82c9e14478d79fbe1ae9690a36285ac1
warn-access: Fix up check_pointer_uses [PR104715]

The following testcase emits bogus -Wdangling-pointer warnings.
The bug is that when it sees that ptr immediate use is a call that
returns one of its arguments, it will assume that the return value
is based on ptr, but that is the case only if ptr is passed to the
argument that is actually returned (so e.g. for memcpy the first argument,
etc.).  When the builtins guarantee e.g. that the result is based on the
first argument (either ERF_RETURNS_ARG 0 in which case it will always
just returns the first argument as is, or when it is something like
strstr or strpbrk or mempcpy that it returns some pointer based on the
first argument), it means the result is not based on second or following
argument if any.  The second hunk fixes this.

The first hunk just removes an unnecessary TREE_CODE check, the code only
pushes SSA_NAMEs into the pointers vector and if it didn't, it uses
      FOR_EACH_IMM_USE_FAST (use_p, iter, ptr)
a few lines below this, which of course requires that ptr is a SSA_NAME.
Tree checking on SSA_NAME_VERSION will already ensure that if it wasn't
a SSA_NAME, we'd ICE.

2022-03-01  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/104715
* gimple-ssa-warn-access.cc (pass_waccess::check_pointer_uses): Don't
unnecessarily test if ptr is a SSA_NAME, it has to be.  Only push lhs
of a call if gimple_call_return_arg is equal to ptr, not just when it
is non-NULL.

* c-c++-common/Wdangling-pointer-7.c: New test.
gcc/gimple-ssa-warn-access.cc
gcc/testsuite/c-c++-common/Wdangling-pointer-7.c [new file with mode: 0644]