[clang] [Clang][Sema] Fix crash on a string literal following a designated array element (PR #241933)
Akash Manna via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 15 20:05:58 PST 2026
https://github.com/akash-manna-sky updated https://github.com/llvm/llvm-project/pull/241933
>From 2a46908deb54a442f8cdf8d91cf2f65da6230635 Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Thu, 12 Nov 2026 12:15:17 +0530
Subject: [PATCH] [Clang][Sema] Fix crash on a string literal following a
designated array element
Fixes #139511
The string-literal shortcut in `CheckArrayType` assumes the string starts the array. It also ran when the checker re-entered `CheckArrayType` to finish an array after a designator such as `.y[2] = 'e'`, so `"aaa"` was stored as element 3 of `y` with its array type, and the constant evaluator (C++) or `ConstExprEmitter` (C) asserted on it.
The shortcut now applies only at element index 0. After a designator the string takes the ordinary element path and gets the same error that the top-level `char a[10] = {[2] = 'e', "aaa"}` already does.
LLM tools were used for this contribution. I've reviewed, built, and tested the change myself before pushing to GitHub.
---
clang/docs/ReleaseNotes.md | 1 +
clang/lib/Sema/SemaInit.cpp | 5 +++--
clang/test/Sema/designated-initializers.c | 2 ++
clang/test/SemaCXX/designated-initializers.cpp | 4 ++++
4 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d1dbbbb6eb77b..78edf927e13890 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -822,6 +822,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
(e.g. `({ s.b; })`) was not subject to integer promotion, unlike an ordinary
bit-field access. (#GH221542)
- No longer crashing due to follow-on diagnostics when there is an invalid operand in a logical operator involving a vector operand. (#GH227588)
+- Fixed a crash when a string literal follows a designated initializer for an element of a character array subobject. (#GH139511)
- Fixed assertion failures caused by stale linkage information when an extern variable or function declaration is merged with a preceding static declaration. (#GH204759, #GH204754)
- Fixed a crash due to typo correction mishandling custom keywords `_virtual_inheritance` and `_multiple_inheritance` in `-fms-compatibility` mode. (#GH228003)
- Clang no longer treats a file-scope `thread_local` declaration without an initializer as a tentative definition in C23 mode. As specified by C23 6.9.3, such a declaration is a definition, so declaring the same variable more that once is now diagnosed as a redefinition. (#GH217636)
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 2e3c6309fec20e..d081150088180d 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2156,8 +2156,9 @@ void InitListChecker::CheckArrayType(const InitializedEntity &Entity,
IList->setInit(0, Embed->getDataStringLiteral());
}
- // Check for the special-case of initializing an array with a string.
- if (Index < IList->getNumInits()) {
+ // Check for the special-case of initializing an array with a string. After a
+ // designator the string is the initializer of the next element instead.
+ if (Index < IList->getNumInits() && elementIndex.isZero()) {
if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) ==
SIF_None) {
// We place the string literal directly into the resulting
diff --git a/clang/test/Sema/designated-initializers.c b/clang/test/Sema/designated-initializers.c
index 11dc3a2308dee4..35582b7676bff0 100644
--- a/clang/test/Sema/designated-initializers.c
+++ b/clang/test/Sema/designated-initializers.c
@@ -375,3 +375,5 @@ void gh154046(void) {
[1] = "" // expected-error {{incompatible pointer to integer conversion initializing 'const char' with an expression of type 'char[1]'}}
}[1];
}
+
+const struct { char y[4]; } gh139511 = {.y[2] = 'e', "aaa"}; // expected-error {{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char[4]'}} expected-error {{initializer element is not a compile-time constant}}
diff --git a/clang/test/SemaCXX/designated-initializers.cpp b/clang/test/SemaCXX/designated-initializers.cpp
index a897e5a4e544c4..4f73b2656c5f28 100644
--- a/clang/test/SemaCXX/designated-initializers.cpp
+++ b/clang/test/SemaCXX/designated-initializers.cpp
@@ -177,3 +177,7 @@ namespace ADL {
void g() { f({}, {}); }
}
}
+
+namespace GH139511 {
+ const struct { char y[4]; } a = {.y[2] = 'e', "aaa"}; // expected-error {{cannot initialize an array element of type 'char' with an lvalue of type 'const char[4]'}}
+}
More information about the cfe-commits
mailing list