Skip to contentSkip to conversation

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

PATCH Applied Gustavo A. R. Silva 3 participants 3 messages Jun 17, 2020 – Jun 18, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/mce/dev-mcelog: Use struct_size() helper in kzalloc()

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.

---
 arch/x86/kernel/cpu/mce/dev-mcelog.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/mce/dev-mcelog.c +1 -1
index 43c466020ed5..03e51053592a 100644
@@ -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;
 
Signature · 1 line
2.27.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Gustavo A. R. Silva

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.

Quoted 20 lines
---
 arch/x86/kernel/cpu/mce/dev-mcelog.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/mce/dev-mcelog.c +1 -1
index 43c466020ed5..03e51053592a 100644
@@ -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;
 
Signature · 1 line
2.27.0
#2 lore raw
In reply to Gustavo A. R. Silva
The following commit has been merged into the ras/core branch of tip:

The following commit has been merged into the ras/core branch of tip:

Commit-ID:     5ba7821bcf7d82e35582fce8fe65cd160a6954b4
Gitweb:        https://git.kernel.org/tip/5ba7821bcf7d82e35582fce8fe65cd160a6954b4
Author:        Gustavo A. R. Silva <[email protected]>
AuthorDate:    Wed, 17 Jun 2020 16:17:34 -05:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Thu, 18 Jun 2020 13:24:23 +02:00

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

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.

---
 arch/x86/kernel/cpu/mce/dev-mcelog.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/mce/dev-mcelog.c +1 -1
index 43c4660..03e5105 100644
@@ -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;