[clang] [WinEH] Fix object delete crash (PR #180144)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 26 18:10:24 PDT 2026


================
@@ -90,3 +89,35 @@ int bar()
     }
   }
 }
+
+// CHECK-LABEL: @"?foo1@@YAXXZ"()
+// CHECK-NOT: invoke void @llvm.seh.scope.begin()
+// CHECK:     invoke void @llvm.seh.try.begin()
+// CHECK:     invoke.cont:
+// CHECK:     invoke void @llvm.seh.scope.begin()
+// CHECK:     invoke noundef ptr @"??0A@@QEAA at XZ"
+// CHECK:     invoke void @llvm.seh.scope.end()
+// CHECK:     invoke void @llvm.seh.scope.begin()
+// CHECK:     invoke void @"??1A@@QEAA at XZ"
+// CHECK:     invoke void @llvm.seh.scope.end()
+// CHECK-NOT: invoke void @llvm.seh.try.end()
+// CHECK:     br label %delete.end
+// CHECK:     invoke void @llvm.seh.try.end()
+// CHECK:     call void @"?fin$0 at 0@foo1@@"(i8 noundef 0
+// CHECK:     cleanupret
+// CHECK:     call void @"?fin$0 at 0@foo1@@"(i8 noundef 1
+// CHECK-NOT: @llvm.seh.scope.end
+// CHECK:     cleanupret
+struct A {
+	A();
+	~A();
+};
+
+void foo1() {
+	__try {
----------------
efriedma-quic wrote:

Looking at the IR here, it looks like what happens is that you emit what is, in essence, a nested __finally block to call delete.  Structurally, it's the same IR we'd emit for C++ exception-handling, so the result is well-formed. But I'd guess there's a reason MSVC refuses to do this?

https://developercommunity.visualstudio.com/t/c2712-generated-even-when-seh-does-not-traverse-sc/1692745 has some related discussion.

https://github.com/llvm/llvm-project/pull/180144


More information about the cfe-commits mailing list