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

            Bug ID: 90737
           Summary: wrong code returning address of a local converted to
                    intptr_t
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

GCC issues -Wreturn-local-addr even for returning the address of a local
variable converted to an integer.  In addition, it also replaces the value of
the integer with a zero.  Since returning the integer representation of pointer
is well-defined, as is using such an integer, this leads to
inconsistencies/undefined behavior when the integer is first determined to be
non-zero within the body of the returning function and then zero in its caller.

The warning should only be issued for functions that return a pointer. 
Likewise, the replacement of the address with a zero should only be done for
such functions and not for those returning other types.

$ cat a.c && gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout a.c
typedef __INTPTR_TYPE__ intptr_t;

intptr_t f (void)
{
  int i;
  if ((intptr_t)&i == 0)
    __builtin_abort ();

  return (intptr_t)&i;
}

void g (void)
{
  intptr_t i = f ();
  if (i == 0)
    __builtin_trap ();
}
a.c: In function ‘f’:
a.c:9:10: warning: function returns address of local variable
[-Wreturn-local-addr]
    9 |   return (intptr_t)&i;
      |          ^~~~~~~~~~~~

;; Function f (f, funcdef_no=0, decl_uid=1907, cgraph_uid=1, symbol_order=0)

f ()
{
  <bb 2> [local count: 1073741824]:
  return 0;

}



;; Function g (g, funcdef_no=1, decl_uid=1911, cgraph_uid=2, symbol_order=1)
(unlikely executed)

g ()
{
  <bb 2> [count: 0]:
  __builtin_trap ();

}

Reply via email to