diff mbox series

[next] x86/mce/dev-mcelog: Use struct_size() helper in kzalloc()

Message ID 20200617211734.GA9636@embeddedor (mailing list archive)
State New, archived
Headers show
Series [next] x86/mce/dev-mcelog: Use struct_size() helper in kzalloc() | expand

Commit Message

Gustavo A. R. Silva June 17, 2020, 9:17 p.m. UTC
Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes.

This code was detected with the help of Coccinelle and, audited and
fixed manually.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 arch/x86/kernel/cpu/mce/dev-mcelog.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Luck, Tony June 17, 2020, 9:29 p.m. UTC | #1
On Wed, Jun 17, 2020 at 04:17:34PM -0500, Gustavo A. R. Silva wrote:
> Make use of the struct_size() helper instead of an open-coded version
> in order to avoid any potential type mistakes.
> 
> This code was detected with the help of Coccinelle and, audited and
> fixed manually.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Acked-by: Tony Luck <tony.luck@intel.com>

> ---
>  arch/x86/kernel/cpu/mce/dev-mcelog.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/cpu/mce/dev-mcelog.c b/arch/x86/kernel/cpu/mce/dev-mcelog.c
> index 43c466020ed5..03e51053592a 100644
> --- a/arch/x86/kernel/cpu/mce/dev-mcelog.c
> +++ b/arch/x86/kernel/cpu/mce/dev-mcelog.c
> @@ -345,7 +345,7 @@ static __init int dev_mcelog_init_device(void)
>  	int err;
>  
>  	mce_log_len = max(MCE_LOG_MIN_LEN, num_online_cpus());
> -	mcelog = kzalloc(sizeof(*mcelog) + mce_log_len * sizeof(struct mce), GFP_KERNEL);
> +	mcelog = kzalloc(struct_size(mcelog, entry, mce_log_len), GFP_KERNEL);
>  	if (!mcelog)
>  		return -ENOMEM;
>  
> -- 
> 2.27.0
>
diff mbox series

Patch

diff --git a/arch/x86/kernel/cpu/mce/dev-mcelog.c b/arch/x86/kernel/cpu/mce/dev-mcelog.c
index 43c466020ed5..03e51053592a 100644
--- a/arch/x86/kernel/cpu/mce/dev-mcelog.c
+++ b/arch/x86/kernel/cpu/mce/dev-mcelog.c
@@ -345,7 +345,7 @@  static __init int dev_mcelog_init_device(void)
 	int err;
 
 	mce_log_len = max(MCE_LOG_MIN_LEN, num_online_cpus());
-	mcelog = kzalloc(sizeof(*mcelog) + mce_log_len * sizeof(struct mce), GFP_KERNEL);
+	mcelog = kzalloc(struct_size(mcelog, entry, mce_log_len), GFP_KERNEL);
 	if (!mcelog)
 		return -ENOMEM;