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

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>

+9 -1
+9 -1
include/linux/stddef.h
··· 20 20 #endif 21 21 22 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 + 30 + /** 23 31 * offsetofend(TYPE, MEMBER) 24 32 * 25 33 * @TYPE: The type of the structure 26 34 * @MEMBER: The member within the structure to get the end offset of 27 35 */ 28 36 #define offsetofend(TYPE, MEMBER) \ 29 - (offsetof(TYPE, MEMBER) + sizeof(((TYPE *)0)->MEMBER)) 37 + (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) 30 38 31 39 #endif