From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C3C3A3858D33 for ; Wed, 15 Feb 2023 03:48:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C3C3A3858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676432901; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=+biayyjqPwMbpptUF+EDb0rZUOJwL8BkUg+CXgCGBd0=; b=Sqan+UIHC8U32uDTZ+IRW/k6Cr7xacxZeI3cpJarVRU+UvaCORAxeD55hDeuUM9SQMQdg4 JmzBvEr9G3uUmILUJNTw6ECG1OWD08/67TC+/a//Y4PtC6H9Cg62NwCrgELlXNH/fyMkVo SIZllHlmvSaPpoizapf9w6vUUUEIwCQ= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-511-psfGGD2YOJ6Q_tBG7CByAQ-1; Tue, 14 Feb 2023 22:48:20 -0500 X-MC-Unique: psfGGD2YOJ6Q_tBG7CByAQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E75381C0518C for ; Wed, 15 Feb 2023 03:48:19 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.33.106]) by smtp.corp.redhat.com (Postfix) with ESMTP id C31F6C15BA0 for ; Wed, 15 Feb 2023 03:48:19 +0000 (UTC) From: Marek Polacek To: GCC Patches Subject: [PATCH] warn-access: wrong -Wdangling-pointer with labels [PR106080] Date: Tue, 14 Feb 2023 22:48:15 -0500 Message-Id: <20230215034815.1276847-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: -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. 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