Skip to content

Commit c0a1ecb

Browse files
linuswrichardweinberger
authored andcommitted
gpio: make library immune to error pointers
Most functions that take a GPIO descriptor in need to check the descriptor for IS_ERR(). We do this mostly in the VALIDATE_DESC() macro except for the gpiod_to_irq() function which needs special handling. Cc: stable@vger.kernel.org Reported-by: Grygorii Strashko <grygorii.strashko@ti.com> Reviewed-by: Grygorii Strashko <grygorii.strashko@ti.com> Acked-by: Alexandre Courbot <acourbot@nvidia.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 39243ee commit c0a1ecb

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

drivers/gpio/gpiolib.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,8 +1373,12 @@ static int __gpiod_request(struct gpio_desc *desc, const char *label)
13731373
#define VALIDATE_DESC(desc) do { \
13741374
if (!desc) \
13751375
return 0; \
1376+
if (IS_ERR(desc)) { \
1377+
pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1378+
return PTR_ERR(desc); \
1379+
} \
13761380
if (!desc->gdev) { \
1377-
pr_warn("%s: invalid GPIO\n", __func__); \
1381+
pr_warn("%s: invalid GPIO (no device)\n", __func__); \
13781382
return -EINVAL; \
13791383
} \
13801384
if ( !desc->gdev->chip ) { \
@@ -1386,8 +1390,12 @@ static int __gpiod_request(struct gpio_desc *desc, const char *label)
13861390
#define VALIDATE_DESC_VOID(desc) do { \
13871391
if (!desc) \
13881392
return; \
1393+
if (IS_ERR(desc)) { \
1394+
pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1395+
return; \
1396+
} \
13891397
if (!desc->gdev) { \
1390-
pr_warn("%s: invalid GPIO\n", __func__); \
1398+
pr_warn("%s: invalid GPIO (no device)\n", __func__); \
13911399
return; \
13921400
} \
13931401
if (!desc->gdev->chip) { \
@@ -2061,7 +2069,7 @@ int gpiod_to_irq(const struct gpio_desc *desc)
20612069
* requires this function to not return zero on an invalid descriptor
20622070
* but rather a negative error number.
20632071
*/
2064-
if (!desc || !desc->gdev || !desc->gdev->chip)
2072+
if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
20652073
return -EINVAL;
20662074

20672075
chip = desc->gdev->chip;

0 commit comments

Comments
 (0)