[clang] [clang][sema] Fixed a crash when mixture of designated and non-designated initializers in union (PR #114424)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 31 09:14:37 PDT 2024
https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/114424
Fixed: #113855
When the first init element is invalid, StructuredList can be empty.
It cause illegal state if we still set initialized field.
>From 0e4dd975f2139243bebdffe53aa9fde39a734730 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Fri, 1 Nov 2024 00:13:59 +0800
Subject: [PATCH] [clang][sema] Fixed a crash when mixture of designated and
non-designated initializers in union
Fixed: #113855
When the first init element is invalid, StructuredList can be empty.
It cause illegal state if we still set initialized field.
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaInit.cpp | 9 ++++++---
clang/test/SemaCXX/PR113855.cpp | 15 +++++++++++++++
3 files changed, 22 insertions(+), 3 deletions(-)
create mode 100644 clang/test/SemaCXX/PR113855.cpp
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 at -1 {{mixture of designated and non-designated initializers in the same initializer list is a C99 extension}}
+ // expected-note at -2 {{first non-designated initializer is here}}
+ // expected-error at -3 {{initializer for aggregate with no elements requires explicit braces}}
+}
More information about the cfe-commits
mailing list