https://github.com/naveen-seth created 
https://github.com/llvm/llvm-project/pull/167171

Fixes #166327

Clang previously hit an assertion in C++ mode when a nested initializer list 
was followed by a designated initializer that referred to a union member of the 
same subobject.

The assertions failing this were logically sound, and this change ensures that 
such initializations are handled correctly during semantic analysis to avoid 
failing any assertion.

>From c8962667df937c7b8dd41ab0e078d278fc4cc2a3 Mon Sep 17 00:00:00 2001
From: naveen-seth <[email protected]>
Date: Sat, 8 Nov 2025 19:53:08 +0100
Subject: [PATCH] [clang][Sema] Fix crash on designated initializer of union
 member in subobject

Fixes #166327

Clang previously hit an assertion in C++ mode when a nested
initializer list was followed by a designated initializer that
referred to a union member of the same subobject.

The assertions failing this were logically sound, and this change
ensures that such initializations are handled correctly during
semantic analysis to avoid failing any assertion.
---
 clang/lib/Sema/SemaInit.cpp                   |  5 +++-
 .../crash-union-designated-initializer.cpp    | 23 +++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/crash-union-designated-initializer.cpp

diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index cc6ddf568d346..13fb22ebfe0ef 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2402,8 +2402,11 @@ void InitListChecker::CheckStructUnionTypes(
         CheckEmptyInitializable(
             InitializedEntity::InitializeMember(*Field, &Entity),
             IList->getEndLoc());
-        if (StructuredList)
+        if (StructuredList) {
           StructuredList->setInitializedFieldInUnion(*Field);
+          StructuredList->resizeInits(SemaRef.Context,
+                                      StructuredList->getNumInits() + 1);
+        }
         break;
       }
     }
diff --git a/clang/test/SemaCXX/crash-union-designated-initializer.cpp 
b/clang/test/SemaCXX/crash-union-designated-initializer.cpp
new file mode 100644
index 0000000000000..7ba40311eac0b
--- /dev/null
+++ b/clang/test/SemaCXX/crash-union-designated-initializer.cpp
@@ -0,0 +1,23 @@
+// Ensures that Clang does not crash in C++ mode, when a nested initializer
+// is followed by a designated initializer for a union member of that same
+// subobject.
+// See issue #166327.
+
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+
+auto main(void) -> int {
+  struct Point {
+    float x;
+    float y;
+    union {
+      int idx;
+      char label;
+    } extra;
+  };
+
+  struct SavePoint {
+    struct Point p;
+  };
+
+  SavePoint save = {.p = {.x = 3.0, .y = 4.0}, .p.extra.label = 'p'}; // 
expected-warning {{nested designators are a C99 extension}}
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to