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: 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: 26dd68d293fd ("overflow: add DEFINE_FLEX() for on-stack allocs")
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/aBQVeyKfLOkO9Yss@kspp
Signed-off-by: Kees Cook <kees@kernel.org>

authored by

Gustavo A. R. Silva and committed by
Kees Cook
47e36ed7 6e6500e4

+3 -3
+3 -3
include/linux/overflow.h
··· 396 396 * @name: Name for a variable to define. 397 397 * @member: Name of the array member. 398 398 * @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). 400 400 */ 401 401 #define _DEFINE_FLEX(type, name, member, count, initializer...) \ 402 402 _Static_assert(__builtin_constant_p(count), \ ··· 404 404 union { \ 405 405 u8 bytes[struct_size_t(type, member, count)]; \ 406 406 type obj; \ 407 - } name##_u initializer; \ 407 + } name##_u = { .obj initializer }; \ 408 408 type *name = (type *)&name##_u 409 409 410 410 /** ··· 444 444 * elements in array @member. 445 445 */ 446 446 #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, }) 448 448 449 449 /** 450 450 * STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family.