[PATCH] stm class: Use struct_size()

From: Christophe JAILLET
Date: Sat Jul 22 2023 - 16:07:53 EST


Use struct_size() instead of hand-writing it, when allocating a structure
with a flex array.

This is less verbose, more robust and more informative.

Signed-off-by: Christophe JAILLET <christophe.jaillet@xxxxxxxxxx>
---
It will also be helpful if the __counted_by() annotation is added with a
Coccinelle script such as:
https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/commit/?h=devel/counted_by&id=adc5b3cb48a049563dc673f348eab7b6beba8a9b
---
drivers/hwtracing/stm/core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hwtracing/stm/core.c b/drivers/hwtracing/stm/core.c
index 534fbefc7f6a..2195310ab81c 100644
--- a/drivers/hwtracing/stm/core.c
+++ b/drivers/hwtracing/stm/core.c
@@ -863,7 +863,7 @@ int stm_register_device(struct device *parent, struct stm_data *stm_data,
return -EINVAL;

nmasters = stm_data->sw_end - stm_data->sw_start + 1;
- stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *));
+ stm = vzalloc(struct_size(stm, masters, nmasters));
if (!stm)
return -ENOMEM;

--
2.34.1