* [PATCH] leds: is31fl32xx: Use struct_size() helper
@ 2019-08-30 18:14 Gustavo A. R. Silva
2019-08-30 20:29 ` Jacek Anaszewski
2019-09-01 10:26 ` Pavel Machek
0 siblings, 2 replies; 3+ messages in thread
From: Gustavo A. R. Silva @ 2019-08-30 18:14 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 is31fl32xx_priv {
...
struct is31fl32xx_led_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_is31fl32xx_priv(int num_leds)
{
return sizeof(struct is31fl32xx_priv) +
(sizeof(struct is31fl32xx_led_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-is31fl32xx.c | 8 +-------
1 file changed, 1 insertion(+), 7 deletions(-)
diff --git a/drivers/leds/leds-is31fl32xx.c b/drivers/leds/leds-is31fl32xx.c
index 6fbab70dfb04..6f29b8943913 100644
--- a/drivers/leds/leds-is31fl32xx.c
+++ b/drivers/leds/leds-is31fl32xx.c
@@ -324,12 +324,6 @@ static int is31fl32xx_init_regs(struct is31fl32xx_priv *priv)
return 0;
}
-static inline size_t sizeof_is31fl32xx_priv(int num_leds)
-{
- return sizeof(struct is31fl32xx_priv) +
- (sizeof(struct is31fl32xx_led_data) * num_leds);
-}
-
static int is31fl32xx_parse_child_dt(const struct device *dev,
const struct device_node *child,
struct is31fl32xx_led_data *led_data)
@@ -450,7 +444,7 @@ static int is31fl32xx_probe(struct i2c_client *client,
if (!count)
return -EINVAL;
- priv = devm_kzalloc(dev, sizeof_is31fl32xx_priv(count),
+ priv = devm_kzalloc(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: is31fl32xx: Use struct_size() helper
2019-08-30 18:14 [PATCH] leds: is31fl32xx: Use struct_size() helper Gustavo A. R. Silva
@ 2019-08-30 20:29 ` Jacek Anaszewski
2019-09-01 10:26 ` Pavel Machek
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 8:14 PM, 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 is31fl32xx_priv {
> ...
> struct is31fl32xx_led_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_is31fl32xx_priv(int num_leds)
> {
> return sizeof(struct is31fl32xx_priv) +
> (sizeof(struct is31fl32xx_led_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-is31fl32xx.c | 8 +-------
> 1 file changed, 1 insertion(+), 7 deletions(-)
>
> diff --git a/drivers/leds/leds-is31fl32xx.c b/drivers/leds/leds-is31fl32xx.c
> index 6fbab70dfb04..6f29b8943913 100644
> --- a/drivers/leds/leds-is31fl32xx.c
> +++ b/drivers/leds/leds-is31fl32xx.c
> @@ -324,12 +324,6 @@ static int is31fl32xx_init_regs(struct is31fl32xx_priv *priv)
> return 0;
> }
>
> -static inline size_t sizeof_is31fl32xx_priv(int num_leds)
> -{
> - return sizeof(struct is31fl32xx_priv) +
> - (sizeof(struct is31fl32xx_led_data) * num_leds);
> -}
> -
> static int is31fl32xx_parse_child_dt(const struct device *dev,
> const struct device_node *child,
> struct is31fl32xx_led_data *led_data)
> @@ -450,7 +444,7 @@ static int is31fl32xx_probe(struct i2c_client *client,
> if (!count)
> return -EINVAL;
>
> - priv = devm_kzalloc(dev, sizeof_is31fl32xx_priv(count),
> + priv = devm_kzalloc(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
* Re: [PATCH] leds: is31fl32xx: Use struct_size() helper
2019-08-30 18:14 [PATCH] leds: is31fl32xx: Use struct_size() helper Gustavo A. R. Silva
2019-08-30 20:29 ` Jacek Anaszewski
@ 2019-09-01 10:26 ` Pavel Machek
1 sibling, 0 replies; 3+ messages in thread
From: Pavel Machek @ 2019-09-01 10:26 UTC (permalink / raw)
To: Gustavo A. R. Silva
Cc: Jacek Anaszewski, Dan Murphy, linux-leds, linux-kernel, Kees Cook
[-- Attachment #1: Type: text/plain, Size: 1119 bytes --]
On Fri 2019-08-30 13:14:48, 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 is31fl32xx_priv {
> ...
> struct is31fl32xx_led_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_is31fl32xx_priv(int num_leds)
> {
> return sizeof(struct is31fl32xx_priv) +
> (sizeof(struct is31fl32xx_led_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>
Acked-by: Pavel Machek <pavel@ucw.cz>
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2019-09-01 10:26 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-30 18:14 [PATCH] leds: is31fl32xx: Use struct_size() helper Gustavo A. R. Silva
2019-08-30 20:29 ` Jacek Anaszewski
2019-09-01 10:26 ` Pavel Machek
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.