From: Marek Polacek <polacek@redhat.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] warn-access: wrong -Wdangling-pointer with labels [PR106080]
Date: Tue, 14 Feb 2023 22:48:15 -0500 [thread overview]
Message-ID: <20230215034815.1276847-1-polacek@redhat.com> (raw)
-Wdangling-pointer warns when the address of a label escapes. This
causes grief in OCaml (<https://github.com/ocaml/ocaml/issues/11358>) as
well as in the kernel:
<https://bugzilla.kernel.org/show_bug.cgi?id=215851> 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.
Bootstrapped/regtested on ppc64le-pc-linux-gnu, ok for trunk and 12?
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.
---
gcc/gimple-ssa-warn-access.cc | 19 +++++--------------
.../c-c++-common/Wdangling-pointer-10.c | 12 ++++++++++++
.../c-c++-common/Wdangling-pointer-9.c | 9 +++++++++
3 files changed, 26 insertions(+), 14 deletions(-)
create mode 100644 gcc/testsuite/c-c++-common/Wdangling-pointer-10.c
create mode 100644 gcc/testsuite/c-c++-common/Wdangling-pointer-9.c
diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc
index ad9dac54874..2eab1d59abd 100644
--- a/gcc/gimple-ssa-warn-access.cc
+++ b/gcc/gimple-ssa-warn-access.cc
@@ -4326,15 +4326,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
@@ -4363,7 +4354,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;
}
@@ -4383,7 +4374,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);
@@ -4467,7 +4458,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);
@@ -4528,7 +4519,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))
@@ -4573,7 +4564,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;
auto_diagnostic_group d;
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..2147f733607
--- /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; });
+}
base-commit: c348a717213b03c6661878934f197f4d261f0e56
--
2.39.1
next reply other threads:[~2023-02-15 3:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-15 3:48 Marek Polacek [this message]
2023-02-15 9:50 ` Jakub Jelinek
2023-02-15 13:46 ` [PATCH v2] " Marek Polacek
2023-02-15 13:47 ` Jakub Jelinek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230215034815.1276847-1-polacek@redhat.com \
--to=polacek@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).