This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix fold_offsetof_1 (PR middle-end/41935)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Jason Merrill <jason at redhat dot com>, "Joseph S. Myers" <joseph at codesourcery dot com>
- Cc: Dodji Seketeli <dodji at redhat dot com>, gcc-patches at gcc dot gnu dot org
- Date: Wed, 4 Nov 2009 13:55:24 +0100
- Subject: [PATCH] Fix fold_offsetof_1 (PR middle-end/41935)
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
Hi!
2009-11-04 Jakub Jelinek <jakub@redhat.com>
PR middle-end/41935
* c-common.c (fold_offsetof_1) <case ARRAY_REF>: Don't crash for VLAs
or non-constant index, allow index one past the last element.
* gcc.dg/pr41935.c: New test.
* c-c++-common/builtin-offsetof.c (f0): Allow index one past the last
element.
* gcc.c-torture/execute/pr41935.c: New test.
--- gcc/c-common.c.jj 2009-11-04 08:22:49.000000000 +0100
+++ gcc/c-common.c 2009-11-04 11:25:16.000000000 +0100
@@ -8398,14 +8398,22 @@ fold_offsetof_1 (tree expr, tree stop_re
off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
/* Check if the offset goes beyond the upper bound of the array. */
- {
- tree nelts = array_type_nelts (TREE_TYPE (TREE_OPERAND (expr, 0)));
- HOST_WIDE_INT index = int_cst_value (t);
- if (index > int_cst_value (nelts))
- warning (OPT_Warray_bounds,
- "index %wd denotes an offset greater than size of %qT",
- index, TREE_TYPE (TREE_OPERAND (expr, 0)));
- }
+ if (code == PLUS_EXPR && TREE_CODE (t) == INTEGER_CST)
+ {
+ tree upbound = array_ref_up_bound (expr);
+ if (upbound != NULL_TREE
+ && TREE_CODE (upbound) == INTEGER_CST
+ && !tree_int_cst_equal (upbound,
+ TYPE_MAX_VALUE (TREE_TYPE (upbound))))
+ {
+ upbound = size_binop (PLUS_EXPR, upbound,
+ build_int_cst (TREE_TYPE (upbound), 1));
+ if (tree_int_cst_lt (upbound, t))
+ warning (OPT_Warray_bounds,
+ "index %E denotes an offset greater than size of %qT",
+ t, TREE_TYPE (TREE_OPERAND (expr, 0)));
+ }
+ }
break;
case COMPOUND_EXPR:
--- gcc/testsuite/gcc.dg/pr41935.c.jj 2009-11-04 10:38:56.000000000 +0100
+++ gcc/testsuite/gcc.dg/pr41935.c 2009-11-04 10:34:39.000000000 +0100
@@ -0,0 +1,25 @@
+/* PR middle-end/41935 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+extern void abort (void);
+struct A { int a; int b[10]; };
+
+int
+foo (struct A *p)
+{
+ return __builtin_offsetof (struct A, b[p->a]);
+}
+
+int
+main ()
+{
+ struct A a;
+ a.a = 7;
+ if (foo (&a) != 7 * sizeof (int) + __builtin_offsetof (struct A, b))
+ abort ();
+ a.a = 2;
+ if (foo (&a) != 2 * sizeof (int) + __builtin_offsetof (struct A, b))
+ abort ();
+ return 0;
+}
--- gcc/testsuite/c-c++-common/builtin-offsetof.c.jj 2009-11-04 08:15:30.000000000 +0100
+++ gcc/testsuite/c-c++-common/builtin-offsetof.c 2009-11-04 10:55:30.000000000 +0100
@@ -21,9 +21,9 @@ f0 ()
__builtin_offsetof(struct A, p[0]); // { dg-error "non constant address" }
__builtin_offsetof(struct B, p[0]); // OK
__builtin_offsetof(struct B, p[9]); // OK
- __builtin_offsetof(struct B, p[10]); // { dg-warning "greater than size" }
+ __builtin_offsetof(struct B, p[10]); // OK
+ __builtin_offsetof(struct B, p[11]); // { dg-warning "greater than size" }
__builtin_offsetof(struct B, a.p); // OK
__builtin_offsetof(struct B, p[0]); // OK
__builtin_offsetof(struct B, a.p[0]); // { dg-error "non constant address" }
}
-
--- gcc/testsuite/gcc.c-torture/execute/pr41935.c.jj 2009-11-04 11:44:24.000000000 +0100
+++ gcc/testsuite/gcc.c-torture/execute/pr41935.c 2009-11-04 11:51:08.000000000 +0100
@@ -0,0 +1,25 @@
+/* PR middle-end/41935 */
+
+extern void abort (void);
+
+long int
+foo (int n, int i, int j)
+{
+ typedef int T[n];
+ struct S { int a; T b[n]; };
+ return __builtin_offsetof (struct S, b[i][j]);
+}
+
+int
+main (void)
+{
+ typedef int T[5];
+ struct S { int a; T b[5]; };
+ if (foo (5, 2, 3)
+ != __builtin_offsetof (struct S, b) + (5 * 2 + 3) * sizeof (int))
+ abort ();
+ if (foo (5, 5, 5)
+ != __builtin_offsetof (struct S, b) + (5 * 5 + 5) * sizeof (int))
+ abort ();
+ return 0;
+}
Jakub