This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] improve -Wrestrict for struct members (PR 85753)


On 05/14/2018 03:21 PM, Jeff Law wrote:
On 05/11/2018 05:09 PM, Martin Sebor wrote:
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.
OK.
jeff

It occurred to me that it doesn't matter if the type is an array,
struct, or a basic type like int.  They all should be treated
the same so after retesting I ended up committing a slightly
simplified version of the original: r260280.

Martin


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]