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

            Bug ID: 83701
           Summary: missing -Wrestrict on strcat to an array depending on
                    offset
           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: ---

Calling strcat to append to the same string as the source always overlaps
exactly 1 byte: the terminating NUL.  This is true regardless of the source and
destination offsets.  The test case below shows that the -Wrestict warning
doesn't detect this problem with complete consistency.

$ cat b.c && gcc -O2 -S -Wall b.c
extern char* stpcpy (char*, const char*);
extern char* strcat (char*, const char*);
extern char* strcpy (char*, const char*);

void f1 (char *d)
{
  strcpy (d, "01234");
  strcat (d + 2, d + 1);   // -Wrestrict (good)
}

void f2 (char *d)
{
  strcpy (d, "12345");
  strcat (d + 2, d + 1);   // -Wrestrict (good)
}

extern char d[];

void f3 (void)
{
  strcpy (d, "23456");
  strcat (d + 2, d + 1);   // missing -Wrestrict
}

void f4 (void)
{
  strcpy (d, "34567");
  strcat (d + 2, d + 1);   // missing -Wrestrict
}


void f5 (void)
{
  strcpy (d, "23456");
  strcat (d + 1, d + 2);   // -Wrestrict (good)
}
b.c: In function ‘f1’:
b.c:8:3: warning: ‘strcat’ accessing 5 bytes at offsets 2 and 1 overlaps 1 byte
at offset 5 [-Wrestrict]
   strcat (d + 2, d + 1);   // -Wrestrict (good)
   ^~~~~~~~~~~~~~~~~~~~~
b.c: In function ‘f2’:
b.c:14:3: warning: ‘strcat’ accessing 5 bytes at offsets 2 and 1 overlaps 1
byte at offset 5 [-Wrestrict]
   strcat (d + 2, d + 1);   // -Wrestrict (good)
   ^~~~~~~~~~~~~~~~~~~~~
b.c: In function ‘f5’:
b.c:35:3: warning: ‘strcat’ accessing 4 bytes at offsets 1 and 2 overlaps 1
byte at offset 5 [-Wrestrict]
   strcat (d + 1, d + 2);   // -Wrestrict (good)
   ^~~~~~~~~~~~~~~~~~~~~

Reply via email to