The attached patch extends -Wrestrict to constrain valid offset
ranges into objects of struct types to the bounds of the object
type, the same way the warning already handles arrays. This
makes it possible to detect overlapping accesses in cases like
the second call to memcpy below:
char a[16];
struct { char a[16]; } x;
void f (int i, int j)
{
memcpy (&a[i], &a[j], 9); // -Wrestrict (good)
memcpy (&x.a[i], &x.a[j], 9); // missing -Wrestrict
}
These is no way to copy 9 bytes within a 16 byte array without
either overlap or without accessing memory outside the bounaries
of the object.
This is for GCC 9.
Thanks
Martin
gcc-85753.diff
PR tree-optimization/85753 - missing -Wrestrict on memcpy into a member array
gcc/ChangeLog:
PR tree-optimization/85753
* gimple-ssa-warn-restrict.c (builtin_memref::builtin_memref): Handle
RECORD_TYPE in addition to ARRAY_TYPE.
gcc/testsuite/ChangeLog:
PR tree-optimization/85753
* gcc.dg/Wrestrict-10.c: Adjust.
* gcc.dg/Wrestrict-16.c: New test.