re PR c++/68508 (Internal compiler error with parentheses around return value in...
authorJakub Jelinek <jakub@redhat.com>
Thu, 26 Nov 2015 09:52:48 +0000 (10:52 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Thu, 26 Nov 2015 09:52:48 +0000 (10:52 +0100)
PR c++/68508
* cp-tree.h (cp_ubsan_maybe_instrument_downcast): Add INTYPE argument.
* cp-ubsan.c (cp_ubsan_maybe_instrument_downcast): Likewise.  Use
it instead of or in addition to TREE_TYPE (op).  Use
is_properly_derived_from, return NULL_TREE if TREE_TYPE (intype) and
TREE_TYPE (type) are the same type minus qualifiers.
* typeck.c (build_static_cast_1): Adjust callers.

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

From-SVN: r230928

gcc/cp/ChangeLog
gcc/cp/cp-tree.h
gcc/cp/cp-ubsan.c
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ubsan/pr68508.C [new file with mode: 0644]

index 82a29a3b0f63b36ba995535ba3540d0ffccb1730..90d86dca052bf7d6ba81f707129f7c02250be10f 100644 (file)
@@ -1,3 +1,13 @@
+2015-11-26  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/68508
+       * cp-tree.h (cp_ubsan_maybe_instrument_downcast): Add INTYPE argument.
+       * cp-ubsan.c (cp_ubsan_maybe_instrument_downcast): Likewise.  Use
+       it instead of or in addition to TREE_TYPE (op).  Use
+       is_properly_derived_from, return NULL_TREE if TREE_TYPE (intype) and
+       TREE_TYPE (type) are the same type minus qualifiers.
+       * typeck.c (build_static_cast_1): Adjust callers.
+
 2015-11-25  Martin Sebor  <msebor@redhat.com>
 
        PR c++/67876
index 1672291f9b691d5f14c40d8cbf7ebe1277cd2794..caa601d1c98ab71da1615977fd9b9f8b80833c14 100644 (file)
@@ -6854,7 +6854,7 @@ extern bool cilk_valid_spawn                    (tree);
 /* In cp-ubsan.c */
 extern void cp_ubsan_maybe_instrument_member_call (tree);
 extern void cp_ubsan_instrument_member_accesses (tree *);
-extern tree cp_ubsan_maybe_instrument_downcast (location_t, tree, tree);
+extern tree cp_ubsan_maybe_instrument_downcast (location_t, tree, tree, tree);
 extern tree cp_ubsan_maybe_instrument_cast_to_vbase (location_t, tree, tree);
 
 /* -- end of C++ */
index e780c2ef2f0fd3a9394808baab9bec1450533dcb..6ffeb16ce1d20bebe9afee37568e68094aa714bd 100644 (file)
@@ -243,13 +243,14 @@ cp_ubsan_instrument_member_accesses (tree *t_p)
 /* Instrument downcast.  */
 
 tree
-cp_ubsan_maybe_instrument_downcast (location_t loc, tree type, tree op)
+cp_ubsan_maybe_instrument_downcast (location_t loc, tree type,
+                                   tree intype, tree op)
 {
   if (!POINTER_TYPE_P (type)
+      || !POINTER_TYPE_P (intype)
       || !POINTER_TYPE_P (TREE_TYPE (op))
-      || !CLASS_TYPE_P (TREE_TYPE (type))
       || !CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
-      || !DERIVED_FROM_P (TREE_TYPE (TREE_TYPE (op)), TREE_TYPE (type)))
+      || !is_properly_derived_from (TREE_TYPE (type), TREE_TYPE (intype)))
     return NULL_TREE;
 
   return cp_ubsan_maybe_instrument_vptr (loc, op, TREE_TYPE (type), true,
index 95178905f9e5884fc4c32a13bad36afac0317d0a..1d2943f50fb1d1f286f0453dff020fa6328459a4 100644 (file)
@@ -6590,7 +6590,8 @@ build_static_cast_1 (tree type, tree expr, bool c_cast_p,
       if (flag_sanitize & SANITIZE_VPTR)
        {
          tree ubsan_check
-           = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
+           = cp_ubsan_maybe_instrument_downcast (input_location, type,
+                                                 intype, expr);
          if (ubsan_check)
            expr = ubsan_check;
        }
@@ -6737,7 +6738,8 @@ build_static_cast_1 (tree type, tree expr, bool c_cast_p,
       if (flag_sanitize & SANITIZE_VPTR)
        {
          tree ubsan_check
-           = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
+           = cp_ubsan_maybe_instrument_downcast (input_location, type,
+                                                 intype, expr);
          if (ubsan_check)
            expr = ubsan_check;
        }
index 9d022fce86be8c1971c5a5239a40e5ba4fa8c5c6..2e9f9623db8ff9b1fc44c68b4eb9c41235ec8398 100644 (file)
@@ -1,3 +1,8 @@
+2015-11-26  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/68508
+       * g++.dg/ubsan/pr68508.C: New test.
+
 2015-11-25  Martin Sebor  <msebor@redhat.com>
 
        PR c++/67876
diff --git a/gcc/testsuite/g++.dg/ubsan/pr68508.C b/gcc/testsuite/g++.dg/ubsan/pr68508.C
new file mode 100644 (file)
index 0000000..ffe8f00
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/68508
+// { dg-do compile }
+// { dg-options "-std=c++14 -fsanitize=vptr" }
+
+struct A
+{
+  virtual int foo () { return 0; }
+};
+
+const A &
+bar ()
+{
+  static A x = A ();
+  return (x);
+}