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

            Bug ID: 83519
           Summary: missing -Wrestrict on an overlapping strcpy to a
                    non-member array
           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: ---

Most likely due to bug 83501, while the newly enhanced -Wrestrict warning
detects the overlapping copy in function f() below, it fails to detect the same
problem in g() and h().  The strlen pass isn't prepared to handle the MEM_REF.

$ cat a.c && gcc -O2 -S -Wall -fdump-tree-strlen=/dev/stdout a.c
extern char* stpcpy (char*, const char*);   // work around bug 82429

struct S { char a[17]; };

void f (struct S *p, const char *s)
{
  __builtin_strcpy (p->a, "0123456789abcdef");

  __builtin_strcpy (p->a, p->a + 4);   // warning (good)
}

char a[17];

void g (const char *s)
{
  __builtin_strcpy (a, "0123456789abcdef");

  __builtin_strcpy (a, a + 4);   // missing warning (bug)
}

void h (const char *s)
{
   char a[17];

  __builtin_strcpy (a, "0123456789abcdef");

  __builtin_strcpy (a, a + 4);   // missing warning (bug)

  extern void sink (void*);
  sink (a);
}


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

a.c: In function ‘f’:
a.c:9:3: warning: ‘__builtin_strcpy’ accessing 13 bytes at offsets 0 and 4
overlaps 9 bytes at offset 4 [-Wrestrict]
   __builtin_strcpy (p->a, p->a + 4);   // warning (good)
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
f (struct S * p, const char * s)
{
  char[17] * _1;
  const char * _2;

  <bb 2> [local count: 1073741825]:
  _1 = &p_3(D)->a;
  __builtin_memcpy (_1, "0123456789abcdef", 17);
  _2 = &MEM[(void *)p_3(D) + 4B];
  __builtin_strcpy (_1, _2);
  return;

}



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

g (const char * s)
{
  <bb 2> [local count: 1073741825]:
  MEM[(char * {ref-all})&a] = MEM[(char * {ref-all})"0123456789abcdef"];
  __builtin_strcpy (&a, &MEM[(void *)&a + 4B]);
  return;

}



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

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

  <bb 2> [local count: 1073741825]:
  MEM[(char * {ref-all})&a] = MEM[(char * {ref-all})"0123456789abcdef"];
  __builtin_strcpy (&a, &MEM[(void *)&a + 4B]);
  sink (&a);
  a ={v} {CLOBBER};
  return;

}

Reply via email to