]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
gpio: sysfs: Fix an end of loop test in gpiod_unexport()
authorDan Carpenter <dan.carpenter@linaro.org>
Fri, 18 Jul 2025 21:22:15 +0000 (16:22 -0500)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Sat, 19 Jul 2025 16:06:47 +0000 (18:06 +0200)
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: 1cd53df733c2 ("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>
drivers/gpio/gpiolib-sysfs.c

index f31adc56bef1e215a257eab37ca3319c55ef36a6..b64106f1cb7b90ac2c3595c5d1101cba93fa0e6a 100644 (file)
@@ -927,7 +927,7 @@ EXPORT_SYMBOL_GPL(gpiod_export_link);
  */
 void gpiod_unexport(struct gpio_desc *desc)
 {
-       struct gpiod_data *desc_data = NULL;
+       struct gpiod_data *tmp, *desc_data = NULL;
        struct gpiodev_data *gdev_data;
        struct gpio_device *gdev;
 
@@ -945,9 +945,12 @@ void gpiod_unexport(struct gpio_desc *desc)
                if (!gdev_data)
                        return;
 
-               list_for_each_entry(desc_data, &gdev_data->exported_lines, list)
-                       if (gpiod_is_equal(desc, desc_data->desc))
+               list_for_each_entry(tmp, &gdev_data->exported_lines, list) {
+                       if (gpiod_is_equal(desc, tmp->desc)) {
+                               desc_data = tmp;
                                break;
+                       }
+               }
 
                if (!desc_data)
                        return;