From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25529 invoked by alias); 15 Sep 2014 22:31:33 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 25491 invoked by uid 48); 15 Sep 2014 22:31:27 -0000 From: "jacob.benoit.1 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/63272] New: GCC should warn when using pointer to dead scoped variable within the same function Date: Mon, 15 Sep 2014 22:31:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new 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: jacob.benoit.1 at gmail dot com X-Bugzilla-Status: UNCONFIRMED 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter attachments.created Message-ID: 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/msg01680.txt.bz2 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