Author: Congcong Cai
Date: 2024-11-04T22:28:07+08:00
New Revision: 2f1a0df72a3cf5b0f927a8c519cf327c4d0d008e

URL: 
https://github.com/llvm/llvm-project/commit/2f1a0df72a3cf5b0f927a8c519cf327c4d0d008e
DIFF: 
https://github.com/llvm/llvm-project/commit/2f1a0df72a3cf5b0f927a8c519cf327c4d0d008e.diff

LOG: [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.

Added: 
    clang/test/SemaCXX/PR113855.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaInit.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a8e2e5c1e045ce..dc45202f6b2e86 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -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)

diff  --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index e2a59f63ccf589..f13355bb93cbeb 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -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);
   }

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}}
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to