[clang] [Sema] Fix crash on invalid code with parenthesized aggregate initialization (PR #76232)

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 22 03:56:33 PST 2023


https://github.com/ilya-biryukov updated https://github.com/llvm/llvm-project/pull/76232

>From 491f3b09a2064c82c1646ca1d0c2987478bb4f51 Mon Sep 17 00:00:00 2001
From: Ilya Biryukov <ibiryukov at google.com>
Date: Fri, 22 Dec 2023 12:33:34 +0100
Subject: [PATCH 1/2] [Sema] Fix crash on invalid code with parenthesized
 aggregate initialization

Fixes #76228.
Use the same logic as braced init lists, also adds a test that puts
incomplete types in various positions to check for regressions in the
future.
---
 clang/lib/Sema/SemaInit.cpp          |  8 ++++++++
 clang/test/SemaCXX/crash-GH76228.cpp | 28 ++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
 create mode 100644 clang/test/SemaCXX/crash-GH76228.cpp

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index d5ba7fd3413718..f768d2726b0a1c 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -5512,6 +5512,14 @@ static void TryOrBuildParenListInitialization(
   } else if (auto *RT = Entity.getType()->getAs<RecordType>()) {
     bool IsUnion = RT->isUnionType();
     const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
+    if (RD->isInvalidDecl()) {
+      // Exit early to avoid confusion when processing members.
+      // We do the same for braced list initialization in
+      // `CheckStructUnionTypes`.
+      Sequence.SetFailed(
+          clang::InitializationSequence::FK_ParenthesizedListInitFailed);
+      return;
+    }
 
     if (!IsUnion) {
       for (const CXXBaseSpecifier &Base : RD->bases()) {
diff --git a/clang/test/SemaCXX/crash-GH76228.cpp b/clang/test/SemaCXX/crash-GH76228.cpp
new file mode 100644
index 00000000000000..a10b9994c5e532
--- /dev/null
+++ b/clang/test/SemaCXX/crash-GH76228.cpp
@@ -0,0 +1,28 @@
+// RUN: %clang_cc1 -std=c++20 -verify %s
+// Check we don't crash on incomplete members and bases when handling parenthesized initialization.
+class incomplete; // expected-note at -0 3  {{forward declaration of 'incomplete'}}
+struct foo {
+  int a;
+  incomplete b;
+  // expected-error at -1 {{incomplete type}}
+};
+foo a1(0);
+
+struct one_int {
+    int a;
+};
+struct bar : one_int, incomplete {};
+// expected-error at -1 {{incomplete type}}
+bar a2(0);
+
+incomplete a3[3](1,2,3);
+// expected-error at -1 {{incomplete type}}
+
+struct qux : foo {
+};
+qux a4(0);
+
+struct fred {
+    foo a[3];
+};
+fred a5(0);
\ No newline at end of file

>From c8b0de00c1836cb6eaf864081139886ead3f20cc Mon Sep 17 00:00:00 2001
From: Ilya Biryukov <ibiryukov at google.com>
Date: Fri, 22 Dec 2023 12:55:51 +0100
Subject: [PATCH 2/2] Add a trailing newline to the test file

---
 clang/test/SemaCXX/crash-GH76228.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/SemaCXX/crash-GH76228.cpp b/clang/test/SemaCXX/crash-GH76228.cpp
index a10b9994c5e532..33a9395823127e 100644
--- a/clang/test/SemaCXX/crash-GH76228.cpp
+++ b/clang/test/SemaCXX/crash-GH76228.cpp
@@ -25,4 +25,4 @@ qux a4(0);
 struct fred {
     foo a[3];
 };
-fred a5(0);
\ No newline at end of file
+fred a5(0);



More information about the cfe-commits mailing list