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

Reply via email to