Skip to content

Commit 5607f5e

Browse files
Dan CarpenterBartosz Golaszewski
authored andcommitted
gpio: sysfs: Fix an end of loop test in gpiod_unexport()
The test for "if (!desc_data)" does not work correctly because the list iterator in a list_for_each_entry() loop is always non-NULL. If we don't exit via a break, then it points to invalid memory. Instead, use a tmp variable for the list iterator and only set the "desc_data" when we have found a match. Fixes: 1cd53df ("gpio: sysfs: don't look up exported lines as class devices") Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org> Link: https://lore.kernel.org/r/747545bf-05f0-4f89-ba77-cb96bf9041f1@sabinyo.mountain Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
1 parent ae455b2 commit 5607f5e

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

drivers/gpio/gpiolib-sysfs.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ EXPORT_SYMBOL_GPL(gpiod_export_link);
927927
*/
928928
void gpiod_unexport(struct gpio_desc *desc)
929929
{
930-
struct gpiod_data *desc_data = NULL;
930+
struct gpiod_data *tmp, *desc_data = NULL;
931931
struct gpiodev_data *gdev_data;
932932
struct gpio_device *gdev;
933933

@@ -945,9 +945,12 @@ void gpiod_unexport(struct gpio_desc *desc)
945945
if (!gdev_data)
946946
return;
947947

948-
list_for_each_entry(desc_data, &gdev_data->exported_lines, list)
949-
if (gpiod_is_equal(desc, desc_data->desc))
948+
list_for_each_entry(tmp, &gdev_data->exported_lines, list) {
949+
if (gpiod_is_equal(desc, tmp->desc)) {
950+
desc_data = tmp;
950951
break;
952+
}
953+
}
951954

952955
if (!desc_data)
953956
return;

0 commit comments

Comments
 (0)