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

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
There is nothing to indicate that the first call to memcpy() in comment #0
overlaps so -Wrestrict doesn't warn for it.

One thing to note is that the warning treats raw memory functions slightly
differently than string functions because the former tend to be used to copy
regions of the same array within itself, while string functions don't.  So
while the first memcpy call doesn't trigger a warning, if it were replaced with
one to strncpy, GCC would warn:

In file included from /usr/include/string.h:630,
                 from d.c:3:
pr83688.c: In function ‘test’:
pr83688.c:11:5: warning: ‘__builtin_strncpy’ accessing 1 or more bytes at
offsets 0 and 5 may overlap up to 9223372036854775802 bytes at offset
[9223372036854775806, 5] [-Wrestrict]
     strncpy (buf, buf+5, strlen(buf+5)+1);
     ^~~~~~~

(The numbers it prints could stand to be improved.)

Regarding -Wformat-overflow, it does (obviously) check for sprintf overflow. 
It doesn't check for overlap like in this call:

  sprintf (d + i, "%s", d);

That's what the July 2017 patch added.  In case of either warning, I don't
think their quality would be improved by explicitly saying that overflow or
overlap is undefined.  Very few GCC warnings do, and I feel that describing the
problem is more informative than saying that something undefined has happened. 
It would of course be possible to print a note after the warning saying that
it's undefined.  But I'd rather have the note include additional detail about
the context of the problem than state (what should be) obvious.

Reply via email to