[PATCH] avoid folding of invalid indices to compound literals (PR 92341)

Jeff Law law@redhat.com
Mon Nov 4 22:28:00 GMT 2019


On 11/4/19 3:05 PM, Martin Sebor wrote:
> While testing some other changes I noticed that -Warray-bounds
> fails to detect out-of-bounds indices to compound literals such
> as in:
> 
>   int *p = (int[]){ 1, 2, 3 };
>   // ...
>   p[3] = 7;
> 
> This is because SRA transforms such references into accesses to
> uninitialized scalar variables and also sets the TREE_NO_WARNING
> bit for the replacement variables.  This prevents -Wuninitialized
> from detecting such bugs, although that wouldn't be the right
> warning to issue in these cases).
> 
> The attached patch tweaks SRA to avoid this transformation when
> the access is out of the bounds of the referenced variable.  That
> in turn lets -Warray-bounds diagnose these invalid accesses.
> 
> The patch also adjusts -Warray-bounds to reference to correct
> index and message and issue the warning even for zero-length
> compound literal arrays.  This was exposed and the fix is relied
> on by the test I wrote for the compound literals.
> 
> Finally, the change also corrects an oversight of mine from some
> time ago in failing to handle out-of-bounds indices relative to
> addresses of function parameters.  This is a trivial one-line
> tweak that could be submitted separately but it doesn't seem
> worth the overhead.
> 
> Tested on x86_64-linux.
> 
> Martin
> 
> gcc-92341.diff
> 
> PR middle-end/92341 - missing -Warray-bounds indexing past the end of a compound literal
> PR middle-end/82612 - missing -Warray-bounds on a non-zero offset from the address of a non-array object
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR middle-end/92341
> 	PR middle-end/82612
> 	* gcc.dg/Warray-bounds-50.c: New test.
> 	* gcc.dg/Warray-bounds-51.c: New test.
> 
> gcc/ChangeLog:
> 
> 	PR middle-end/92341
> 	PR middle-end/82612
> 	* tree-sra.c (get_access_for_expr): Fail for out-of-bounds offsets.
> 	* tree-vrp.c (vrp_prop::check_array_ref): Correct index and text
> 	of message printed in a warning for empty arrays.
> 	(vrp_prop::check_mem_ref): Also handle function parameters and
> 	empty arrays.
OK
jeff



More information about the Gcc-patches mailing list