[clang] [Clang] Disable use of the counted_by attribute for whole struct pointers (PR #112636)

Jan Hendrik Farr via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 24 14:16:52 PDT 2024


Cydox wrote:

> For the record, I think the most correct definition, in terms of "this is how much memory you should allocate for a struct with a flexible array member" is this:
> 
> ```c
> max(
>     sizeof(struct S),      // always at least the size of the struct itself
>     round_up(
>         alignof(struct S), // size must be a multiple of alignment
>         offsetof(struct S, fam) + count * sizeof(((struct S *)0)->fam[0])
>     )
> )
> ```

I mean it would be useful to round up to the alignment for when you wanne have an array of the structs, but I'm not sure this is actually required by the standard. Do you have more justification for the alignment requirement on structs containing FAMs?

I would argue the minimum "legal" amount to allocate is:
```C
max(
    sizeof(struct S),
    offsetof(struct S, fam) + count * sizeof(((struct S *)0)->fam[0])
)
```

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


More information about the cfe-commits mailing list