From 61c3c49040b532d94e2371b0049447561ee00319 Mon Sep 17 00:00:00 2001 From: Dodji Seketeli Date: Tue, 3 Nov 2009 10:44:36 +0000 Subject: [PATCH] re PR c++/38699 (ICE using offsetof with pointer and array accesses) Fix PR c++/38699 gcc/ChangeLog: PR c++/38699 * c-common.c (fold_offsetof_1): Issue errors when the member designator of the offsetoff expression is not legitimate. gcc/testsuite/ChangeLog: * c-c++-common/dfp/builtin-offsetof.c: New test. * g++.dg/other/offsetof6.C: Likewise. From-SVN: r153843 --- gcc/ChangeLog | 6 +++++ gcc/c-common.c | 23 ++++++++++++------ gcc/testsuite/ChangeLog | 6 +++++ gcc/testsuite/c-c++-common/dfp/builtin-offsetof.c | 29 +++++++++++++++++++++++ gcc/testsuite/g++.dg/other/offsetof6.C | 26 ++++++++++++++++++++ 5 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/dfp/builtin-offsetof.c create mode 100644 gcc/testsuite/g++.dg/other/offsetof6.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index f9f37d8..ddb6b5d 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2009-11-03 Dodji Seketeli + + PR c++/38699 + * c-common.c (fold_offsetof_1): Issue errors when the member designator + of the offsetoff expression is not legitimate. + 2009-11-03 Uros Bizjak * config/i386/i386.md (*call_value_1_rex64_ms_sysv): Use register diff --git a/gcc/c-common.c b/gcc/c-common.c index 8b85f66..91de41c 100644 --- a/gcc/c-common.c +++ b/gcc/c-common.c @@ -8356,15 +8356,14 @@ fold_offsetof_1 (tree expr, tree stop_ref) error ("cannot apply % when % is overloaded"); return error_mark_node; - case INTEGER_CST: - gcc_assert (integer_zerop (expr)); - return size_zero_node; - case NOP_EXPR: case INDIRECT_REF: - base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref); - gcc_assert (base == error_mark_node || base == size_zero_node); - return base; + if (!integer_zerop (TREE_OPERAND (expr, 0))) + { + error ("cannot apply % to a non constant address"); + return error_mark_node; + } + return size_zero_node; case COMPONENT_REF: base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref); @@ -8397,6 +8396,16 @@ fold_offsetof_1 (tree expr, tree stop_ref) } t = convert (sizetype, t); 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 %ld denotes an offset greater than size of %qT", + index, TREE_TYPE (TREE_OPERAND (expr, 0))); + } break; case COMPOUND_EXPR: diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 672d36d..03fdb9c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2009-11-03 Dodji Seketeli + + PR c++/38699 + * c-c++-common/dfp/builtin-offsetof.c: New test. + * g++.dg/other/offsetof6.C: Likewise. + 2009-11-03 Uros Bizjak PR target/41900 diff --git a/gcc/testsuite/c-c++-common/dfp/builtin-offsetof.c b/gcc/testsuite/c-c++-common/dfp/builtin-offsetof.c new file mode 100644 index 0000000..0ab498a --- /dev/null +++ b/gcc/testsuite/c-c++-common/dfp/builtin-offsetof.c @@ -0,0 +1,29 @@ +// Contributed by Dodji Seketeli +// Origin PR c++/38699 +// { dg-options "-Warray-bounds" } +// { dg-do compile } + +struct A +{ + const char *p; +}; + +struct B +{ + char p[10]; + struct A a; +}; + +void +f0 () +{ + __builtin_offsetof(struct A, p); // OK + __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, a.p); // OK + __builtin_offsetof(struct B, p[0]); // OK + __builtin_offsetof(struct B, a.p[0]); // { dg-error "non constant address" } +} + diff --git a/gcc/testsuite/g++.dg/other/offsetof6.C b/gcc/testsuite/g++.dg/other/offsetof6.C new file mode 100644 index 0000000..b77d1b9 --- /dev/null +++ b/gcc/testsuite/g++.dg/other/offsetof6.C @@ -0,0 +1,26 @@ +// Contributed by Dodji Seketeli +// Origin PR c++/38699 +// { dg-do compile } + +template +struct A +{ + const T *p; +}; + +struct B +{ + A a; +}; + +template class A; + +void +f0 () +{ + __builtin_offsetof(A, p); // OK + __builtin_offsetof(A, p[1]); // { dg-error "non constant address" } + __builtin_offsetof(B, a.p); // OK + __builtin_offsetof(B, a.p[1]); // { dg-error "non constant address" } +} + -- 2.7.4