Skip to content

Commit 47e36ed

Browse files
GustavoARSilvakees
authored andcommitted
overflow: Fix direct struct member initialization in _DEFINE_FLEX()
Currently, to statically initialize the struct members of the `type` object created by _DEFINE_FLEX(), the internal `obj` member must be explicitly referenced at the call site. See: struct flex { int a; int b; struct foo flex_array[]; }; _DEFINE_FLEX(struct flex, instance, flex_array, FIXED_SIZE, = { .obj = { .a = 0, .b = 1, }, }); This leaks _DEFINE_FLEX() internal implementation details and make the helper harder to use and read. Fix this and allow for a more natural and intuitive C99 init-style: _DEFINE_FLEX(struct flex, instance, flex_array, FIXED_SIZE, = { .a = 0, .b = 1, }); Note that before these changes, the `initializer` argument was optional, but now it's required. Also, update "counter" member initialization in DEFINE_FLEX(). Fixes: 26dd68d ("overflow: add DEFINE_FLEX() for on-stack allocs") Signed-off-by: Gustavo A. R. Silva <[email protected]> Link: https://lore.kernel.org/r/aBQVeyKfLOkO9Yss@kspp Signed-off-by: Kees Cook <[email protected]>
1 parent 6e6500e commit 47e36ed

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

include/linux/overflow.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,15 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
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 (could be empty for no init).
399+
* @initializer: Initializer expression (e.g., pass `= { }` at minimum).
400400
*/
401401
#define _DEFINE_FLEX(type, name, member, count, initializer...) \
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 initializer; \
407+
} name##_u = { .obj initializer }; \
408408
type *name = (type *)&name##_u
409409

410410
/**
@@ -444,7 +444,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
444444
* elements in array @member.
445445
*/
446446
#define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT) \
447-
_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, })
447+
_DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .COUNTER = COUNT, })
448448

449449
/**
450450
* STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family.

0 commit comments

Comments
 (0)