Skip to content

Commit 5c78e79

Browse files
committed
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/[email protected] [1] Fixes: 47e36ed ("overflow: Fix direct struct member initialization in _DEFINE_FLEX()") Signed-off-by: Kees Cook <[email protected]>
1 parent d6a0e0b commit 5c78e79

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

include/linux/overflow.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,24 +389,37 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
389389
struct_size((type *)NULL, member, count)
390390

391391
/**
392-
* _DEFINE_FLEX() - helper macro for DEFINE_FLEX() family.
393-
* Enables caller macro to pass (different) initializer.
392+
* __DEFINE_FLEX() - helper macro for DEFINE_FLEX() family.
393+
* Enables caller macro to pass arbitrary trailing expressions
394394
*
395395
* @type: structure type name, including "struct" keyword.
396396
* @name: Name for a variable to define.
397397
* @member: Name of the array member.
398398
* @count: Number of elements in the array; must be compile-time const.
399-
* @initializer: Initializer expression (e.g., pass `= { }` at minimum).
399+
* @trailer: Trailing expressions for attributes and/or initializers.
400400
*/
401-
#define _DEFINE_FLEX(type, name, member, count, initializer...) \
401+
#define __DEFINE_FLEX(type, name, member, count, trailer...) \
402402
_Static_assert(__builtin_constant_p(count), \
403403
"onstack flex array members require compile-time const count"); \
404404
union { \
405405
u8 bytes[struct_size_t(type, member, count)]; \
406406
type obj; \
407-
} name##_u = { .obj initializer }; \
407+
} name##_u trailer; \
408408
type *name = (type *)&name##_u
409409

410+
/**
411+
* _DEFINE_FLEX() - helper macro for DEFINE_FLEX() family.
412+
* Enables caller macro to pass (different) initializer.
413+
*
414+
* @type: structure type name, including "struct" keyword.
415+
* @name: Name for a variable to define.
416+
* @member: Name of the array member.
417+
* @count: Number of elements in the array; must be compile-time const.
418+
* @initializer: Initializer expression (e.g., pass `= { }` at minimum).
419+
*/
420+
#define _DEFINE_FLEX(type, name, member, count, initializer...) \
421+
__DEFINE_FLEX(type, name, member, count, = { .obj initializer })
422+
410423
/**
411424
* DEFINE_RAW_FLEX() - Define an on-stack instance of structure with a trailing
412425
* flexible array member, when it does not have a __counted_by annotation.
@@ -424,7 +437,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
424437
* elements in array @member.
425438
*/
426439
#define DEFINE_RAW_FLEX(type, name, member, count) \
427-
_DEFINE_FLEX(type, name, member, count, = {})
440+
__DEFINE_FLEX(type, name, member, count, = { })
428441

429442
/**
430443
* DEFINE_FLEX() - Define an on-stack instance of structure with a trailing

0 commit comments

Comments
 (0)