From 776248b87e53e18386405561ca260c665ca6bdb7 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Wed, 20 May 2009 23:09:11 +0200 Subject: [PATCH] re PR middle-end/40204 (segfault with bitfields in structs) PR middle-end/40204 * fold-const.c (fold_binary) : Avoid infinite recursion if build_int_cst_type returns the same INTEGER_CST as arg1. * gcc.c-torture/compile/pr40204.c: New test. From-SVN: r147749 --- gcc/ChangeLog | 7 +++++++ gcc/fold-const.c | 8 +++++--- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.c-torture/compile/pr40204.c | 14 ++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr40204.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 25d73d95479..8770426088c 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2009-05-20 Jakub Jelinek + + PR middle-end/40204 + * fold-const.c (fold_binary) : Avoid infinite + recursion if build_int_cst_type returns the same INTEGER_CST as + arg1. + 2009-05-20 Eric Botcazou * fold-const.c (build_fold_addr_expr_with_type): Take the address of diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 0ac9e296c66..e322ecb2cf8 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -11382,6 +11382,8 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1) if (prec < HOST_BITS_PER_WIDE_INT || newmask == ~(unsigned HOST_WIDE_INT) 0) { + tree newmaskt; + if (shift_type != TREE_TYPE (arg0)) { tem = fold_build2 (TREE_CODE (arg0), shift_type, @@ -11392,9 +11394,9 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1) } else tem = op0; - return fold_build2 (BIT_AND_EXPR, type, tem, - build_int_cst_type (TREE_TYPE (op1), - newmask)); + newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask); + if (!tree_int_cst_equal (newmaskt, arg1)) + return fold_build2 (BIT_AND_EXPR, type, tem, newmaskt); } } } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index cca56e5c0db..465ae000009 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2009-05-20 Jakub Jelinek + + PR middle-end/40204 + * gcc.c-torture/compile/pr40204.c: New test. + 2009-05-20 Richard Guenther * gcc.c-torture/compile/20090518-1.c: New testcase. diff --git a/gcc/testsuite/gcc.c-torture/compile/pr40204.c b/gcc/testsuite/gcc.c-torture/compile/pr40204.c new file mode 100644 index 00000000000..3193284ff7a --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr40204.c @@ -0,0 +1,14 @@ +/* PR middle-end/40204 */ + +struct S +{ + unsigned int a : 4; + unsigned int b : 28; +} s; +char c; + +void +f (void) +{ + s.a = (c >> 4) & ~(1 << 4); +} -- 2.30.2