Skip to contentSkip to conversation

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

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leds: gpio: 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_gpio_leds_priv().

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

---
 drivers/leds/leds-gpio.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/leds/leds-gpio.c +3 -10
index 2bf74595610f..30ae453f32f7 100644
@@ -125,12 +125,6 @@ struct gpio_leds_priv {
 	struct gpio_led_data leds[];
 };
 
-static inline int sizeof_gpio_leds_priv(int num_leds)
-{
-	return sizeof(struct gpio_leds_priv) +
-		(sizeof(struct gpio_led_data) * num_leds);
-}
-
 static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -142,7 +136,7 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
 	if (!count)
 		return ERR_PTR(-ENODEV);
 
-	priv = devm_kzalloc(dev, sizeof_gpio_leds_priv(count), GFP_KERNEL);
+	priv = devm_kzalloc(dev, struct_size(priv, leds, count), GFP_KERNEL);
 	if (!priv)
 		return ERR_PTR(-ENOMEM);
 
@@ -260,9 +254,8 @@ static int gpio_led_probe(struct platform_device *pdev)
 	int i, ret = 0;
 
 	if (pdata && pdata->num_leds) {
-		priv = devm_kzalloc(&pdev->dev,
-				sizeof_gpio_leds_priv(pdata->num_leds),
-					GFP_KERNEL);
+		priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, pdata->num_leds),
+				    GFP_KERNEL);
 		if (!priv)
 			return -ENOMEM;
 
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_gpio_leds_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