dim created this revision.
dim added reviewers: rjmccall, rsmith, majnemer, efriedma.

In some cases, a class type can have a definition, but its destructor
may not yet be known.  In that case, a segfault can occur in
`EmitObjectDelete`.

Avoid it by checking the return value of `CXXRecordDecl::getDestructor`,
and add a minimized test case.

Fixes PR36749 <https://bugs.llvm.org/show_bug.cgi?id=36749>.


Repository:
  rC Clang

https://reviews.llvm.org/D44536

Files:
  lib/CodeGen/CGExprCXX.cpp
  test/CodeGenCXX/pr36749.cpp


Index: test/CodeGenCXX/pr36749.cpp
===================================================================
--- /dev/null
+++ test/CodeGenCXX/pr36749.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -cc1 -triple x86_64-- -emit-llvm-only %s
+class a {
+protected:
+  ~a();
+};
+struct b : a {};
+struct c;
+struct d {
+  c *e;
+  virtual ~d() { delete e; } // expected-warning {{deleting pointer to 
incomplete type 'c' may cause undefined behavior}}
+};
+struct c {
+  b f;
+};
+void g() { new d; }
Index: lib/CodeGen/CGExprCXX.cpp
===================================================================
--- lib/CodeGen/CGExprCXX.cpp
+++ lib/CodeGen/CGExprCXX.cpp
@@ -1862,7 +1862,7 @@
     if (RD->hasDefinition() && !RD->hasTrivialDestructor()) {
       Dtor = RD->getDestructor();
 
-      if (Dtor->isVirtual()) {
+      if (Dtor && Dtor->isVirtual()) {
         CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType,
                                                     Dtor);
         return;


Index: test/CodeGenCXX/pr36749.cpp
===================================================================
--- /dev/null
+++ test/CodeGenCXX/pr36749.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -cc1 -triple x86_64-- -emit-llvm-only %s
+class a {
+protected:
+  ~a();
+};
+struct b : a {};
+struct c;
+struct d {
+  c *e;
+  virtual ~d() { delete e; } // expected-warning {{deleting pointer to incomplete type 'c' may cause undefined behavior}}
+};
+struct c {
+  b f;
+};
+void g() { new d; }
Index: lib/CodeGen/CGExprCXX.cpp
===================================================================
--- lib/CodeGen/CGExprCXX.cpp
+++ lib/CodeGen/CGExprCXX.cpp
@@ -1862,7 +1862,7 @@
     if (RD->hasDefinition() && !RD->hasTrivialDestructor()) {
       Dtor = RD->getDestructor();
 
-      if (Dtor->isVirtual()) {
+      if (Dtor && Dtor->isVirtual()) {
         CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType,
                                                     Dtor);
         return;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to