Skip to content

Commit 0e4dd97

Browse files
committed
[clang][sema] Fixed a crash when mixture of designated and non-designated initializers in union
Fixed: #113855 When the first init element is invalid, StructuredList can be empty. It cause illegal state if we still set initialized field.
1 parent 9c5ad62 commit 0e4dd97

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ Bug Fixes to C++ Support
404404
- Fixed an assertion failure in debug mode, and potential crashes in release mode, when
405405
diagnosing a failed cast caused indirectly by a failed implicit conversion to the type of the constructor parameter.
406406
- Fixed an assertion failure by adjusting integral to boolean vector conversions (#GH108326)
407+
- Fixed a crash when mixture of designated and non-designated initializers in union. (#GH113855)
407408

408409
Bug Fixes to AST Handling
409410
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaInit.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,6 +2251,10 @@ bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity,
22512251
return FlexArrayDiag != diag::ext_flexible_array_init;
22522252
}
22532253

2254+
static bool isInitializedStructuredList(const InitListExpr *StructuredList) {
2255+
return StructuredList && StructuredList->getNumInits() == 1U;
2256+
}
2257+
22542258
void InitListChecker::CheckStructUnionTypes(
22552259
const InitializedEntity &Entity, InitListExpr *IList, QualType DeclType,
22562260
CXXRecordDecl::base_class_const_range Bases, RecordDecl::field_iterator Field,
@@ -2497,8 +2501,7 @@ void InitListChecker::CheckStructUnionTypes(
24972501
StructuredList, StructuredIndex);
24982502
InitializedSomething = true;
24992503
InitializedFields.insert(*Field);
2500-
2501-
if (RD->isUnion() && StructuredList) {
2504+
if (RD->isUnion() && isInitializedStructuredList(StructuredList)) {
25022505
// Initialize the first field within the union.
25032506
StructuredList->setInitializedFieldInUnion(*Field);
25042507
}
@@ -2583,7 +2586,7 @@ void InitListChecker::CheckStructUnionTypes(
25832586
CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index,
25842587
StructuredList, StructuredIndex);
25852588

2586-
if (RD->isUnion() && StructuredList) {
2589+
if (RD->isUnion() && isInitializedStructuredList(StructuredList)) {
25872590
// Initialize the first field within the union.
25882591
StructuredList->setInitializedFieldInUnion(*Field);
25892592
}

clang/test/SemaCXX/PR113855.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
3+
struct S {};
4+
5+
union U {
6+
S x;
7+
float y;
8+
};
9+
10+
void f() {
11+
new U{0,.y=1};
12+
// expected-warning@-1 {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}}
13+
// expected-note@-2 {{first non-designated initializer is here}}
14+
// expected-error@-3 {{initializer for aggregate with no elements requires explicit braces}}
15+
}

0 commit comments

Comments
 (0)