-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[clang][sema] Fixed a crash when mixture of designated and non-designated initializers in union #114424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ated initializers in union Fixed: llvm#113855 When the first init element is invalid, StructuredList can be empty. It cause illegal state if we still set initialized field.
@llvm/pr-subscribers-clang Author: Congcong Cai (HerrCai0907) ChangesFixed: #113855 Full diff: https://github.com/llvm/llvm-project/pull/114424.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0a1d0fd85e7ae0..a3449b96c313e0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -404,6 +404,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)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 4d11f2a43fcc6b..0158cac5eb7166 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2251,6 +2251,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,
@@ -2497,8 +2501,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);
}
@@ -2583,7 +2586,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);
}
diff --git a/clang/test/SemaCXX/PR113855.cpp b/clang/test/SemaCXX/PR113855.cpp
new file mode 100644
index 00000000000000..fb2a448eca0452
--- /dev/null
+++ b/clang/test/SemaCXX/PR113855.cpp
@@ -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}}
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
…ated 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.
Fixed: #113855
When the first init element is invalid, StructuredList can be empty.
It cause illegal state if we still set initialized field.