From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 1D87A3858416; Tue, 1 Mar 2022 20:06:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1D87A3858416 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-7439] warn-access: Fix up check_pointer_uses [PR104715] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 4a1c20df82c9e14478d79fbe1ae9690a36285ac1 X-Git-Newrev: 1a0e3bba4b735fa8e4463d52950d0ce9c30c16c7 Message-Id: <20220301200617.1D87A3858416@sourceware.org> Date: Tue, 1 Mar 2022 20:06:17 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Mar 2022 20:06:17 -0000 https://gcc.gnu.org/g:1a0e3bba4b735fa8e4463d52950d0ce9c30c16c7 commit r12-7439-g1a0e3bba4b735fa8e4463d52950d0ce9c30c16c7 Author: Jakub Jelinek Date: Tue Mar 1 21:05:31 2022 +0100 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 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. Diff: --- gcc/gimple-ssa-warn-access.cc | 5 ++-- gcc/testsuite/c-c++-common/Wdangling-pointer-7.c | 36 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc index d015c73dfc6..b7cdad517b3 100644 --- a/gcc/gimple-ssa-warn-access.cc +++ b/gcc/gimple-ssa-warn-access.cc @@ -4169,8 +4169,7 @@ pass_waccess::check_pointer_uses (gimple *stmt, tree ptr, for (unsigned i = 0; i != pointers.length (); ++i) { tree ptr = pointers[i]; - if (TREE_CODE (ptr) == SSA_NAME - && !bitmap_set_bit (visited, SSA_NAME_VERSION (ptr))) + if (!bitmap_set_bit (visited, SSA_NAME_VERSION (ptr))) /* Avoid revisiting the same pointer. */ continue; @@ -4267,7 +4266,7 @@ pass_waccess::check_pointer_uses (gimple *stmt, tree ptr, if (gcall *call = dyn_cast (use_stmt)) { - if (gimple_call_return_arg (call)) + if (gimple_call_return_arg (call) == ptr) if (tree lhs = gimple_call_lhs (call)) if (TREE_CODE (lhs) == SSA_NAME) pointers.safe_push (lhs); diff --git a/gcc/testsuite/c-c++-common/Wdangling-pointer-7.c b/gcc/testsuite/c-c++-common/Wdangling-pointer-7.c new file mode 100644 index 00000000000..8423d3b01ad --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wdangling-pointer-7.c @@ -0,0 +1,36 @@ +/* PR tree-optimization/104715 */ +/* { dg-do compile } */ +/* { dg-options "-Wdangling-pointer" } */ + +char * +foo (char *p) +{ + { + char q[61] = "012345678901234567890123456789012345678901234567890123456789"; + char *r = q; + p = __builtin_strcat (p, r); + } + return p; /* { dg-bogus "using dangling pointer" } */ +} + +char * +bar (char *p) +{ + { + char q[] = "0123456789"; + char *r = q; + p = __builtin_strstr (p, r); + } + return p; /* { dg-bogus "using dangling pointer" } */ +} + +char * +baz (char *p) +{ + { + char q[] = "0123456789"; + char *r = q; + p = __builtin_strpbrk (p, r); + } + return p; /* { dg-bogus "using dangling pointer" } */ +}