[Bug tree-optimization/92341] missing -Warray-bounds indexing past the end of a compound literal
msebor at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Mon Nov 4 00:03:00 GMT 2019
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92341
Martin Sebor <msebor at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |ASSIGNED
Last reconfirmed| |2019-11-04
Assignee|unassigned at gcc dot gnu.org |msebor at gcc dot gnu.org
Ever confirmed|0 |1
--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
The otherwise untested change below prevents SRA from folding the out-of-bounds
access and lets VRP diagnose the invalid access as follows:
pr82612.c:26:11: warning: array subscript 2 is outside array bounds of ‘int[2]’
[-Warray-bounds]
26 | return p[2];
| ~^~~
pr82612.c:25:19: note: while referencing ‘({anonymous})’
25 | int *p = (int[]){ 1, 2 };
| ^
diff --git a/gcc/tree-sra.c b/gcc/tree-sra.c
index 3f104238d93..63f1800f0d4 100644
--- a/gcc/tree-sra.c
+++ b/gcc/tree-sra.c
@@ -3068,6 +3068,13 @@ get_access_for_expr (tree expr)
|| !DECL_P (base))
return NULL;
+ if (tree basesize = DECL_SIZE (base))
+ {
+ poly_int64 sz = tree_to_poly_int64 (basesize);
+ if (known_le (sz, offset))
+ return NULL;
+ }
+
if (!bitmap_bit_p (candidate_bitmap, DECL_UID (base)))
return NULL;
More information about the Gcc-bugs
mailing list