]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: throw and private destructor [PR109172]
authorJason Merrill <jason@redhat.com>
Fri, 17 Mar 2023 19:27:10 +0000 (15:27 -0400)
committerJason Merrill <jason@redhat.com>
Fri, 17 Mar 2023 21:31:36 +0000 (17:31 -0400)
Since we aren't going through the normal call machinery, we need to check
the dtor access specifically.

PR c++/109172

gcc/cp/ChangeLog:

* except.cc (build_throw): Check dtor access.

gcc/testsuite/ChangeLog:

* g++.dg/eh/dtor4.C: New test.

gcc/cp/except.cc
gcc/testsuite/g++.dg/eh/dtor4.C [new file with mode: 0644]

index 916e8189db6fde3dc99c03ebd81ce94bf84cb230..91a5e0498601de3e884dd76a119971d530bf3b41 100644 (file)
@@ -639,6 +639,8 @@ build_throw (location_t loc, tree exp)
       tree object, ptr;
       tree allocate_expr;
 
+      tsubst_flags_t complain = tf_warning_or_error;
+
       /* The CLEANUP_TYPE is the internal type of a destructor.  */
       if (!cleanup_type)
        {
@@ -759,11 +761,15 @@ build_throw (location_t loc, tree exp)
       cleanup = NULL_TREE;
       if (type_build_dtor_call (TREE_TYPE (object)))
        {
-         tree dtor_fn = lookup_fnfields (TYPE_BINFO (TREE_TYPE (object)),
+         tree binfo = TYPE_BINFO (TREE_TYPE (object));
+         tree dtor_fn = lookup_fnfields (binfo,
                                          complete_dtor_identifier, 0,
                                          tf_warning_or_error);
          dtor_fn = BASELINK_FUNCTIONS (dtor_fn);
-         mark_used (dtor_fn);
+         if (!mark_used (dtor_fn)
+             || !perform_or_defer_access_check (binfo, dtor_fn,
+                                                dtor_fn, complain))
+           return error_mark_node;
          if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (object)))
            {
              cxx_mark_addressable (dtor_fn);
diff --git a/gcc/testsuite/g++.dg/eh/dtor4.C b/gcc/testsuite/g++.dg/eh/dtor4.C
new file mode 100644 (file)
index 0000000..6c0e804
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/109172
+
+class Demo
+{
+  ~Demo();
+};
+
+int main()
+{
+  try
+    {
+      throw *new Demo;         // { dg-error private }
+    }
+  catch(const Demo& e) { }
+}