Skip to content

Commit 2f1a0df

Browse files
authored
[clang][sema] Fixed a crash when mixture of designated and non-designated initializers in union (llvm#114424)
Fixed: llvm#113855 When the first init element is invalid, StructuredList can be empty. It cause illegal state if we still set initialized field.
1 parent 8993367 commit 2f1a0df

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

clang/docs/ReleaseNotes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ Bug Fixes to C++ Support
559559
- Fixed an assertion failure in debug mode, and potential crashes in release mode, when
560560
diagnosing a failed cast caused indirectly by a failed implicit conversion to the type of the constructor parameter.
561561
- Fixed an assertion failure by adjusting integral to boolean vector conversions (#GH108326)
562+
- Fixed a crash when mixture of designated and non-designated initializers in union. (#GH113855)
562563
- Fixed an issue deducing non-type template arguments of reference type. (#GH73460)
563564
- Fixed an issue in constraint evaluation, where type constraints on the lambda expression
564565
containing outer unexpanded parameters were not correctly expanded. (#GH101754)

clang/lib/Sema/SemaInit.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,10 @@ bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity,
22532253
return FlexArrayDiag != diag::ext_flexible_array_init;
22542254
}
22552255

2256+
static bool isInitializedStructuredList(const InitListExpr *StructuredList) {
2257+
return StructuredList && StructuredList->getNumInits() == 1U;
2258+
}
2259+
22562260
void InitListChecker::CheckStructUnionTypes(
22572261
const InitializedEntity &Entity, InitListExpr *IList, QualType DeclType,
22582262
CXXRecordDecl::base_class_const_range Bases, RecordDecl::field_iterator Field,
@@ -2499,8 +2503,7 @@ void InitListChecker::CheckStructUnionTypes(
24992503
StructuredList, StructuredIndex);
25002504
InitializedSomething = true;
25012505
InitializedFields.insert(*Field);
2502-
2503-
if (RD->isUnion() && StructuredList) {
2506+
if (RD->isUnion() && isInitializedStructuredList(StructuredList)) {
25042507
// Initialize the first field within the union.
25052508
StructuredList->setInitializedFieldInUnion(*Field);
25062509
}
@@ -2585,7 +2588,7 @@ void InitListChecker::CheckStructUnionTypes(
25852588
CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index,
25862589
StructuredList, StructuredIndex);
25872590

2588-
if (RD->isUnion() && StructuredList) {
2591+
if (RD->isUnion() && isInitializedStructuredList(StructuredList)) {
25892592
// Initialize the first field within the union.
25902593
StructuredList->setInitializedFieldInUnion(*Field);
25912594
}

clang/test/SemaCXX/PR113855.cpp

+15
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)