diff options
author | Jakub Jelinek <jakub@redhat.com> | 2009-11-06 09:46:45 +0100 |
---|---|---|
committer | Jakub Jelinek <jakub@gcc.gnu.org> | 2009-11-06 09:46:45 +0100 |
commit | d32599a6e99da9345d2b7d94834b7e9dfcc109a8 (patch) | |
tree | ea3cde61de4ddf98d55d21d6887d89e8f61719f0 /gcc/c-common.c | |
parent | 21a15d9f83dda328d9adfb6deede8d06b8a84c0d (diff) | |
download | gcc-d32599a6e99da9345d2b7d94834b7e9dfcc109a8.tar.gz |
re PR c/41935 (ICE : tree check: expected integer_cst, have nop_expr in int_cst_value, at tree.c:8301)
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 and
allow exceeding array bound in arrays that might be used as flexible
array members.
* gcc.dg/pr41935.c: New test.
* c-c++-common/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.
From-SVN: r153962
Diffstat (limited to 'gcc/c-common.c')
-rw-r--r-- | gcc/c-common.c | 48 |
1 files changed, 40 insertions, 8 deletions
diff --git a/gcc/c-common.c b/gcc/c-common.c index a9ca9608db1..20b24f0c3c2 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -8398,14 +8398,46 @@ fold_offsetof_1 (tree expr, tree stop_ref) 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)) + { + tree v; + + for (v = TREE_OPERAND (expr, 0); + TREE_CODE (v) == COMPONENT_REF; + v = TREE_OPERAND (v, 0)) + if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) + == RECORD_TYPE) + { + tree fld_chain = TREE_CHAIN (TREE_OPERAND (v, 1)); + for (; fld_chain; fld_chain = TREE_CHAIN (fld_chain)) + if (TREE_CODE (fld_chain) == FIELD_DECL) + break; + + if (fld_chain) + break; + } + /* Don't warn if the array might be considered a poor + man's flexible array member with a very permissive + definition thereof. */ + if (TREE_CODE (v) == ARRAY_REF + || TREE_CODE (v) == COMPONENT_REF) + 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: |