Skip to content

Commit 045391a

Browse files
keeslag-linaro
authored andcommitted
leds: gpio: Set num_leds after allocation
With the new __counted_by annotation, the "num_leds" variable needs to valid for accesses to the "leds" array. This requirement is not met in gpio_leds_create(), since "num_leds" starts at "0", so "leds" index "0" will not be considered valid (num_leds would need to be "1" to access index "0"). Fix this by setting the allocation size after allocation, and then update the final count based on how many were actually added to the array. Fixes: 52cd751 ("leds: gpio: Annotate struct gpio_leds_priv with __counted_by") Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Gustavo A. R. Silva <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lee Jones <[email protected]>
1 parent e5ae408 commit 045391a

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

drivers/leds/leds-gpio.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static struct gpio_leds_priv *gpio_leds_create(struct device *dev)
150150
{
151151
struct fwnode_handle *child;
152152
struct gpio_leds_priv *priv;
153-
int count, ret;
153+
int count, used, ret;
154154

155155
count = device_get_child_node_count(dev);
156156
if (!count)
@@ -159,9 +159,11 @@ static struct gpio_leds_priv *gpio_leds_create(struct device *dev)
159159
priv = devm_kzalloc(dev, struct_size(priv, leds, count), GFP_KERNEL);
160160
if (!priv)
161161
return ERR_PTR(-ENOMEM);
162+
priv->num_leds = count;
163+
used = 0;
162164

163165
device_for_each_child_node(dev, child) {
164-
struct gpio_led_data *led_dat = &priv->leds[priv->num_leds];
166+
struct gpio_led_data *led_dat = &priv->leds[used];
165167
struct gpio_led led = {};
166168

167169
/*
@@ -197,8 +199,9 @@ static struct gpio_leds_priv *gpio_leds_create(struct device *dev)
197199
/* Set gpiod label to match the corresponding LED name. */
198200
gpiod_set_consumer_name(led_dat->gpiod,
199201
led_dat->cdev.dev->kobj.name);
200-
priv->num_leds++;
202+
used++;
201203
}
204+
priv->num_leds = used;
202205

203206
return priv;
204207
}

0 commit comments

Comments
 (0)