Skip to content

Commit 86dc6e1

Browse files
[Sema] Fix crash on invalid code with parenthesized aggregate initialization (llvm#76232)
Fixes llvm#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.
1 parent 95b423e commit 86dc6e1

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

clang/lib/Sema/SemaInit.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5512,6 +5512,14 @@ static void TryOrBuildParenListInitialization(
55125512
} else if (auto *RT = Entity.getType()->getAs<RecordType>()) {
55135513
bool IsUnion = RT->isUnionType();
55145514
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
5515+
if (RD->isInvalidDecl()) {
5516+
// Exit early to avoid confusion when processing members.
5517+
// We do the same for braced list initialization in
5518+
// `CheckStructUnionTypes`.
5519+
Sequence.SetFailed(
5520+
clang::InitializationSequence::FK_ParenthesizedListInitFailed);
5521+
return;
5522+
}
55155523

55165524
if (!IsUnion) {
55175525
for (const CXXBaseSpecifier &Base : RD->bases()) {

clang/test/SemaCXX/crash-GH76228.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clang_cc1 -std=c++20 -verify %s
2+
// Check we don't crash on incomplete members and bases when handling parenthesized initialization.
3+
class incomplete; // expected-note@-0 3 {{forward declaration of 'incomplete'}}
4+
struct foo {
5+
int a;
6+
incomplete b;
7+
// expected-error@-1 {{incomplete type}}
8+
};
9+
foo a1(0);
10+
11+
struct one_int {
12+
int a;
13+
};
14+
struct bar : one_int, incomplete {};
15+
// expected-error@-1 {{incomplete type}}
16+
bar a2(0);
17+
18+
incomplete a3[3](1,2,3);
19+
// expected-error@-1 {{incomplete type}}
20+
21+
struct qux : foo {
22+
};
23+
qux a4(0);
24+
25+
struct fred {
26+
foo a[3];
27+
};
28+
fred a5(0);

0 commit comments

Comments
 (0)