mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2025-01-06 05:06:29 +00:00
overflow.h: Add flex_array_size() helper
Add flex_array_size() helper for the calculation of the size, in bytes, of a flexible array member contained within an enclosing structure. Example of usage: struct something { size_t count; struct foo items[]; }; struct something *instance; instance = kmalloc(struct_size(instance, items, count), GFP_KERNEL); instance->count = count; memcpy(instance->items, src, flex_array_size(instance, items, instance->count)); The helper returns SIZE_MAX on overflow instead of wrapping around. Additionally replaces parameter "n" with "count" in struct_size() helper for greater clarity and unification. Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Link: https://lore.kernel.org/r/20200609012233.GA3371@embeddedor Signed-off-by: Kees Cook <keescook@chromium.org>
This commit is contained in:
parent
b3a9e3b962
commit
b19d57d0f3
@ -304,16 +304,33 @@ static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
|
||||
* struct_size() - Calculate size of structure with trailing array.
|
||||
* @p: Pointer to the structure.
|
||||
* @member: Name of the array member.
|
||||
* @n: Number of elements in the array.
|
||||
* @count: Number of elements in the array.
|
||||
*
|
||||
* Calculates size of memory needed for structure @p followed by an
|
||||
* array of @n @member elements.
|
||||
* array of @count number of @member elements.
|
||||
*
|
||||
* Return: number of bytes needed or SIZE_MAX on overflow.
|
||||
*/
|
||||
#define struct_size(p, member, n) \
|
||||
__ab_c_size(n, \
|
||||
#define struct_size(p, member, count) \
|
||||
__ab_c_size(count, \
|
||||
sizeof(*(p)->member) + __must_be_array((p)->member),\
|
||||
sizeof(*(p)))
|
||||
|
||||
/**
|
||||
* flex_array_size() - Calculate size of a flexible array member
|
||||
* within an enclosing structure.
|
||||
*
|
||||
* @p: Pointer to the structure.
|
||||
* @member: Name of the flexible array member.
|
||||
* @count: Number of elements in the array.
|
||||
*
|
||||
* Calculates size of a flexible array of @count number of @member
|
||||
* elements, at the end of structure @p.
|
||||
*
|
||||
* Return: number of bytes needed or SIZE_MAX on overflow.
|
||||
*/
|
||||
#define flex_array_size(p, member, count) \
|
||||
array_size(count, \
|
||||
sizeof(*(p)->member) + __must_be_array((p)->member))
|
||||
|
||||
#endif /* __LINUX_OVERFLOW_H */
|
||||
|
Loading…
Reference in New Issue
Block a user