Skip to content

Commit 29bb79e

Browse files
GustavoARSilvakees
authored andcommitted
stddef: Introduce TRAILING_OVERLAP() helper macro
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 <[email protected]> Signed-off-by: Gustavo A. R. Silva <[email protected]> Link: https://lore.kernel.org/r/aFG8gEwKXAWWIvX0@kspp Signed-off-by: Kees Cook <[email protected]>
1 parent 4bfbc26 commit 29bb79e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

include/linux/stddef.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,24 @@ enum {
9393
#define DECLARE_FLEX_ARRAY(TYPE, NAME) \
9494
__DECLARE_FLEX_ARRAY(TYPE, NAME)
9595

96+
/**
97+
* TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members.
98+
*
99+
* Creates a union between a flexible-array member (FAM) in a struct and a set
100+
* of additional members that would otherwise follow it.
101+
*
102+
* @TYPE: Flexible structure type name, including "struct" keyword.
103+
* @NAME: Name for a variable to define.
104+
* @FAM: The flexible-array member within @TYPE
105+
* @MEMBERS: Trailing overlapping members.
106+
*/
107+
#define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS) \
108+
union { \
109+
TYPE NAME; \
110+
struct { \
111+
unsigned char __offset_to_##FAM[offsetof(TYPE, FAM)]; \
112+
MEMBERS \
113+
}; \
114+
}
115+
96116
#endif

0 commit comments

Comments
 (0)