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 <[email protected]> Date: Thu, 12 Nov 2026 12:15:17 +0530 Subject: [PATCH 1/2] [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]'}} +} >From 7cb4ea8f46639be8d52b111be69ae6a1f2a9c8fd Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Thu, 19 Nov 2026 09:09:39 +0530 Subject: [PATCH 2/2] [Clang][Sema] Merge the string special-case conditions in CheckArrayType Address review feedback: fold the IsStringInit() check into the enclosing if. No functional change. --- clang/lib/Sema/SemaInit.cpp | 43 ++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index e57a97ddfd7919..aae54e3b3c7e5d 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -2159,29 +2159,28 @@ void InitListChecker::CheckArrayType(const InitializedEntity &Entity, // 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 - // initializer list. This is the only place where the structure - // of the structured initializer list doesn't match exactly, - // because doing so would involve allocating one character - // constant for each string. - // FIXME: Should we do these checks in verify-only mode too? - if (!VerifyOnly) - CheckStringInit( - IList->getInit(Index), DeclType, arrayType, SemaRef, Entity, - SemaRef.getLangOpts().C23 && initializingConstexprVariable(Entity)); - if (StructuredList) { - UpdateStructuredListElement(StructuredList, StructuredIndex, - IList->getInit(Index)); - StructuredList->resizeInits(SemaRef.Context, StructuredIndex); - } - ++Index; - if (AggrDeductionCandidateParamTypes) - AggrDeductionCandidateParamTypes->push_back(DeclType); - return; + if (Index < IList->getNumInits() && elementIndex.isZero() && + IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == + SIF_None) { + // We place the string literal directly into the resulting + // initializer list. This is the only place where the structure + // of the structured initializer list doesn't match exactly, + // because doing so would involve allocating one character + // constant for each string. + // FIXME: Should we do these checks in verify-only mode too? + if (!VerifyOnly) + CheckStringInit( + IList->getInit(Index), DeclType, arrayType, SemaRef, Entity, + SemaRef.getLangOpts().C23 && initializingConstexprVariable(Entity)); + if (StructuredList) { + UpdateStructuredListElement(StructuredList, StructuredIndex, + IList->getInit(Index)); + StructuredList->resizeInits(SemaRef.Context, StructuredIndex); } + ++Index; + if (AggrDeductionCandidateParamTypes) + AggrDeductionCandidateParamTypes->push_back(DeclType); + return; } if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { // Check for VLAs; in standard C it would be possible to check this _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
