gpiochip_dup_line_label() returns ERR_PTR(-ENOMEM) when kstrdup() fails to allocate memory for a line label. In for_each_hwgpio_in_range(), the loop increment unconditionally passes _label to kfree(). When an allocation fails, _label holds an error pointer, resulting in a crash as the iterator advances. Because for_each_hwgpio() and for_each_requested_gpio*() are both implemented on top of this helper, callers of both macro families are affected. This was reproduced by forcing the kstrdup() to fail with CONFIG_FAILSLAB while a pinctrl driver using for_each_hwgpio() dumped its GPIO lines. Commit dae739137441 ("gpiolib: don't report -ENOMEM for a requested line without a label") fixed the case where unlabelled lines returned an error pointer, but a genuine kstrdup() failure under memory pressure can still return -ENOMEM. Don't pass an error pointer to kfree() in the loop increment. Fixes: 6fd9c9933475 ("gpiolib: use gpiochip_dup_line_label() in for_each helpers") Cc: # v6.8+ Signed-off-by: Maxin B. John --- include/linux/gpio/driver.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index 4077dc678cae..dd5a8b9bd3dd 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -556,7 +556,7 @@ DEFINE_CLASS(_gpiochip_for_each_data, #define for_each_hwgpio_in_range(_chip, _i, _base, _size, _label) \ for (CLASS(_gpiochip_for_each_data, _data)(&_label, &_i); \ _i < _size; \ - _i++, kfree(_label), _label = NULL) \ + _i++, kfree(IS_ERR(_label) ? NULL : _label), _label = NULL) \ for_each_if(!IS_ERR(_label = gpiochip_dup_line_label(_chip, _base + _i))) /** -- 2.47.3