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

Merge tag 'overflow-v5.8-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux

Pull flex-array size helper from Kees Cook:
"During the treewide clean-ups of zero-length "flexible arrays", the
struct_size() helper was heavily used, but it was noticed that many
times it would have been nice to have an additional helper to get the
size of just the flexible array itself.

This need appears to be even more common when cleaning up the 1-byte
array "flexible arrays", so Gustavo implemented it.

I'd love to get this landed early so it can be used during the v5.9
dev cycle to ease the 1-byte array cleanups."

* tag 'overflow-v5.8-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux:
overflow.h: Add flex_array_size() helper

+21 -4
+21 -4
include/linux/overflow.h
··· 304 304 * struct_size() - Calculate size of structure with trailing array. 305 305 * @p: Pointer to the structure. 306 306 * @member: Name of the array member. 307 - * @n: Number of elements in the array. 307 + * @count: Number of elements in the array. 308 308 * 309 309 * Calculates size of memory needed for structure @p followed by an 310 - * array of @n @member elements. 310 + * array of @count number of @member elements. 311 311 * 312 312 * Return: number of bytes needed or SIZE_MAX on overflow. 313 313 */ 314 - #define struct_size(p, member, n) \ 315 - __ab_c_size(n, \ 314 + #define struct_size(p, member, count) \ 315 + __ab_c_size(count, \ 316 316 sizeof(*(p)->member) + __must_be_array((p)->member),\ 317 317 sizeof(*(p))) 318 + 319 + /** 320 + * flex_array_size() - Calculate size of a flexible array member 321 + * within an enclosing structure. 322 + * 323 + * @p: Pointer to the structure. 324 + * @member: Name of the flexible array member. 325 + * @count: Number of elements in the array. 326 + * 327 + * Calculates size of a flexible array of @count number of @member 328 + * elements, at the end of structure @p. 329 + * 330 + * Return: number of bytes needed or SIZE_MAX on overflow. 331 + */ 332 + #define flex_array_size(p, member, count) \ 333 + array_size(count, \ 334 + sizeof(*(p)->member) + __must_be_array((p)->member)) 318 335 319 336 #endif /* __LINUX_OVERFLOW_H */