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

Configure Feed

Select the types of activity you want to include in your feed.

overflow: Introduce __DEFINE_FLEX for having no initializer

While not yet in the tree, there is a proposed patch[1] that was
depending on the prior behavior of _DEFINE_FLEX, which did not have an
explicit initializer. Provide this via __DEFINE_FLEX now, which can also
have attributes applied (e.g. __uninitialized).

Examples of the resulting initializer behaviors can be seen here:
https://godbolt.org/z/P7Go8Tr33

Link: https://lore.kernel.org/netdev/20250520205920.2134829-9-anthony.l.nguyen@intel.com [1]
Fixes: 47e36ed78406 ("overflow: Fix direct struct member initialization in _DEFINE_FLEX()")
Signed-off-by: Kees Cook <kees@kernel.org>

+21 -8
+21 -8
include/linux/overflow.h
··· 389 389 struct_size((type *)NULL, member, count) 390 390 391 391 /** 392 + * __DEFINE_FLEX() - helper macro for DEFINE_FLEX() family. 393 + * Enables caller macro to pass arbitrary trailing expressions 394 + * 395 + * @type: structure type name, including "struct" keyword. 396 + * @name: Name for a variable to define. 397 + * @member: Name of the array member. 398 + * @count: Number of elements in the array; must be compile-time const. 399 + * @trailer: Trailing expressions for attributes and/or initializers. 400 + */ 401 + #define __DEFINE_FLEX(type, name, member, count, trailer...) \ 402 + _Static_assert(__builtin_constant_p(count), \ 403 + "onstack flex array members require compile-time const count"); \ 404 + union { \ 405 + u8 bytes[struct_size_t(type, member, count)]; \ 406 + type obj; \ 407 + } name##_u trailer; \ 408 + type *name = (type *)&name##_u 409 + 410 + /** 392 411 * _DEFINE_FLEX() - helper macro for DEFINE_FLEX() family. 393 412 * Enables caller macro to pass (different) initializer. 394 413 * ··· 418 399 * @initializer: Initializer expression (e.g., pass `= { }` at minimum). 419 400 */ 420 401 #define _DEFINE_FLEX(type, name, member, count, initializer...) \ 421 - _Static_assert(__builtin_constant_p(count), \ 422 - "onstack flex array members require compile-time const count"); \ 423 - union { \ 424 - u8 bytes[struct_size_t(type, member, count)]; \ 425 - type obj; \ 426 - } name##_u = { .obj initializer }; \ 427 - type *name = (type *)&name##_u 402 + __DEFINE_FLEX(type, name, member, count, = { .obj initializer }) 428 403 429 404 /** 430 405 * DEFINE_RAW_FLEX() - Define an on-stack instance of structure with a trailing ··· 437 424 * elements in array @member. 438 425 */ 439 426 #define DEFINE_RAW_FLEX(type, name, member, count) \ 440 - _DEFINE_FLEX(type, name, member, count, = {}) 427 + __DEFINE_FLEX(type, name, member, count, = { }) 441 428 442 429 /** 443 430 * DEFINE_FLEX() - Define an on-stack instance of structure with a trailing