public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function
@ 2014-09-15 22:31 jacob.benoit.1 at gmail dot com
  2014-09-16  6:14 ` [Bug c/63272] " manu at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: jacob.benoit.1 at gmail dot com @ 2014-09-15 22:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63272

            Bug ID: 63272
           Summary: GCC should warn when using pointer to dead scoped
                    variable within the same function
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jacob.benoit.1 at gmail dot com

Created attachment 33497
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33497&action=edit
testcase

Consider this testcase:

int foo(int i)
{
  int *p = 0;

  if (i)
  {
    int k = i;
    p = &k;
  } /* end of nested scope, k dies, p becomes a dangling pointer */

  return p ? *p : 0; /* the compiler should warn that p points to dead k */
}

Neither Clang 3.4 nor GCC 4.8 currently generate a warning. Yet, since all is
local to this function, it seems like something that a compiler could easily
warn against, and that would have saved me some time today!

Also see LLVM bug: http://llvm.org/bugs/show_bug.cgi?id=20952


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/63272] GCC should warn when using pointer to dead scoped variable within the same function
  2014-09-15 22:31 [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function jacob.benoit.1 at gmail dot com
@ 2014-09-16  6:14 ` manu at gcc dot gnu.org
  2020-10-30 10:06 ` dcb314 at hotmail dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu.org @ 2014-09-16  6:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63272

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-09-16
                 CC|                            |manu at gcc dot gnu.org
     Ever confirmed|0                           |1

--- Comment #1 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
It would be nice to have. Perhaps related to PR49974.
>From gcc-bugs-return-461855-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Tue Sep 16 06:38:12 2014
Return-Path: <gcc-bugs-return-461855-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 6177 invoked by alias); 16 Sep 2014 06:38:11 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 6098 invoked by uid 48); 16 Sep 2014 06:38:00 -0000
From: "pinskia at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/63272] GCC should warn when using pointer to dead scoped variable within the same function
Date: Tue, 16 Sep 2014 06:38:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: c
X-Bugzilla-Version: unknown
X-Bugzilla-Keywords:
X-Bugzilla-Severity: normal
X-Bugzilla-Who: pinskia at gcc dot gnu.org
X-Bugzilla-Status: NEW
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields:
Message-ID: <bug-63272-4-u3bJmmcqAG@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-63272-4@http.gcc.gnu.org/bugzilla/>
References: <bug-63272-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2014-09/txt/msg01689.txt.bz2
Content-length: 275

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63272

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Manuel López-Ibáñez from comment #1)
> It would be nice to have. Perhaps related to PR49974.

Definitely related to PR 53855.
>From gcc-bugs-return-461856-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Tue Sep 16 06:40:09 2014
Return-Path: <gcc-bugs-return-461856-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 7532 invoked by alias); 16 Sep 2014 06:40:08 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 7495 invoked by uid 48); 16 Sep 2014 06:40:05 -0000
From: "pinskia at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/63272] GCC should warn when using pointer to dead scoped variable within the same function
Date: Tue, 16 Sep 2014 06:40:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: c
X-Bugzilla-Version: unknown
X-Bugzilla-Keywords:
X-Bugzilla-Severity: normal
X-Bugzilla-Who: pinskia at gcc dot gnu.org
X-Bugzilla-Status: NEW
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields:
Message-ID: <bug-63272-4-lX1cLlDQ5e@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-63272-4@http.gcc.gnu.org/bugzilla/>
References: <bug-63272-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2014-09/txt/msg01690.txt.bz2
Content-length: 255

https://gcc.gnu.org/bugzilla/show_bug.cgi?idc272

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
But really this is already talked about the warning in bug 60517 and really in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id`770#c1 .


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/63272] GCC should warn when using pointer to dead scoped variable within the same function
  2014-09-15 22:31 [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function jacob.benoit.1 at gmail dot com
  2014-09-16  6:14 ` [Bug c/63272] " manu at gcc dot gnu.org
@ 2020-10-30 10:06 ` dcb314 at hotmail dot com
  2021-11-01 22:21 ` msebor at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: dcb314 at hotmail dot com @ 2020-10-30 10:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63272

David Binderman <dcb314 at hotmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dcb314 at hotmail dot com

--- Comment #4 from David Binderman <dcb314 at hotmail dot com> ---
I can confirm that gcc version 11, some six years later, doesn't implement
this warning.

Fortunately, the cppcheck static analyser does.

Example code:

// Dangling pointer

void g( int *);

void f()
{
        int a;
        int * p = 0;

        {
                int b;

                p = &b;
        }
        g( p);
}

$ /home/dcb/cppcheck/trunk/cppcheck oct30a.cc
oct30a.cc:16:5: error: Using pointer to local variable 'b' that is out of
scope. [invalidLifetime]
 g( p);
    ^
oct30a.cc:14:7: note: Address of variable taken here.
  p = &b;
      ^
oct30a.cc:12:7: note: Variable created here.
  int b;
      ^
oct30a.cc:16:5: note: Using pointer to local variable 'b' that is out of scope.
 g( p);
    ^

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/63272] GCC should warn when using pointer to dead scoped variable within the same function
  2014-09-15 22:31 [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function jacob.benoit.1 at gmail dot com
  2014-09-16  6:14 ` [Bug c/63272] " manu at gcc dot gnu.org
  2020-10-30 10:06 ` dcb314 at hotmail dot com
@ 2021-11-01 22:21 ` msebor at gcc dot gnu.org
  2021-12-16 23:31 ` msebor at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-11-01 22:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63272

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |msebor at gcc dot gnu.org
           Keywords|                            |patch

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
Patch: https://gcc.gnu.org/pipermail/gcc-patches/2021-November/583045.html

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/63272] GCC should warn when using pointer to dead scoped variable within the same function
  2014-09-15 22:31 [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function jacob.benoit.1 at gmail dot com
                   ` (2 preceding siblings ...)
  2021-11-01 22:21 ` msebor at gcc dot gnu.org
@ 2021-12-16 23:31 ` msebor at gcc dot gnu.org
  2021-12-17  9:06 ` [Bug middle-end/63272] " dcb314 at hotmail dot com
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-12-16 23:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63272

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |oleg.pekar.2017 at gmail dot com

--- Comment #6 from Martin Sebor <msebor at gcc dot gnu.org> ---
*** Bug 98901 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/63272] GCC should warn when using pointer to dead scoped variable within the same function
  2014-09-15 22:31 [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function jacob.benoit.1 at gmail dot com
                   ` (3 preceding siblings ...)
  2021-12-16 23:31 ` msebor at gcc dot gnu.org
@ 2021-12-17  9:06 ` dcb314 at hotmail dot com
  2022-01-15 23:46 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: dcb314 at hotmail dot com @ 2021-12-17  9:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63272

--- Comment #7 from David Binderman <dcb314 at hotmail dot com> ---
(In reply to Martin Sebor from comment #5)
> Patch: https://gcc.gnu.org/pipermail/gcc-patches/2021-November/583045.html

Weeks have rolled by and this patch doesn't seem to have made it into trunk.
Disappointing.

Time to nag someone with trunk write permission ?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/63272] GCC should warn when using pointer to dead scoped variable within the same function
  2014-09-15 22:31 [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function jacob.benoit.1 at gmail dot com
                   ` (4 preceding siblings ...)
  2021-12-17  9:06 ` [Bug middle-end/63272] " dcb314 at hotmail dot com
@ 2022-01-15 23:46 ` cvs-commit at gcc dot gnu.org
  2022-01-16  0:04 ` msebor at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-01-15 23:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63272

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:9d6a0f388eb048f8d87f47af78f07b5ce513bfe6

commit r12-6606-g9d6a0f388eb048f8d87f47af78f07b5ce513bfe6
Author: Martin Sebor <msebor@redhat.com>
Date:   Sat Jan 15 16:41:40 2022 -0700

    Add -Wdangling-pointer [PR63272].

    Resolves:
    PR c/63272 - GCC should warn when using pointer to dead scoped variable
with
    in the same function

    gcc/c-family/ChangeLog:

            PR c/63272
            * c.opt (-Wdangling-pointer): New option.

    gcc/ChangeLog:

            PR c/63272
            * diagnostic-spec.c (nowarn_spec_t::nowarn_spec_t): Handle
            -Wdangling-pointer.
            * doc/invoke.texi (-Wdangling-pointer): Document new option.
            * gimple-ssa-warn-access.cc (pass_waccess::clone): Set new member.
            (pass_waccess::check_pointer_uses): New function.
            (pass_waccess::gimple_call_return_arg): New function.
            (pass_waccess::gimple_call_return_arg_ref): New function.
            (pass_waccess::check_call_dangling): New function.
            (pass_waccess::check_dangling_uses): New function overloads.
            (pass_waccess::check_dangling_stores): New function.
            (pass_waccess::check_dangling_stores): New function.
            (pass_waccess::m_clobbers): New data member.
            (pass_waccess::m_func): New data member.
            (pass_waccess::m_run_number): New data member.
            (pass_waccess::m_check_dangling_p): New data member.
            (pass_waccess::check_alloca): Check m_early_checks_p.
            (pass_waccess::check_alloc_size_call): Same.
            (pass_waccess::check_strcat): Same.
            (pass_waccess::check_strncat): Same.
            (pass_waccess::check_stxcpy): Same.
            (pass_waccess::check_stxncpy): Same.
            (pass_waccess::check_strncmp): Same.
            (pass_waccess::check_memop_access): Same.
            (pass_waccess::check_read_access): Same.
            (pass_waccess::check_builtin): Call check_pointer_uses.
            (pass_waccess::warn_invalid_pointer): Add arguments.
            (is_auto_decl): New function.
            (pass_waccess::check_stmt): New function.
            (pass_waccess::check_block): Call check_stmt.
            (pass_waccess::execute): Call check_dangling_uses,
            check_dangling_stores.  Empty m_clobbers.
            * passes.def (pass_warn_access): Invoke pass two more times.

    gcc/testsuite/ChangeLog:

            PR c/63272
            * g++.dg/warn/Wfree-nonheap-object-6.C: Disable valid warnings.
            * g++.dg/warn/ref-temp1.C: Prune expected warning.
            * gcc.dg/uninit-pr50476.c: Expect a new warning.
            * c-c++-common/Wdangling-pointer-2.c: New test.
            * c-c++-common/Wdangling-pointer-3.c: New test.
            * c-c++-common/Wdangling-pointer-4.c: New test.
            * c-c++-common/Wdangling-pointer-5.c: New test.
            * c-c++-common/Wdangling-pointer-6.c: New test.
            * c-c++-common/Wdangling-pointer.c: New test.
            * g++.dg/warn/Wdangling-pointer-2.C: New test.
            * g++.dg/warn/Wdangling-pointer.C: New test.
            * gcc.dg/Wdangling-pointer-2.c: New test.
            * gcc.dg/Wdangling-pointer.c: New test.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/63272] GCC should warn when using pointer to dead scoped variable within the same function
  2014-09-15 22:31 [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function jacob.benoit.1 at gmail dot com
                   ` (5 preceding siblings ...)
  2022-01-15 23:46 ` cvs-commit at gcc dot gnu.org
@ 2022-01-16  0:04 ` msebor at gcc dot gnu.org
  2024-04-09  7:32 ` pinskia at gcc dot gnu.org
  2024-04-09  7:33 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-01-16  0:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63272

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |12.0
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #9 from Martin Sebor <msebor at gcc dot gnu.org> ---
Implemented in GCC 12 as -Wdangling-pointer.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/63272] GCC should warn when using pointer to dead scoped variable within the same function
  2014-09-15 22:31 [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function jacob.benoit.1 at gmail dot com
                   ` (6 preceding siblings ...)
  2022-01-16  0:04 ` msebor at gcc dot gnu.org
@ 2024-04-09  7:32 ` pinskia at gcc dot gnu.org
  2024-04-09  7:33 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-09  7:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63272

--- Comment #10 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 88058 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/63272] GCC should warn when using pointer to dead scoped variable within the same function
  2014-09-15 22:31 [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function jacob.benoit.1 at gmail dot com
                   ` (7 preceding siblings ...)
  2024-04-09  7:32 ` pinskia at gcc dot gnu.org
@ 2024-04-09  7:33 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-09  7:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63272

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dgilbert at redhat dot com

--- Comment #11 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 89990 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-04-09  7:33 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-15 22:31 [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function jacob.benoit.1 at gmail dot com
2014-09-16  6:14 ` [Bug c/63272] " manu at gcc dot gnu.org
2020-10-30 10:06 ` dcb314 at hotmail dot com
2021-11-01 22:21 ` msebor at gcc dot gnu.org
2021-12-16 23:31 ` msebor at gcc dot gnu.org
2021-12-17  9:06 ` [Bug middle-end/63272] " dcb314 at hotmail dot com
2022-01-15 23:46 ` cvs-commit at gcc dot gnu.org
2022-01-16  0:04 ` msebor at gcc dot gnu.org
2024-04-09  7:32 ` pinskia at gcc dot gnu.org
2024-04-09  7:33 ` pinskia at gcc dot gnu.org

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).