Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Gustavo A. R. Silva and committed by
Kees Cook
2bbdcf02 413187f7

+23 -7
+23 -7
include/linux/stddef.h
··· 94 94 __DECLARE_FLEX_ARRAY(TYPE, NAME) 95 95 96 96 /** 97 + * __TRAILING_OVERLAP() - Overlap a flexible-array member with trailing 98 + * members. 99 + * 100 + * Creates a union between a flexible-array member (FAM) in a struct and a set 101 + * of additional members that would otherwise follow it. 102 + * 103 + * @TYPE: Flexible structure type name, including "struct" keyword. 104 + * @NAME: Name for a variable to define. 105 + * @FAM: The flexible-array member within @TYPE 106 + * @ATTRS: Any struct attributes (usually empty) 107 + * @MEMBERS: Trailing overlapping members. 108 + */ 109 + #define __TRAILING_OVERLAP(TYPE, NAME, FAM, ATTRS, MEMBERS) \ 110 + union { \ 111 + TYPE NAME; \ 112 + struct { \ 113 + unsigned char __offset_to_FAM[offsetof(TYPE, FAM)]; \ 114 + MEMBERS \ 115 + } ATTRS; \ 116 + } 117 + 118 + /** 97 119 * TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members. 98 120 * 99 121 * Creates a union between a flexible-array member (FAM) in a struct and a set ··· 127 105 * @MEMBERS: Trailing overlapping members. 128 106 */ 129 107 #define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS) \ 130 - union { \ 131 - TYPE NAME; \ 132 - struct { \ 133 - unsigned char __offset_to_FAM[offsetof(TYPE, FAM)]; \ 134 - MEMBERS \ 135 - }; \ 136 - } 108 + __TRAILING_OVERLAP(TYPE, NAME, FAM, /* no attrs */, MEMBERS) 137 109 138 110 #endif