[clang][sema] Fixed a crash when mixture of designated and non-designated initializers in union (#114424)

Fixed: #113855
When the first init element is invalid, StructuredList can be empty.
It cause illegal state if we still set initialized field.
This commit is contained in:
Congcong Cai 2024-11-04 22:28:07 +08:00 committed by GitHub
parent 899336735a
commit 2f1a0df72a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View File

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

View File

@ -2253,6 +2253,10 @@ bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity,
return FlexArrayDiag != diag::ext_flexible_array_init;
}
static bool isInitializedStructuredList(const InitListExpr *StructuredList) {
return StructuredList && StructuredList->getNumInits() == 1U;
}
void InitListChecker::CheckStructUnionTypes(
const InitializedEntity &Entity, InitListExpr *IList, QualType DeclType,
CXXRecordDecl::base_class_const_range Bases, RecordDecl::field_iterator Field,
@ -2499,8 +2503,7 @@ void InitListChecker::CheckStructUnionTypes(
StructuredList, StructuredIndex);
InitializedSomething = true;
InitializedFields.insert(*Field);
if (RD->isUnion() && StructuredList) {
if (RD->isUnion() && isInitializedStructuredList(StructuredList)) {
// Initialize the first field within the union.
StructuredList->setInitializedFieldInUnion(*Field);
}
@ -2585,7 +2588,7 @@ void InitListChecker::CheckStructUnionTypes(
CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index,
StructuredList, StructuredIndex);
if (RD->isUnion() && StructuredList) {
if (RD->isUnion() && isInitializedStructuredList(StructuredList)) {
// Initialize the first field within the union.
StructuredList->setInitializedFieldInUnion(*Field);
}

View File

@ -0,0 +1,15 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
struct S {};
union U {
S x;
float y;
};
void f() {
new U{0,.y=1};
// expected-warning@-1 {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}}
// expected-note@-2 {{first non-designated initializer is here}}
// expected-error@-3 {{initializer for aggregate with no elements requires explicit braces}}
}