]> git.baikalelectronics.ru Git - kernel.git/commit
overflow.h: Add flex_array_size() helper
authorGustavo A. R. Silva <gustavoars@kernel.org>
Tue, 9 Jun 2020 01:22:33 +0000 (20:22 -0500)
committerKees Cook <keescook@chromium.org>
Wed, 17 Jun 2020 03:45:08 +0000 (20:45 -0700)
commit1d60d7ac9c6fbbd212904fa42118c4fa0449d2ff
treec0bf158b81e425eeb159a12dd173a421cf7ddef1
parent2e0873da09585b8a2daf9ca8c2facd15f454ec3f
overflow.h: Add flex_array_size() helper

Add flex_array_size() helper for the calculation of the size, in bytes,
of a flexible array member contained within an enclosing structure.

Example of usage:

struct something {
size_t count;
struct foo items[];
};

struct something *instance;

instance = kmalloc(struct_size(instance, items, count), GFP_KERNEL);
instance->count = count;
memcpy(instance->items, src, flex_array_size(instance, items, instance->count));

The helper returns SIZE_MAX on overflow instead of wrapping around.

Additionally replaces parameter "n" with "count" in struct_size() helper
for greater clarity and unification.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/20200609012233.GA3371@embeddedor
Signed-off-by: Kees Cook <keescook@chromium.org>
include/linux/overflow.h