Skip to content

Commit 2bbdcf0

Browse files
GustavoARSilvakees
authored andcommitted
stddef: Introduce __TRAILING_OVERLAP()
Introduce underlying __TRAILING_OVERLAP() macro to let callers apply atributes to trailing overlapping members. For instance, the code below: | struct flex { | size_t count; | int data[]; | }; | struct { | struct flex f; | struct foo a; | struct boo b; | } __packed instance; can now be changed to the following, and preserve the __packed attribute: | __TRAILING_OVERLAP(struct flex, f, data, __packed, | struct foo a; | struct boo b; | ) instance; Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Link: https://lore.kernel.org/r/f80c529b239ce11f0a51f714fe00ddf839e05f5e.1758115257.git.gustavoars@kernel.org Signed-off-by: Kees Cook <kees@kernel.org>
1 parent 413187f commit 2bbdcf0

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

include/linux/stddef.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,39 @@ enum {
9494
__DECLARE_FLEX_ARRAY(TYPE, NAME)
9595

9696
/**
97-
* TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members.
97+
* __TRAILING_OVERLAP() - Overlap a flexible-array member with trailing
98+
* members.
9899
*
99100
* Creates a union between a flexible-array member (FAM) in a struct and a set
100101
* of additional members that would otherwise follow it.
101102
*
102103
* @TYPE: Flexible structure type name, including "struct" keyword.
103104
* @NAME: Name for a variable to define.
104105
* @FAM: The flexible-array member within @TYPE
106+
* @ATTRS: Any struct attributes (usually empty)
105107
* @MEMBERS: Trailing overlapping members.
106108
*/
107-
#define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS) \
109+
#define __TRAILING_OVERLAP(TYPE, NAME, FAM, ATTRS, MEMBERS) \
108110
union { \
109111
TYPE NAME; \
110112
struct { \
111113
unsigned char __offset_to_FAM[offsetof(TYPE, FAM)]; \
112114
MEMBERS \
113-
}; \
115+
} ATTRS; \
114116
}
115117

118+
/**
119+
* TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members.
120+
*
121+
* Creates a union between a flexible-array member (FAM) in a struct and a set
122+
* of additional members that would otherwise follow it.
123+
*
124+
* @TYPE: Flexible structure type name, including "struct" keyword.
125+
* @NAME: Name for a variable to define.
126+
* @FAM: The flexible-array member within @TYPE
127+
* @MEMBERS: Trailing overlapping members.
128+
*/
129+
#define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS) \
130+
__TRAILING_OVERLAP(TYPE, NAME, FAM, /* no attrs */, MEMBERS)
131+
116132
#endif

0 commit comments

Comments
 (0)