Skip to content

Commit 4229a47

Browse files
committed
stddef.h: Introduce sizeof_field()
The size of fields within a structure is needed in a few places in the kernel already, and will be needed for the usercopy whitelisting when declaring whitelist regions within structures. This creates a dedicated macro and redefines offsetofend() to use it. Existing usage, ignoring the 1200+ lustre assert uses: $ git grep -E 'sizeof\(\(\((struct )?[a-zA-Z_]+ \*\)0\)->' | \ grep -v staging/lustre | wc -l 65 Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent c758868 commit 4229a47

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

include/linux/stddef.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ enum {
1919
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
2020
#endif
2121

22+
/**
23+
* sizeof_field(TYPE, MEMBER)
24+
*
25+
* @TYPE: The structure containing the field of interest
26+
* @MEMBER: The field to return the size of
27+
*/
28+
#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
29+
2230
/**
2331
* offsetofend(TYPE, MEMBER)
2432
*
2533
* @TYPE: The type of the structure
2634
* @MEMBER: The member within the structure to get the end offset of
2735
*/
2836
#define offsetofend(TYPE, MEMBER) \
29-
(offsetof(TYPE, MEMBER) + sizeof(((TYPE *)0)->MEMBER))
37+
(offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
3038

3139
#endif

0 commit comments

Comments
 (0)