@@ -245,13 +245,14 @@ cp_ubsan_instrument_member_accesses (tre
/* 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,
@@ -6854,7 +6854,7 @@ extern bool cilk_valid_spawn
/* 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++ */
@@ -6590,7 +6590,8 @@ build_static_cast_1 (tree type, tree exp
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 exp
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;
}
@@ -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);
+}