[clang] [Clang][Sema]: Allow flexible arrays in unions and alone in structs (PR #84428)

Kees Cook via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 20 09:12:30 PDT 2024


kees wrote:

> Is this an existing bug? i.e. it's the CodeGen test for `union { char x[]; } x = {0};` ... :P

Confirmed. Adding a CodeGen test for `union { char x[]; } x = {0};` without any of the changes from this PR still hits the assert. I assume this was from making flex array initialization work in C++ in commit 5955a0f9375a, which only added tests for structs. @efriedma-quic do you know what the "expected" situation should be here? This is the assert in `CodeGenModule::EmitGlobalVarDefinition`:

```
      CharUnits VarSize = getContext().getTypeSizeInChars(ASTTy) +
                          InitDecl->getFlexibleArrayInitChars(getContext());
      CharUnits CstSize = CharUnits::fromQuantity(
          getDataLayout().getTypeAllocSize(Init->getType()));
      assert(VarSize == CstSize && "Emitted constant has unexpected size");
```

It feels like there's logic missing for calculating union size with an initialized flexible array? Taking just my last commit is sufficient to trip this assert (https://github.com/llvm/llvm-project/pull/84428/commits/06bc935771399672d0140340b226b9c2b04e13fd).


https://github.com/llvm/llvm-project/pull/84428


More information about the cfe-commits mailing list