]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
gpio: make gpiod_is_equal() arguments stricter
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Fri, 20 Jun 2025 12:58:02 +0000 (14:58 +0200)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Mon, 30 Jun 2025 06:58:26 +0000 (08:58 +0200)
It makes no sense for a GPIO descriptor comparator to return true when
the arguments passed to it are NULL or IS_ERR(). Let's validate both and
return false unless both are valid GPIO descriptors.

Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Link: https://lore.kernel.org/all/Z_aFBfjb17JxOwyk@black.fi.intel.com/
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Acked-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Link: https://lore.kernel.org/r/20250620-gpiod-is-equal-improv-v1-2-a75060505d2c@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
drivers/gpio/gpiolib.c

index 6b4f94c3887fcbe1d90a0ff5800100e9ae7ad5b9..7b2174b10219964a3f106562e1685a6d900d56c8 100644 (file)
@@ -278,20 +278,6 @@ struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc)
 }
 EXPORT_SYMBOL_GPL(gpiod_to_gpio_device);
 
-/**
- * gpiod_is_equal() - Check if two GPIO descriptors refer to the same pin.
- * @desc: Descriptor to compare.
- * @other: The second descriptor to compare against.
- *
- * Returns:
- * True if the descriptors refer to the same physical pin. False otherwise.
- */
-bool gpiod_is_equal(const struct gpio_desc *desc, const struct gpio_desc *other)
-{
-       return desc == other;
-}
-EXPORT_SYMBOL_GPL(gpiod_is_equal);
-
 /**
  * gpio_device_get_base() - Get the base GPIO number allocated by this device
  * @gdev: GPIO device
@@ -400,6 +386,21 @@ static int validate_desc(const struct gpio_desc *desc, const char *func)
                return; \
        } while (0)
 
+/**
+ * gpiod_is_equal() - Check if two GPIO descriptors refer to the same pin.
+ * @desc: Descriptor to compare.
+ * @other: The second descriptor to compare against.
+ *
+ * Returns:
+ * True if the descriptors refer to the same physical pin. False otherwise.
+ */
+bool gpiod_is_equal(const struct gpio_desc *desc, const struct gpio_desc *other)
+{
+       return validate_desc(desc, __func__) > 0 &&
+              !IS_ERR_OR_NULL(other) && desc == other;
+}
+EXPORT_SYMBOL_GPL(gpiod_is_equal);
+
 static int gpiochip_get_direction(struct gpio_chip *gc, unsigned int offset)
 {
        int ret;