The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [RFC PATCH] overflow: Add DECLARE_SIZED_FLEX() helper family
@ 2026-07-14 15:18 Jesse Taube
  2026-07-15 16:12 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: Jesse Taube @ 2026-07-14 15:18 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-hardening, Gustavo A. R. Silva, Kees Cook, Przemek Kitszel,
	Christoph Hellwig, John Meneghini, Chris Leech, Jesse Taube,
	Jesse Taube

Add new DECLARE_SIZED_FLEX() helper to set the default size of a
flexible-array member. The code is identical to the declaration in
__DEFINE_FLEX() which has also been changed to use DECLARE_SIZED_FLEX().

Add DECLARE_COUNTED_FLEX_ARRAY() helper which is a variant of
DECLARE_FLEX_ARRAY() with a counted-by attribute.

Also add default sized variants of
DECLARE_FLEX_ARRAY(), DECLARE_SIZED_FLEX(), and
DECLARE_COUNTED_FLEX_ARRAY(), DECLARE_COUNTED_SIZED_FLEX().

Signed-off-by: Jesse Taube <jtaubepe@redhat.com>
---
 include/linux/overflow.h | 58 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 54 insertions(+), 4 deletions(-)

diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index a8cb6319b4fb..a3384cae49e5 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -464,6 +464,59 @@ static __always_inline size_t __must_check size_sub(size_t minuend, size_t subtr
  */
 #define struct_offset(p, member) (offsetof(typeof(*(p)), member))
 
+/**
+ * __DECLARE_SIZED_FLEX() - helper macro for DECLARE_SIZED_FLEX() family.
+ * Allows for easily declaring a structure with a trailing flexible array member
+ * to have a specific size.
+ *
+ * @obj: object to be given a specific size
+ * @name: Name of the array member.
+ * @count: Number of elements in the array; must be compile-time const.
+ */
+#define __DECLARE_SIZED_FLEX(obj, name, count)	\
+	_Static_assert(__builtin_constant_p(count),		\
+		       "default sized flex array members require compile-time const count");	\
+	union {							\
+		u8 bytes[struct_size_t(obj, name, count)];	\
+		obj;						\
+	}
+
+/**
+ * DECLARE_COUNTED_FLEX_ARRAY() - Declare a counted flexible array
+ *
+ * @type: Type name.
+ * @name: Name of the array member.
+ * @counter: Name of the __counted_by member.
+ */
+#define DECLARE_COUNTED_FLEX_ARRAY(type, name, counter)\
+	struct { \
+		size_t counter; \
+		type name[] __counted_by(counter); \
+	}
+
+/**
+ * DECLARE_SIZED_FLEX() - Declare a structure with a trailing flexible array
+ * member with a default size.
+ *
+ * @type: Type name.
+ * @name: Name of the array member.
+ * @count: Number of elements in the array; must be compile-time const.
+ */
+#define DECLARE_SIZED_FLEX(type, name, count)	\
+	__DECLARE_SIZED_FLEX(type name[], name, count)
+
+/**
+ * DECLARE_COUNTED_SIZED_FLEX() - Declare a structure with a trailing flexible
+ * array member counted by count, with a default size.
+ *
+ * @type: Type name.
+ * @name: Name of the array member.
+ * @counter: Name of the __counted_by member.
+ * @count: Number of elements in the array; must be compile-time const.
+ */
+#define DECLARE_COUNTED_SIZED_FLEX(type, name, counter, count)	\
+	__DECLARE_SIZED_FLEX(DECLARE_COUNTED_FLEX_ARRAY(type, name, counter), name, count)
+
 /**
  * __DEFINE_FLEX() - helper macro for DEFINE_FLEX() family.
  * Enables caller macro to pass arbitrary trailing expressions
@@ -477,10 +530,7 @@ static __always_inline size_t __must_check size_sub(size_t minuend, size_t subtr
 #define __DEFINE_FLEX(type, name, member, count, trailer...)			\
 	_Static_assert(__builtin_constant_p(count),				\
 		       "onstack flex array members require compile-time const count"); \
-	union {									\
-		u8 bytes[struct_size_t(type, member, count)];			\
-		type obj;							\
-	} name##_u trailer;							\
+	__DECLARE_SIZED_FLEX(type obj, member, count) name##_u trailer;		\
 	type *name = (type *)&name##_u
 
 /**
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-15 16:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-14 15:18 [RFC PATCH] overflow: Add DECLARE_SIZED_FLEX() helper family Jesse Taube
2026-07-15 16:12 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox