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

            Bug ID: 80669
           Summary: [8 Regression] Bad -Wstringop-overflow warnings for
                    stpncpy
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jsm28 at gcc dot gnu.org
  Target Milestone: ---

The following code (compiled with -Wall, on x86_64, trunk revision 247733)
produces a bogus warning (causing the glibc testsuite build to fail):

char buf[100];
void
f (void)
{
  __builtin_stpncpy (buf, "foo", 4);
}

t.c: In function 'f':
t.c:5:3: warning: '__builtin_stpncpy' reading 4 bytes from a region of size 3
[-Wstringop-overflow=]
   __builtin_stpncpy (buf, "foo", 4);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The region being read actually has four bytes, not three; it's NUL-terminated. 
And since it's NUL-terminated, any size argument to stpncpy, up to the size of
the destination buffer, would be OK, just as with strncpy; it only makes sense
to diagnose a read buffer overrun for strncpy or stpncpy if the source buffer
has no NUL bytes and the size is too big for it.  In any case, the same
warnings should be given for both strncpy and stpncpy, which means not warning
for this test case (just as a corresponding test with strncpy does not warn).

Reply via email to