]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
2010-05-25 Richard Guenther <rguenther@suse.de>
authorrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 25 May 2010 15:49:34 +0000 (15:49 +0000)
committerrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 25 May 2010 15:49:34 +0000 (15:49 +0000)
PR middle-end/44069
* gimple-fold.c (maybe_fold_stmt_addition): Avoid generating
out-of-bounds array accesses.

* g++.dg/torture/pr44069.C: New testcase.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@159824 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/gimple-fold.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/torture/pr44069.C [new file with mode: 0644]

index 3a7e80c1a8e55c1f5ca0e74397a727d1e0aefb6e..02fb290b833963cc30b9fb42e04bf3132ea9ebcf 100644 (file)
@@ -1,3 +1,9 @@
+2010-05-25  Richard Guenther  <rguenther@suse.de>
+
+       PR middle-end/44069
+       * gimple-fold.c (maybe_fold_stmt_addition): Avoid generating
+       out-of-bounds array accesses.
+
 2010-05-25  Richard Guenther  <rguenther@suse.de>
 
        * lto-wrapper.c (nr, input_names, output_names, makefile): Globalize.
index 4fb1b3f0ba7a85534209e63022309c9d19ec9c1c..e74f52457d8379b02b80c4852b65086a2b0a0b5d 100644 (file)
@@ -640,6 +640,18 @@ maybe_fold_stmt_addition (location_t loc, tree res_type, tree op0, tree op1)
          if (!is_gimple_assign (offset_def))
            return NULL_TREE;
 
+         /* As we will end up creating a variable index array access
+            in the outermost array dimension make sure there isn't
+            a more inner array that the index could overflow to.  */
+         if (TREE_CODE (TREE_OPERAND (op0, 0)) == ARRAY_REF)
+           return NULL_TREE;
+
+         /* Do not build array references of something that we can't
+            see the true number of array dimensions for.  */
+         if (!DECL_P (TREE_OPERAND (op0, 0))
+             && !handled_component_p (TREE_OPERAND (op0, 0)))
+           return NULL_TREE;
+
          if (gimple_assign_rhs_code (offset_def) == MULT_EXPR
              && TREE_CODE (gimple_assign_rhs2 (offset_def)) == INTEGER_CST
              && tree_int_cst_equal (gimple_assign_rhs2 (offset_def),
index a65db83dc41cba11fbd5d40a331a15b5d07930c9..42e23ab5c70b4bbf2bd30bb1f5872437be67aab9 100644 (file)
@@ -1,3 +1,8 @@
+2010-05-25  Richard Guenther  <rguenther@suse.de>
+
+       PR middle-end/44069
+       * g++.dg/torture/pr44069.C: New testcase.
+
 2010-05-25  Richard Guenther  <rguenther@suse.de>
 
        * gcc.dg/tree-ssa/sra-10.c: Do not dump esra details.
diff --git a/gcc/testsuite/g++.dg/torture/pr44069.C b/gcc/testsuite/g++.dg/torture/pr44069.C
new file mode 100644 (file)
index 0000000..99fcd17
--- /dev/null
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+
+template <unsigned R, unsigned C>
+class M {
+public:
+    M(const int* arr) {
+       for (unsigned long r = 0; r < R; ++r)
+         for (unsigned long c = 0; c < C; ++c)
+           m[r*C+c] = arr[r*C+c];
+    }
+    int operator()(unsigned r, unsigned c) const
+      { return m[r*C+c]; }
+private:
+    int m[R*C];
+};
+extern "C" void abort (void);
+int main()
+{
+  int vals[2][2] = { { 1, 2 }, { 5, 6 } };
+  M<2,2> m( &(vals[0][0]) );
+  if (m(1,0) != 5)
+    abort ();
+  return 0;
+}
+