From: Kees Cook
Sent: Thursday, August 17, 2023 6:00 PM
On Thu, Aug 17, 2023 at 02:35:23PM +0000, David Laight wrote:
quoted
From: Przemek Kitszel
quoted
Sent: Wednesday, August 16, 2023 3:06 PM
...
quoted
quoted
+#define DEFINE_FLEX(type, name, member, count) \
+ union { \
+ u8 bytes[struct_size_t(type, member, count)]; \
+ type obj; \
+ } name##_u __aligned(_Alignof(type)) = {}; \
You shouldn't need the _Alignof() it is the default.
In the sense that since "type" is in the union, it's okay?
The alignment of the union is the larger of the alignments
of all its members.
Which is what you want.
quoted
I'm not sure you should be forcing the memset() either.
This already got discussed: better to fail safe.
Perhaps call it DEFINE_FLEX_Z() to make this clear and
give the option for a non-zeroing version later.
Not everyone wants the expense of zeroing everything.
..
quoted
You might want to add:
Static_assert(is_constexpr(count), "DEFINE_FLEX: non-constant count " #count);
That would be nice, though can Static_assert()s live in the middle of
variable definitions?
I checked and it is fine.
(I double-checked by adding a statement and getting an error.)
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)