From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id EC2FB3858D28; Wed, 15 Feb 2023 17:39:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EC2FB3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676482762; bh=Re1lEbVJ+VgGK6bd+XJ12UhwVNCKkGnASD5DAmB2pZw=; h=From:To:Subject:Date:From; b=tAn63bbRS6t1fYvbtRwWragzyy73KuLkNsVgMES3+ulCLanfrk8b0+BS8ncbqy87o fzuW6YB0bFGjMXit6IIfeO0tSwWQMVYi79uxu1UKXe+RB8KdDxe+vjPI5umnUV54UO cHFXLiVebRRR4ToQTbKa52GK0zcEgyBGNXclgqBI= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-9176] warn-access: wrong -Wdangling-pointer with labels [PR106080] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 690512f25c8f0434ec3193bbf79160d2a2bda98f X-Git-Newrev: 825a47f1bb9a42df65157d4dc0a11b2c054e97cc Message-Id: <20230215173922.EC2FB3858D28@sourceware.org> Date: Wed, 15 Feb 2023 17:39:22 +0000 (GMT) List-Id: https://gcc.gnu.org/g:825a47f1bb9a42df65157d4dc0a11b2c054e97cc commit r12-9176-g825a47f1bb9a42df65157d4dc0a11b2c054e97cc Author: Marek Polacek Date: Tue Feb 14 17:05:44 2023 -0500 warn-access: wrong -Wdangling-pointer with labels [PR106080] -Wdangling-pointer warns when the address of a label escapes. This causes grief in OCaml () as well as in the kernel: because it uses #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) to get the PC. -Wdangling-pointer is documented to warn about pointers to objects. However, it uses is_auto_decl which checks DECL_P, but DECL_P is also true for a label/enumerator/function declaration, none of which is an object. Rather, it should use auto_var_p which correctly checks VAR_P and PARM_DECL. PR middle-end/106080 gcc/ChangeLog: * gimple-ssa-warn-access.cc (is_auto_decl): Remove. Use auto_var_p instead. gcc/testsuite/ChangeLog: * c-c++-common/Wdangling-pointer-10.c: New test. * c-c++-common/Wdangling-pointer-9.c: New test. (cherry picked from commit d482b20fd346482635a770281a164a09d608b058) Diff: --- gcc/gimple-ssa-warn-access.cc | 19 +++++-------------- gcc/testsuite/c-c++-common/Wdangling-pointer-10.c | 12 ++++++++++++ gcc/testsuite/c-c++-common/Wdangling-pointer-9.c | 9 +++++++++ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc index efe55dab917..bac8675aa5a 100644 --- a/gcc/gimple-ssa-warn-access.cc +++ b/gcc/gimple-ssa-warn-access.cc @@ -4308,15 +4308,6 @@ pass_waccess::check_call (gcall *stmt) check_nonstring_args (stmt); } - -/* Return true of X is a DECL with automatic storage duration. */ - -static inline bool -is_auto_decl (tree x) -{ - return DECL_P (x) && !DECL_EXTERNAL (x) && !TREE_STATIC (x); -} - /* Check non-call STMT for invalid accesses. */ void @@ -4345,7 +4336,7 @@ pass_waccess::check_stmt (gimple *stmt) while (handled_component_p (lhs)) lhs = TREE_OPERAND (lhs, 0); - if (is_auto_decl (lhs)) + if (auto_var_p (lhs)) m_clobbers.remove (lhs); return; } @@ -4365,7 +4356,7 @@ pass_waccess::check_stmt (gimple *stmt) while (handled_component_p (arg)) arg = TREE_OPERAND (arg, 0); - if (!is_auto_decl (arg)) + if (!auto_var_p (arg)) return; gimple **pclobber = m_clobbers.get (arg); @@ -4449,7 +4440,7 @@ void pass_waccess::check_dangling_uses (tree var, tree decl, bool maybe /* = false */, bool objref /* = false */) { - if (!decl || !is_auto_decl (decl)) + if (!decl || !auto_var_p (decl)) return; gimple **pclob = m_clobbers.get (decl); @@ -4510,7 +4501,7 @@ pass_waccess::check_dangling_stores (basic_block bb, if (!m_ptr_qry.get_ref (lhs, stmt, &lhs_ref, 0)) continue; - if (is_auto_decl (lhs_ref.ref)) + if (auto_var_p (lhs_ref.ref)) continue; if (DECL_P (lhs_ref.ref)) @@ -4555,7 +4546,7 @@ pass_waccess::check_dangling_stores (basic_block bb, || rhs_ref.deref != -1) continue; - if (!is_auto_decl (rhs_ref.ref)) + if (!auto_var_p (rhs_ref.ref)) continue; location_t loc = gimple_location (stmt); diff --git a/gcc/testsuite/c-c++-common/Wdangling-pointer-10.c b/gcc/testsuite/c-c++-common/Wdangling-pointer-10.c new file mode 100644 index 00000000000..ef553bdf2ce --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wdangling-pointer-10.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wdangling-pointer" } */ + +struct S { + int x; +}; + +void g (int **p) +{ + struct S s = {}; + *p = &s.x; /* { dg-warning "address of local variable" } */ +} diff --git a/gcc/testsuite/c-c++-common/Wdangling-pointer-9.c b/gcc/testsuite/c-c++-common/Wdangling-pointer-9.c new file mode 100644 index 00000000000..f6c92855404 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wdangling-pointer-9.c @@ -0,0 +1,9 @@ +/* PR middle-end/106080 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wdangling-pointer" } */ + +void +foo (void **failaddr) +{ + *failaddr = ({ __label__ __here; __here: &&__here; }); /* { dg-bogus "address of local variable" } */ +}