leds: is31fl32xx: Use struct_size() helper
PATCH 1/1 leds: is31fl32xx: 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 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.
--- drivers/leds/leds-is31fl32xx.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/leds/leds-is31fl32xx.c +1 -7
@@ -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 ;
Signature · 1 line
2.23.0
Jacek Anaszewski
AppliedHi 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 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_tsizeof_is31fl32xx_priv(int num_leds){
returnsizeof(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 <[email protected]>--- drivers/leds/leds-is31fl32xx.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)drivers/leds/leds-is31fl32xx.c +0
-6@@ -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)
drivers/leds/leds-is31fl32xx.c static int is31fl32xx_probe(struct i2c_client *client, · line 450
Quoted 8 lines
drivers/leds/leds-is31fl32xx.c +1
-1@@ -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.
Signature · 2 lines
Best regards, Jacek Anaszewski
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 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_tsizeof_is31fl32xx_priv(int num_leds){
returnsizeof(struct is31fl32xx_priv)+
(sizeof(struct is31fl32xx_led_data)*num_leds);
}
Quoted 7 lines
with:
struct_size(priv, leds, count)This code was detected with the help of Coccinelle.
Signed-off-by Gustavo A. R. Silva <[email protected]>
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html