re PR sanitizer/80349 (UBSAN: compile time crash with "type mismatch in binary expres...
authorJakub Jelinek <jakub@redhat.com>
Wed, 12 Apr 2017 06:12:26 +0000 (08:12 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Wed, 12 Apr 2017 06:12:26 +0000 (08:12 +0200)
PR sanitizer/80349
* fold-const.c (fold_binary_loc) <case BIT_IOR_EXPR>: Convert arg0's
first argument to type.

* g++.dg/ubsan/pr80349.C: New test.

From-SVN: r246865

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ubsan/pr80349.C [new file with mode: 0644]

index 388db6d..f96256b 100644 (file)
@@ -1,3 +1,9 @@
+2017-04-12  Jakub Jelinek  <jakub@redhat.com>
+
+       PR sanitizer/80349
+       * fold-const.c (fold_binary_loc) <case BIT_IOR_EXPR>: Convert arg0's
+       first argument to type.
+
 2017-04-11  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
 
        PR target/80376
index 524bb0f..2f2e93a 100644 (file)
@@ -9916,12 +9916,12 @@ fold_binary_loc (location_t loc,
            }
 
          if (c3 != c1)
-           return fold_build2_loc (loc, BIT_IOR_EXPR, type,
-                                   fold_build2_loc (loc, BIT_AND_EXPR, type,
-                                                    TREE_OPERAND (arg0, 0),
-                                                    wide_int_to_tree (type,
-                                                                      c3)),
-                                   arg1);
+           {
+             tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
+             tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
+                                    wide_int_to_tree (type, c3));
+             return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
+           }
        }
 
       /* See if this can be simplified into a rotate first.  If that
index 354adb0..bf315d3 100644 (file)
@@ -1,3 +1,8 @@
+2017-04-12  Jakub Jelinek  <jakub@redhat.com>
+
+       PR sanitizer/80349
+       * g++.dg/ubsan/pr80349.C: New test.
+
 2017-04-11  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/80370
diff --git a/gcc/testsuite/g++.dg/ubsan/pr80349.C b/gcc/testsuite/g++.dg/ubsan/pr80349.C
new file mode 100644 (file)
index 0000000..f705b92
--- /dev/null
@@ -0,0 +1,11 @@
+// PR sanitizer/80349
+// { dg-do compile }
+// { dg-options "-fsanitize=undefined" }
+
+extern const long long int v;
+
+void
+foo ()
+{
+  (int)((v & 50 | 051UL) << 0) << 0;
+}