https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016
--- Comment #55 from Kees Cook <kees at outflux dot net> --- (In reply to Alejandro Colomar from comment #49) > For reading the counted_by value, that is, for reading the number of > elements in the FAM, I'm implementing a __lengthof__ operator, which returns > the value that the typical ARRAY_SIZE() macro returns, but also works with > FAMs declared with this attribute (and I also want to make it work on > function parameters declared as arrays). > > Consider that for your design of a builtin for setting the counted_by value, > since you don't need to have a built-in for reading the value. ;) > > <https://inbox.sourceware.org/gcc-patches/ > p33myw2zqvtysc3ayfygcvarw4ka3wwltycqjyehdgy64wsgug@ewi3uvid6rrb/T/> > > That is, __lengthof__ should be useful in all of the following cases: > > #define memberof(T, m) (((T *) NULL)->m) > > struct s { > size_t n; > int fam[] __attribute__((counted_by(n))); > }; > > __attribute__((access(read_only, 3, 4))) > void > f(size_t n1, int p[n1], int q[], size_t n2) > { > int a[3]; > size_t m; > struct s *s; > > m = offsetof(struct s, fam) + sizeof(memberof(struct s, fam)[0]) * 7; > s = malloc(m); > > assert(__lengthof__(a) == 3); > assert(__lengthof__(p) == n1); > assert(__lengthof__(q) == n2); > assert(__lengthof__(memberof(struct s, fam)) == m); > } I was expecting "s->n = 7", not " ...= m", and for "__lengthof__(memberof(struct s, fam))" to be "== 7". It should be reporting the array element count, not the bytes, (nor the bytes of the entire allocation).