https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92630
Bug ID: 92630 Summary: missing -Wrestrict with attribute access and restrict Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- Besides out-of-bounds reads and writes, the new attribute access also makes it possible to detect overlapping copies. Calls in all four functions below should be diagnosed as suggested in the comments. $ cat t.c && gcc -O2 -S -Wall -Wextra t.c typedef __SIZE_TYPE__ size_t; void* memcpy (void* restrict, const void* restrict, size_t); __attribute__ ((access (write_only, 1, 3), access (read_only, 2, 3))) void f0 (void* restrict, const void *restrict, unsigned); char a[8]; void f1 (void) { memcpy (a, a + 2, 8); // -Wstringop-overflow (good) } void f2 (void) { memcpy (a, a + 2, 4); // -Wsrestrict (good) } __attribute__ ((access (write_only, 1, 3), access (read_only, 2, 3))) void mycpy (void* restrict, const void *restrict, unsigned); void g1 (void) { mycpy (a, a + 2, 8); // warning (good) } void g2 (void) { mycpy (a, a + 2, 4); // missing -Wsrestrict } t.c: In function ‘f2’: t.c:17:3: warning: ‘memcpy’ accessing 4 bytes at offsets 0 and 2 overlaps 2 bytes at offset 2 [-Wrestrict] 17 | memcpy (a, a + 2, 4); // -Wsrestrict (good) | ^~~~~~~~~~~~~~~~~~~~ t.c: In function ‘f1’: t.c:12:3: warning: ‘memcpy’ forming offset [8, 9] is out of the bounds [0, 8] of object ‘a’ with type ‘char[8]’ [-Warray-bounds] 12 | memcpy (a, a + 2, 8); // warning (good) | ^~~~~~~~~~~~~~~~~~~~ t.c:8:6: note: ‘a’ declared here 8 | char a[8]; | ^ t.c: In function ‘g1’: t.c:25:3: warning: ‘mycpy’ reading 8 bytes from a region of size 6 [-Wstringop-overflow=] 25 | mycpy (a, a + 2, 8); // -Wstringop-overflow (good) | ^~~~~~~~~~~~~~~~~~~ t.c:21:1: note: in a call to function ‘mycpy’ declared with attribute ‘read_only (2, 3)’ 21 | mycpy (void* restrict, const void *restrict, unsigned); | ^~~~~