Bootstrap / regtest running on x86_64-unknown-linux-gnu.

Richard.

2017-02-28  Richard Biener  <rguent...@suse.de>

        PR middle-end/79731
        * fold-const.c (decode_field_reference): Reject out-of-bound
        accesses.

        * c-c++-common/torture/pr79731.c: New testcase.

Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c    (revision 245772)
+++ gcc/fold-const.c    (working copy)
@@ -4133,7 +4133,10 @@ decode_field_reference (location_t loc,
                               punsignedp, preversep, pvolatilep);
   if ((inner == exp && and_mask == 0)
       || *pbitsize < 0 || offset != 0
-      || TREE_CODE (inner) == PLACEHOLDER_EXPR)
+      || TREE_CODE (inner) == PLACEHOLDER_EXPR
+      /* Reject out-of-bound accesses (PR79731).  */
+      || compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
+                          *pbitpos + *pbitsize) < 0)
     return 0;
 
   *exp_ = exp;
Index: gcc/testsuite/c-c++-common/torture/pr79731.c
===================================================================
--- gcc/testsuite/c-c++-common/torture/pr79731.c        (nonexistent)
+++ gcc/testsuite/c-c++-common/torture/pr79731.c        (working copy)
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-Wno-psabi -w" } */
+
+typedef unsigned V __attribute__ ((vector_size (8)));
+V
+foo (unsigned x, V v)
+{
+  do {
+      v %= x;
+      x = 1;
+  } while (v[1]);
+  return v;
+}
+void fn2 ()
+{
+  V x = foo (5, (V) { 0, 1 });
+  if (x[0] || x[1] || x[2] || x[3]);
+}

Reply via email to