Skip to content

Commit f5dc302

Browse files
authored
[C] Fix failing assertion with designated inits (#154120)
Incompatible pointer to integer conversion diagnostic checks would trigger an assertion when the designated initializer is for an array of unknown bounds. Fixes #154046
1 parent b368e7f commit f5dc302

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ Bug Fixes in This Version
193193
targets that treat ``_Float16``/``__fp16`` as native scalar types. Previously
194194
the warning was silently lost because the operands differed only by an implicit
195195
cast chain. (#GH149967).
196+
- Fixed a crash with incompatible pointer to integer conversions in designated
197+
initializers involving string literals. (#GH154046)
196198

197199
Bug Fixes to Compiler Builtins
198200
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaInit.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3294,8 +3294,9 @@ InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
32943294
if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) {
32953295
// Get the length of the string.
32963296
uint64_t StrLen = SL->getLength();
3297-
if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen))
3298-
StrLen = cast<ConstantArrayType>(AT)->getZExtSize();
3297+
if (const auto *CAT = dyn_cast<ConstantArrayType>(AT);
3298+
CAT && CAT->getSize().ult(StrLen))
3299+
StrLen = CAT->getZExtSize();
32993300
StructuredList->resizeInits(Context, StrLen);
33003301

33013302
// Build a literal for each character in the string, and put them into
@@ -3317,8 +3318,9 @@ InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
33173318

33183319
// Get the length of the string.
33193320
uint64_t StrLen = Str.size();
3320-
if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen))
3321-
StrLen = cast<ConstantArrayType>(AT)->getZExtSize();
3321+
if (const auto *CAT = dyn_cast<ConstantArrayType>(AT);
3322+
CAT && CAT->getSize().ult(StrLen))
3323+
StrLen = CAT->getZExtSize();
33223324
StructuredList->resizeInits(Context, StrLen);
33233325

33243326
// Build a literal for each character in the string, and put them into

clang/test/Sema/designated-initializers.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,10 @@ struct {
368368
.b = 0, // expected-warning {{initializer overrides prior initialization of this subobject}}
369369
},
370370
};
371+
372+
void gh154046(void) {
373+
(void)(const char[]) {
374+
[0] = "", // expected-error {{incompatible pointer to integer conversion initializing 'const char' with an expression of type 'char[1]'}}
375+
[1] = "" // expected-error {{incompatible pointer to integer conversion initializing 'const char' with an expression of type 'char[1]'}}
376+
}[1];
377+
}

clang/test/SemaObjC/exprs.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ void test_encode(void) {
3636
(void)@encode(Incomplete_ObjC_class*);
3737
(void)@encode(id);
3838
}
39+
40+
void gh154046(void) {
41+
(void)(const char[]) {
42+
[0] = @encode(int), // expected-error {{incompatible pointer to integer conversion initializing 'const char' with an expression of type 'char[2]'}}
43+
[1] = @encode(float) // expected-error {{incompatible pointer to integer conversion initializing 'const char' with an expression of type 'char[2]'}}
44+
}[1];
45+
}

0 commit comments

Comments
 (0)