Skip to content

Commit 68e21b7

Browse files
committed
[clang] Fix assertion failure when initializing union with FAM
When initializing a union that constrain a struct with a flexible array member, and the initializer list is empty, we currently trigger an assertion failure. This happens because getFlexibleArrayInitChars() assumes that the initializer list is non-empty. Fixes llvm#77085.
1 parent 3574b61 commit 68e21b7

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

clang/lib/AST/Decl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2835,7 +2835,7 @@ CharUnits VarDecl::getFlexibleArrayInitChars(const ASTContext &Ctx) const {
28352835
if (!Ty || !Ty->getDecl()->hasFlexibleArrayMember())
28362836
return CharUnits::Zero();
28372837
auto *List = dyn_cast<InitListExpr>(getInit()->IgnoreParens());
2838-
if (!List)
2838+
if (!List || List->getNumInits() == 0)
28392839
return CharUnits::Zero();
28402840
const Expr *FlexibleInit = List->getInit(List->getNumInits() - 1);
28412841
auto InitTy = Ctx.getAsConstantArrayType(FlexibleInit->getType());

clang/test/CodeGen/flexible-array-init.c

+8
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ struct __attribute((packed, aligned(4))) { char a; int x; char z[]; } e = { 1, 2
2020

2121
struct { int x; char y[]; } f = { 1, { 13, 15 } };
2222
// CHECK: @f ={{.*}} global <{ i32, [2 x i8] }> <{ i32 1, [2 x i8] c"\0D\0F" }>
23+
24+
union {
25+
struct {
26+
int a;
27+
char b[];
28+
} x;
29+
} in_union = {};
30+
// CHECK: @in_union ={{.*}} global %union.anon zeroinitializer

0 commit comments

Comments
 (0)