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

            Bug ID: 81811
           Summary: missing -Wreturn-local-addr returning strcpy result
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

Modifying the test case from bug 81810 to have each of the test functions
return the address of the local array by returning each of the built-in's
return value exposes a defect in the implementation of -Wreturn-local-addr. 
The warning successfully detects the bug in the case of memcpy but fails to
detect the same problem involving strcpy or strncpy.

$ cat a.c && gcc -O2 -S -Wall  -fdump-tree-optimized=/dev/stdout a.c
void* f (const void *p, unsigned n)
{
  char a[8];
  return __builtin_memcpy (a, p, n);   // -Wreturn-local-addr (good)
}

char* g (const char *s)
{
  char a[8];
  return __builtin_strcpy (a, s);   // missing -Wreturn-local-addr
}

char* h (const char *s)
{
  char a[8];
  return __builtin_strncpy (a, s, sizeof a);   // missing -Wreturn-local-addr
}

a.c: In function ‘f’:
a.c:4:10: warning: function returns address of local variable
[-Wreturn-local-addr]
   return __builtin_memcpy (a, p, n);   // -Wreturn-local-addr (good)
          ^~~~~~~~~~~~~~~~~~~~~~~~~~
a.c:3:8: note: declared here
   char a[8];
        ^

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

f (const void * p, unsigned int n)
{
  <bb 2> [100.00%] [count: INV]:
  return 0B;

}



;; Function g (g, funcdef_no=1, decl_uid=1820, cgraph_uid=1, symbol_order=1)

g (const char * s)
{
  char a[8];
  char * _4;

  <bb 2> [100.00%] [count: INV]:
  _4 = __builtin_strcpy (&a, s_2(D));
  a ={v} {CLOBBER};
  return _4;

}



;; Function h (h, funcdef_no=2, decl_uid=1824, cgraph_uid=2, symbol_order=2)

h (const char * s)
{
  char a[8];
  char * _4;

  <bb 2> [100.00%] [count: INV]:
  _4 = __builtin_strncpy (&a, s_2(D), 8);
  a ={v} {CLOBBER};
  return _4;

}

Reply via email to