Re: [PATCH v6 3/5] compiler_types: Introduce __flex_counter() and family

Peter Zijlstra
2025-12-04T08:55:00Z

On Wed, Dec 03, 2025 at 03:30:33PM -0800, Kees Cook wrote:
> Introduce __flex_counter() which wraps __builtin_counted_by_ref(),
> as newly introduced by GCC[1] and Clang[2]. Use of __flex_counter()
> allows access to the counter member of a struct's flexible array member
> when it has been annotated with __counted_by().
>
> Introduce typeof_flex_counter(), overflows_flex_counter_type(), and
> __set_flex_counter() to provide the needed _Generic() wrappers to get
> sane results out of __flex_counter().
>
> For example, with:
>
> struct foo {
> int counter;
> short array[] __counted_by(counter);
> } *p;
>
> __flex_counter(p->array) will resolve to: &p->counter
>
> typeof_flex_counter(p->array) will resolve to "int". (If p->array was not
> annotated, it would resolve to "size_t".)

Uhh, how will this interact with the proposed extension of
__counted_by() to normal pointer types?

Because the moment we have __counted_by() on pointers (remember, you
promised to rename __counted_by_ptr() to __counted_by() once GCC-16
releases) using __flex_counter() on a ptr thing makes absolutely no
sense.

So perhaps just name this thing __counter_of() or something?