Skip to content

Commit

Permalink
leds: ns2: Use struct_size() in devm_kzalloc()
Browse files Browse the repository at this point in the history
Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes. Also, remove unnecessary
function sizeof_ns2_led_priv().

This code was detected with the help of Coccinelle and, audited and
fixed manually.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Pavel Machek <pavel@ucw.cz>
  • Loading branch information
Gustavo A. R. Silva authored and Pavel Machek committed Jul 12, 2020
1 parent c4e9441 commit a7ad53c
Showing 1 changed file with 1 addition and 8 deletions.
9 changes: 1 addition & 8 deletions drivers/leds/leds-ns2.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,6 @@ struct ns2_led_priv {
struct ns2_led_data leds_data[];
};

static inline int sizeof_ns2_led_priv(int num_leds)
{
return sizeof(struct ns2_led_priv) +
(sizeof(struct ns2_led_data) * num_leds);
}

static int ns2_led_probe(struct platform_device *pdev)
{
struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
Expand All @@ -378,8 +372,7 @@ static int ns2_led_probe(struct platform_device *pdev)
return -EINVAL;
#endif /* CONFIG_OF_GPIO */

priv = devm_kzalloc(&pdev->dev,
sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL);
priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds_data, pdata->num_leds), GFP_KERNEL);
if (!priv)
return -ENOMEM;
priv->num_leds = pdata->num_leds;
Expand Down

0 comments on commit a7ad53c

Please sign in to comment.