Skip to content

Commit

Permalink
leds: is31fl32xx: Use struct_size() helper
Browse files Browse the repository at this point in the history
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 <[email protected]>
Acked-by: Pavel Machek <[email protected]>
Signed-off-by: Jacek Anaszewski <[email protected]>
  • Loading branch information
GustavoARSilva authored and jacek-anaszewski committed Sep 1, 2019
1 parent 2637fd4 commit 1669ec7
Showing 1 changed file with 1 addition and 7 deletions.
8 changes: 1 addition & 7 deletions drivers/leds/leds-is31fl32xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1669ec7

Please sign in to comment.