Skip to contentSkip to conversation

leds: ns2: Use struct_size() in devm_kzalloc()

PATCH Applied Gustavo A. R. Silva 2 participants 2 messages Jun 17, 2020 – Jul 12, 2020 LED

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leds: ns2: Use struct_size() in devm_kzalloc()

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes. Also, remove unnecessary function sizeof_ns2_led_priv().

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

---
 drivers/leds/leds-ns2.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/leds/leds-ns2.c +1 -8
index 538ca5755602..bd806e7c8017 100644
@@ -348,12 +348,6 @@ struct ns2_led_priv {
 	struct ns2_led_data leds_data[];
 };
 
-static inline int sizeof_ns2_led_priv(int num_leds)
-{
-	return sizeof(struct ns2_led_priv) +
-		      (sizeof(struct ns2_led_data) * num_leds);
-}
-
 static int ns2_led_probe(struct platform_device *pdev)
 {
 	struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
@@ -378,8 +372,7 @@ static int ns2_led_probe(struct platform_device *pdev)
 		return -EINVAL;
 #endif /* CONFIG_OF_GPIO */
 
-	priv = devm_kzalloc(&pdev->dev,
-			    sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL);
+	priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds_data, pdata->num_leds), GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;
 	priv->num_leds = pdata->num_leds;
Signature · 1 line
2.27.0
Applied
#1 lore raw
Quoted 8 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. Also, remove unnecessary function sizeof_ns2_led_priv().

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

Thanks, applied.
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html