linux-leds.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] leds: pwm: Use struct_size() helper
@ 2019-08-30  0:53 Gustavo A. R. Silva
  2019-08-30  5:40 ` Kees Cook
  2019-08-30 20:29 ` Jacek Anaszewski
  0 siblings, 2 replies; 3+ messages in thread
From: Gustavo A. R. Silva @ 2019-08-30  0:53 UTC (permalink / raw)
  To: Jacek Anaszewski, Pavel Machek, Dan Murphy
  Cc: linux-leds, linux-kernel, Gustavo A. R. Silva, Kees Cook

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.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
 drivers/leds/leds-pwm.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
index d0e1f2710351..8b6965a563e9 100644
--- a/drivers/leds/leds-pwm.c
+++ b/drivers/leds/leds-pwm.c
@@ -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;
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] leds: pwm: Use struct_size() helper
  2019-08-30  0:53 [PATCH] leds: pwm: Use struct_size() helper Gustavo A. R. Silva
@ 2019-08-30  5:40 ` Kees Cook
  2019-08-30 20:29 ` Jacek Anaszewski
  1 sibling, 0 replies; 3+ messages in thread
From: Kees Cook @ 2019-08-30  5:40 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Jacek Anaszewski, Pavel Machek, Dan Murphy, linux-leds, linux-kernel

On Thu, Aug 29, 2019 at 07:53:20PM -0500, Gustavo A. R. Silva wrote:
> 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.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>

Reviewed-by: Kees Cook <keescook@chromium.org>

-Kees

> ---
>  drivers/leds/leds-pwm.c | 8 +-------
>  1 file changed, 1 insertion(+), 7 deletions(-)
> 
> diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
> index d0e1f2710351..8b6965a563e9 100644
> --- a/drivers/leds/leds-pwm.c
> +++ b/drivers/leds/leds-pwm.c
> @@ -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;
> -- 
> 2.23.0
> 

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] leds: pwm: Use struct_size() helper
  2019-08-30  0:53 [PATCH] leds: pwm: Use struct_size() helper Gustavo A. R. Silva
  2019-08-30  5:40 ` Kees Cook
@ 2019-08-30 20:29 ` Jacek Anaszewski
  1 sibling, 0 replies; 3+ messages in thread
From: Jacek Anaszewski @ 2019-08-30 20:29 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Pavel Machek, Dan Murphy
  Cc: linux-leds, linux-kernel, Kees Cook

Hi Gustavo,

Thank you for the patch.

On 8/30/19 2:53 AM, Gustavo A. R. Silva wrote:
> 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.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> ---
>  drivers/leds/leds-pwm.c | 8 +-------
>  1 file changed, 1 insertion(+), 7 deletions(-)
> 
> diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c
> index d0e1f2710351..8b6965a563e9 100644
> --- a/drivers/leds/leds-pwm.c
> +++ b/drivers/leds/leds-pwm.c
> @@ -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;
> 

Applied.

-- 
Best regards,
Jacek Anaszewski

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-08-30 20:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-30  0:53 [PATCH] leds: pwm: Use struct_size() helper Gustavo A. R. Silva
2019-08-30  5:40 ` Kees Cook
2019-08-30 20:29 ` Jacek Anaszewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).