]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
stddef: Introduce TRAILING_OVERLAP() helper macro
authorGustavo A. R. Silva <gustavoars@kernel.org>
Tue, 17 Jun 2025 19:05:36 +0000 (13:05 -0600)
committerKees Cook <kees@kernel.org>
Wed, 18 Jun 2025 21:30:53 +0000 (14:30 -0700)
Add new TRAILING_OVERLAP() helper macro to create a union between
a flexible-array member (FAM) and a set of members that would
otherwise follow it. This overlays the trailing members onto the
FAM while preserving the original memory layout.

Co-developed-by: Kees Cook <kees@kernel.org>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/aFG8gEwKXAWWIvX0@kspp
Signed-off-by: Kees Cook <kees@kernel.org>
include/linux/stddef.h

index 929d67710cc51e5fd5efe5e012ea2e13eb4dadcd..dab49e2ec8c0af8b13f9502dc622680515828f52 100644 (file)
@@ -93,4 +93,24 @@ enum {
 #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
        __DECLARE_FLEX_ARRAY(TYPE, NAME)
 
+/**
+ * TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members.
+ *
+ * Creates a union between a flexible-array member (FAM) in a struct and a set
+ * of additional members that would otherwise follow it.
+ *
+ * @TYPE: Flexible structure type name, including "struct" keyword.
+ * @NAME: Name for a variable to define.
+ * @FAM: The flexible-array member within @TYPE
+ * @MEMBERS: Trailing overlapping members.
+ */
+#define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS)                             \
+       union {                                                                 \
+               TYPE NAME;                                                      \
+               struct {                                                        \
+                       unsigned char __offset_to_##FAM[offsetof(TYPE, FAM)];   \
+                       MEMBERS                                                 \
+               };                                                              \
+       }
+
 #endif