[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

Qizhi Hu via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 30 18:46:12 PDT 2024


https://github.com/jcsxky created https://github.com/llvm/llvm-project/pull/87173

Try to fix https://github.com/llvm/llvm-project/issues/75221
This crash caused by calculating record layout which contains a field declaration with dependent type. Make it invalid when report diagnose to prevent this crash. Set the record declaration incomplete bypass the assertion and restore the status when finish setting it invalid.

>From 98c7c28b2e3484d599847f4a4046fc4ebef5a1e0 Mon Sep 17 00:00:00 2001
From: huqizhi <huqizhi at feysh.com>
Date: Sun, 31 Mar 2024 09:38:05 +0800
Subject: [PATCH] [Clang][Sema] set declaration invalid earlier to prevent
 crash in calculating record layout

---
 clang/docs/ReleaseNotes.rst    | 2 ++
 clang/lib/Sema/SemaType.cpp    | 3 +++
 clang/test/SemaCXX/PR75221.cpp | 7 +++++++
 3 files changed, 12 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR75221.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 37b843915a0dee..20578c9b60e33c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -465,6 +465,8 @@ Bug Fixes to C++ Support
   following the first `::` were ignored).
 - Fix an out-of-bounds crash when checking the validity of template partial specializations. (part of #GH86757).
 - Fix an issue caused by not handling invalid cases when substituting into the parameter mapping of a constraint. Fixes (#GH86757).
+- Fix a crash caused by defined struct in a type alias template when the structure
+  has fields with dependent type. Fixes (#GH75221).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index fd94caa4e1d449..973ad20c943bde 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -3899,6 +3899,9 @@ static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
       SemaRef.Diag(OwnedTagDecl->getLocation(), DiagID)
           << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
       D.setInvalidType(true);
+      OwnedTagDecl->setCompleteDefinition(false);
+      OwnedTagDecl->setInvalidDecl();
+      OwnedTagDecl->setCompleteDefinition();
     }
   }
 
diff --git a/clang/test/SemaCXX/PR75221.cpp b/clang/test/SemaCXX/PR75221.cpp
new file mode 100644
index 00000000000000..08b7a06676a8a5
--- /dev/null
+++ b/clang/test/SemaCXX/PR75221.cpp
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s
+// expected-no-diagnostics
+
+template <class T> using foo = struct foo {
+  T size = 0;
+};
+foo a;



More information about the cfe-commits mailing list