From ea9d9d749c3203e9fb01267fabecad93b7f1c06c Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Thu, 14 Mar 2019 09:24:21 +0000 Subject: [PATCH] re PR c++/89698 (Run-time error due to optimization of field access after cast at -Os/-O2 and higher) 2019-03-14 Richard Biener PR middle-end/89698 * fold-const.c (operand_equal_p): For INDIRECT_REF check that the access types are similar. * g++.dg/torture/pr89698.C: New testcase. From-SVN: r269677 --- gcc/ChangeLog | 6 ++++++ gcc/fold-const.c | 14 +++++++++---- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/torture/pr89698.C | 28 ++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/torture/pr89698.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 8c3a6b966f2..2d2495cba27 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2019-03-14 Richard Biener + + PR middle-end/89698 + * fold-const.c (operand_equal_p): For INDIRECT_REF check + that the access types are similar. + 2019-03-14 Jakub Jelinek PR tree-optimization/89703 diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 571566aa6bc..dbc96798e65 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -3220,10 +3220,16 @@ operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags) switch (TREE_CODE (arg0)) { case INDIRECT_REF: - if (!(flags & OEP_ADDRESS_OF) - && (TYPE_ALIGN (TREE_TYPE (arg0)) - != TYPE_ALIGN (TREE_TYPE (arg1)))) - return 0; + if (!(flags & OEP_ADDRESS_OF)) + { + if (TYPE_ALIGN (TREE_TYPE (arg0)) + != TYPE_ALIGN (TREE_TYPE (arg1))) + return 0; + /* Verify that the access types are compatible. */ + if (TYPE_MAIN_VARIANT (TREE_TYPE (arg0)) + != TYPE_MAIN_VARIANT (TREE_TYPE (arg1))) + return 0; + } flags &= ~OEP_ADDRESS_OF; return OP_SAME (0); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index db45dc4fdfc..b0f9efa63fe 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2019-03-14 Richard Biener + + PR middle-end/89698 + * g++.dg/torture/pr89698.C: New testcase. + 2019-03-14 Jakub Jelinek PR tree-optimization/89703 diff --git a/gcc/testsuite/g++.dg/torture/pr89698.C b/gcc/testsuite/g++.dg/torture/pr89698.C new file mode 100644 index 00000000000..fbeb7976c0f --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/pr89698.C @@ -0,0 +1,28 @@ +/* { dg-do run } */ + +extern "C" void abort (void); + +class A { + virtual void f(){}; +public: + int x; + A(int in): x(in) {}; +}; + +class B: public A { +public: + int y; + B(int in):A(in-1), y(in) {}; +}; + +int test(void) +{ + int res; + B b(2); + A* bp = &b; + void* vp = dynamic_cast(bp); + if (((A*)vp)->x == 1 && ((B*)vp)->y == 2) + return 1; + return 0; +} +int main() { if (test() != 1) abort (); return 0; } -- 2.30.2