Skip to content

Commit

Permalink
[clang] Fix assertion failure when initializing union with FAM
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Jan 8, 2024
1 parent 3574b61 commit 68e21b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2835,7 +2835,7 @@ CharUnits VarDecl::getFlexibleArrayInitChars(const ASTContext &Ctx) const {
if (!Ty || !Ty->getDecl()->hasFlexibleArrayMember())
return CharUnits::Zero();
auto *List = dyn_cast<InitListExpr>(getInit()->IgnoreParens());
if (!List)
if (!List || List->getNumInits() == 0)
return CharUnits::Zero();
const Expr *FlexibleInit = List->getInit(List->getNumInits() - 1);
auto InitTy = Ctx.getAsConstantArrayType(FlexibleInit->getType());
Expand Down
8 changes: 8 additions & 0 deletions clang/test/CodeGen/flexible-array-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ struct __attribute((packed, aligned(4))) { char a; int x; char z[]; } e = { 1, 2

struct { int x; char y[]; } f = { 1, { 13, 15 } };
// CHECK: @f ={{.*}} global <{ i32, [2 x i8] }> <{ i32 1, [2 x i8] c"\0D\0F" }>

union {
struct {
int a;
char b[];
} x;
} in_union = {};
// CHECK: @in_union ={{.*}} global %union.anon zeroinitializer

0 comments on commit 68e21b7

Please sign in to comment.