Skip to contentSkip to conversation

leds: is31fl32xx: Use struct_size() helper

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

lore
Share & tools
lore
#0 lore raw

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
index 6fbab70dfb04..6f29b8943913 100644
@@ -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
#1 lore raw

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 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 +0 -6
index 6fbab70dfb04..6f29b8943913 100644
@@ -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
Acked-by on v1 1/1
#2 lore raw
In reply to Gustavo A. R. Silva
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_t sizeof_is31fl32xx_priv(int num_leds) {
return sizeof(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.

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html