Skip to contentSkip to conversation

leds: pwm: Use struct_size() helper

PATCH Applied Gustavo A. R. Silva 3 participants 3 messages Aug 30, 2019 LED

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leds: pwm: Use struct_size() helper

One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct led_pwm_priv {
	...
        struct led_pwm_data leds[0];
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

So, replace the following function:

static inline size_t sizeof_pwm_leds_priv(int num_leds) {
return sizeof(struct led_pwm_priv) +
(sizeof(struct led_pwm_data) * num_leds);
}

with:

struct_size(priv, leds, count)

This code was detected with the help of Coccinelle.

---
 drivers/leds/leds-pwm.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/leds/leds-pwm.c +1 -7
index d0e1f2710351..8b6965a563e9 100644
@@ -65,12 +65,6 @@ static int led_pwm_set(struct led_classdev *led_cdev,
 	return 0;
 }
 
-static inline size_t sizeof_pwm_leds_priv(int num_leds)
-{
-	return sizeof(struct led_pwm_priv) +
-		      (sizeof(struct led_pwm_data) * num_leds);
-}
-
 static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
 		       struct led_pwm *led, struct fwnode_handle *fwnode)
 {
@@ -174,7 +168,7 @@ static int led_pwm_probe(struct platform_device *pdev)
 	if (!count)
 		return -EINVAL;
 
-	priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
+	priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
 			    GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;
Signature · 1 line
2.23.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Gustavo A. R. Silva

One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct led_pwm_priv {
	...
        struct led_pwm_data leds[0];
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

So, replace the following function:

static inline size_t sizeof_pwm_leds_priv(int num_leds) {
return sizeof(struct led_pwm_priv) +
(sizeof(struct led_pwm_data) * num_leds);
}

Quoted 7 lines

with:

struct_size(priv, leds, count)

This code was detected with the help of Coccinelle.

-Kees

Quoted 33 lines
---
 drivers/leds/leds-pwm.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/leds/leds-pwm.c +1 -7
index d0e1f2710351..8b6965a563e9 100644
@@ -65,12 +65,6 @@ static int led_pwm_set(struct led_classdev *led_cdev,
 	return 0;
 }
 
-static inline size_t sizeof_pwm_leds_priv(int num_leds)
-{
-	return sizeof(struct led_pwm_priv) +
-		      (sizeof(struct led_pwm_data) * num_leds);
-}
-
 static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
 		       struct led_pwm *led, struct fwnode_handle *fwnode)
 {
@@ -174,7 +168,7 @@ static int led_pwm_probe(struct platform_device *pdev)
 	if (!count)
 		return -EINVAL;
 
-	priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
+	priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
 			    GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;
Signature · 1 line
2.23.0
Signature · 1 line
Kees Cook
#2 lore raw
In reply to Gustavo A. R. Silva

Hi Gustavo,

Thank you for the patch.

Quoted 50 lines · Gustavo A. R. Silva

One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct led_pwm_priv {
	...
        struct led_pwm_data leds[0];
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

So, replace the following function:

static inline size_t sizeof_pwm_leds_priv(int num_leds) {
return sizeof(struct led_pwm_priv) +
(sizeof(struct led_pwm_data) * num_leds);
}

with:

struct_size(priv, leds, count)

This code was detected with the help of Coccinelle.

---
 drivers/leds/leds-pwm.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/leds/leds-pwm.c +0 -6
index d0e1f2710351..8b6965a563e9 100644
@@ -65,12 +65,6 @@ static int led_pwm_set(struct led_classdev *led_cdev,
 	return 0;
 }
 
-static inline size_t sizeof_pwm_leds_priv(int num_leds)
-{
-	return sizeof(struct led_pwm_priv) +
-		      (sizeof(struct led_pwm_data) * num_leds);
-}
-
 static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
 		       struct led_pwm *led, struct fwnode_handle *fwnode)
 {

drivers/leds/leds-pwm.c static int led_pwm_probe(struct platform_device *pdev) · line 174

Quoted 8 lines
drivers/leds/leds-pwm.c +1 -1
@@ -174,7 +168,7 @@ static int led_pwm_probe(struct platform_device *pdev)
 	if (!count)
 		return -EINVAL;
 
-	priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
+	priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
 			    GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;

Applied.

Signature · 2 lines
Best regards,
Jacek Anaszewski