<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60928>60928</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
provide "element_count" attribute to give more context to __builtin_dynamic_object_size() and -fsanitize=bounds
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
kees
</td>
</tr>
</table>
<pre>
Frequently a structure containing a flexible array member will also contain a member where the count of array elements is stored. For example:
```
struct foo {
...
unsigned int count;
...
int data[];
};
struct foo *allocate_foo(unsigned int how_many)
{
struct foo *p;
p = malloc(sizeof(*p) + how_many * sizeof(*byte_array));
p->count = how_many;
return p;
}
```
While `__builtin_dynamic_object_size(p->data, 1)` will know the size within `allocate_foo()` due to `malloc`'s `__alloc_size` hinting, this information is immediately lost on return. However, the information _is_ still available in `p->count`, but the compiler has no way to know about it.
Please provide a struct member attribute `element_count` that can be used to associate the size of a flexible array to another struct member. For example:
```
struct foo {
...
unsigned int count;
...
int data[] __attribute__((__element_count__(count)));
};
```
Now any later examination of the size of `data` can be calculated. For example, this equality will hold true:
```
__builtin_dynamic_object_size(p->data) == p->count * sizeof(*p->data)
```
and `-fsanitize-bounds` can examine this as well, to trap:
```
p->data[index] = ...; /* traps when index < 0, or index >= p->count */
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8VcFu4zgM_RrlQjSw5cZJDj5M2wn2tNjbHg3ZpmPtyFJGoppmvn5BOUnjdHaKvQxQuLFM8fE98kkqBL23iJVYPYnVy0JFGpyvviGGReO6U7Xz-D2iJXMCBYF8bCl6hNZZUtpquwcFvcE33RgE5b06wYhjgx6O2hhQJrhLMKjrpwE9Ag2cJ1oC15-3osERLQXQAQI5j90Sds4DvqnxYFAUX0T2IrLLs8zOf-l1qg5650Csn6Y1AIDlcvn-Em0i3IG2NKGL4r9iOaRTpCZtrnFiffP7A7L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