[clang] [Clang] Disable use of the counted_by attribute for whole struct pointers (PR #112636)
Tavian Barnes via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 24 14:32:52 PDT 2024
tavianator wrote:
> 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?
Here's an example:
```c
struct S {
int foo;
char fam[];
};
struct S *s = malloc(9);
s->fam[4];
```
[Your C standard quote](https://github.com/llvm/llvm-project/pull/111015#issuecomment-2392534292) says
> when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed
which I interpret as the largest N such that `sizeof(struct S) <= 9` in
```
struct S {
int foo;
char fam[N];
};
```
Well, for N = 4 we have `sizeof(struct S) == 8` and for N = 5 we have `sizeof(struct S) == 12` (due to alignment padding), therefore N = 4. That makes `s->fam[4]` out-of-bounds. Am I wrong?
https://github.com/llvm/llvm-project/pull/112636
More information about the cfe-commits
mailing list