Re:How to use the new counted_by attribute in C (and Linux)
by db48x on Saturday July 20, 2024 @03:42PM
(#41019163)
It’s just as easy to make that mistake, but at least then the compiler can issue a warning.
Re:How to use the new counted_by attribute in C (and Linux)
by akira2501 on Saturday July 20, 2024 @05:39PM
(#41020201)
It's better than that. If you use FORTIFY_SOURCE then you will get _runtime_ bounds checking.
So, while you still have to make sure your structure is sane, now the runtime can play along too.
> One crucial requirement is that the counter must be initialized before the first reference to the flexible-array member. Another requirement is that the array must always contain at least as many elements as indicated by the counter.
This means as the array is initialized and updated, you must keep the counter updated with it. Its just as easy to screw that up than it is to screw up the general problem of accessing elements that are out-of-bounds