On Wed, May 18, 2022 at 09:16:26PM +0800, Lin Yujun wrote:
Replace sizeof() with struct_size() to avoid potential integerThis is not exactly what the patch does.
or heap overflow.
Your patch is replacing the open-coded calculation of the total amount
of memory to be allocated for rapl_pmus with struct_size().
Also, address the following sparse warnings:This does not address this warning because struct_size()
arch/x86/events/rapl.c:685:16: warning: using sizeof on a
flexible structure
still make use of sizeof on the struct-with-flex-array,
internally.
So, the mention to the sparse warning should be removed from
this changelog text.
Signed-off-by: Lin Yujun <linyujun809@xxxxxxxxxx>It seems that in this case, size could be entirely replaced by
---
arch/x86/events/rapl.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
index 77e3a47af5ad..c7e79f0ac04f 100644
--- a/arch/x86/events/rapl.c
+++ b/arch/x86/events/rapl.c
@@ -685,7 +685,7 @@ static int __init init_rapl_pmus(void)
int maxdie = topology_max_packages() * topology_max_die_per_package();
size_t size;
- size = sizeof(*rapl_pmus) + maxdie * sizeof(struct rapl_pmu *);
+ size = struct_size(rapl_pmus, pmus, maxdie);
rapl_pmus = kzalloc(size, GFP_KERNEL);
struct_size().
Thanks
--
Gustavo
.