[clang] [Clang] prevent checking destructor reference with an invalid initializer (PR #97860)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 12 04:19:56 PDT 2024


https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/97860

>From cb3c677c9eb10998ed7357cdde2722f3b3c1c847 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Sat, 6 Jul 2024 00:24:06 +0300
Subject: [PATCH] [Clang] prevent checking destructor reference with an invalid
 initializer

---
 clang/docs/ReleaseNotes.rst       | 1 +
 clang/lib/Sema/SemaInit.cpp       | 3 +++
 clang/test/SemaCXX/destructor.cpp | 9 +++++++++
 3 files changed, 13 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 36cf615a4287c..b85490376c848 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -963,6 +963,7 @@ Bug Fixes to C++ Support
 - Fixed an assertion failure about invalid conversion when calling lambda. (#GH96205).
 - Fixed a bug where the first operand of binary ``operator&`` would be transformed as if it was the operand
   of the address of operator. (#GH97483).
+- Fix a crash when checking destructor reference with an invalid initializer. (#GH97230).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 64e43ded0961e..aa003c60b25ee 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -1986,6 +1986,9 @@ static bool checkDestructorReference(QualType ElementType, SourceLocation Loc,
     return false;
 
   CXXDestructorDecl *Destructor = SemaRef.LookupDestructor(CXXRD);
+  if (!Destructor)
+    return false;
+
   SemaRef.CheckDestructorAccess(Loc, Destructor,
                                 SemaRef.PDiag(diag::err_access_dtor_temp)
                                 << ElementType);
diff --git a/clang/test/SemaCXX/destructor.cpp b/clang/test/SemaCXX/destructor.cpp
index 028bc7cc19698..dfcd1b033af5a 100644
--- a/clang/test/SemaCXX/destructor.cpp
+++ b/clang/test/SemaCXX/destructor.cpp
@@ -577,4 +577,13 @@ static_assert(!__is_trivially_constructible(Foo, const Foo &), "");
 static_assert(!__is_trivially_constructible(Foo, Foo &&), "");
 } // namespace GH89544
 
+namespace GH97230 {
+struct X {
+  ~X() = defaul; // expected-error {{initializer on function does not look like a pure-specifier}} \
+                 // expected-error {{use of undeclared identifier 'defaul'}}
+};
+struct Y : X {} y1{ }; // expected-error {{call to implicitly-deleted default constructor of 'struct Y'}} \
+                       // expected-note {{default constructor of 'Y' is implicitly deleted because base class 'X' has no destructor}}
+}
+
 #endif // BE_THE_HEADER



More information about the cfe-commits mailing list