re PR sanitizer/80348 (UBSAN: compile time crash in ubsan_instrument_division)
authorMarek Polacek <polacek@redhat.com>
Fri, 7 Apr 2017 17:51:55 +0000 (17:51 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Fri, 7 Apr 2017 17:51:55 +0000 (17:51 +0000)
PR sanitizer/80348
* typeck.c (cp_build_binary_op): Convert COP[01] to ORIG_TYPE.

* g++.dg/ubsan/div-by-zero-2.C: New test.

From-SVN: r246770

gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ubsan/div-by-zero-2.C [new file with mode: 0644]

index 9b1f89d8ccda3e1f66bcc8e72c30afe91f522180..5ffd1b74490302ecaef73833704ac82f2c1a386c 100644 (file)
@@ -1,3 +1,8 @@
+2017-04-07  Marek Polacek  <polacek@redhat.com>
+
+       PR sanitizer/80348
+       * typeck.c (cp_build_binary_op): Convert COP[01] to ORIG_TYPE.
+
 2017-04-05  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/80309
index 79391c04fd6452dfadfa8b75d51dab1c0b66861d..65a343581920a2d4acd1c0f9aa5fc31c22e429b3 100644 (file)
@@ -5218,10 +5218,12 @@ cp_build_binary_op (location_t location,
             original result_type.  */
          tree cop0 = op0;
          tree cop1 = op1;
-         if (orig_type != NULL && result_type != orig_type)
+         if (orig_type != NULL_TREE)
            {
-             cop0 = cp_convert (orig_type, op0, complain);
-             cop1 = cp_convert (orig_type, op1, complain);
+             if (TREE_TYPE (cop0) != orig_type)
+               cop0 = cp_convert (orig_type, op0, complain);
+             if (TREE_TYPE (cop1) != orig_type)
+               cop1 = cp_convert (orig_type, op1, complain);
            }
          instrument_expr = ubsan_instrument_division (location, cop0, cop1);
        }
index 94fe5a04af30625626d53dea77108349167d924b..fbaef7a35aeabba170aecbe9b26dd40ed688676e 100644 (file)
@@ -1,3 +1,8 @@
+2017-04-07  Marek Polacek  <polacek@redhat.com>
+
+       PR sanitizer/80348
+       * g++.dg/ubsan/div-by-zero-2.C: New test.
+
 2017-04-07  Vladimir Makarov  <vmakarov@redhat.com>
 
        PR rtl-optimization/70478
diff --git a/gcc/testsuite/g++.dg/ubsan/div-by-zero-2.C b/gcc/testsuite/g++.dg/ubsan/div-by-zero-2.C
new file mode 100644 (file)
index 0000000..d500ae6
--- /dev/null
@@ -0,0 +1,10 @@
+// PR sanitizer/80348
+// { dg-do compile }
+// { dg-options "-fsanitize=integer-divide-by-zero" }
+
+void
+foo ()
+{
+  if (0)
+    unsigned ((0 != 60806) > (0 != 0)) / 0; // { dg-warning "division by zero" }
+}