[PATCH v4 1/3] iio: adc: at91_adc: fix NULL pointer dereference on trigger remove

From: Abdelnasser Hussein

Date: Tue Nov 17 2026 - 09:06:07 EST


When the "atmel,adc-use-external-triggers" property is not set in the
Device Tree, at91_adc_trigger_init() skips the external trigger slots
and leaves the corresponding entries in st->trig as NULL.

In the error rollback path of at91_adc_trigger_init() and in
at91_adc_trigger_remove(), iio_trigger_unregister() and
iio_trigger_free() are called on every slot without checking for NULL,
causing a NULL pointer dereference whenever an external trigger slot
was skipped, either because of a partial allocation failure or because
the external triggers are simply not used.

Skip NULL trigger slots in both teardown loops so that only triggers
that were actually allocated are unregistered and freed.

Fixes: 0e589d5fb317 ("ARM: AT91: IIO: Add AT91 ADC driver.")
Signed-off-by: Abdelnasser Hussein <abdelnasserhussein11@xxxxxxxxx>
---
drivers/iio/adc/at91_adc.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/drivers/iio/adc/at91_adc.c b/drivers/iio/adc/at91_adc.c
index 0ac74ad92fca..c7fc06214c05 100644
--- a/drivers/iio/adc/at91_adc.c
+++ b/drivers/iio/adc/at91_adc.c
@@ -660,6 +660,8 @@ static int at91_adc_trigger_init(struct iio_dev *idev)

error_trigger:
for (i--; i >= 0; i--) {
+ if (!st->trig[i])
+ continue;
iio_trigger_unregister(st->trig[i]);
iio_trigger_free(st->trig[i]);
}
@@ -673,6 +675,8 @@ static void at91_adc_trigger_remove(struct iio_dev *idev)
int i;

for (i = 0; i < st->caps->trigger_number; i++) {
+ if (!st->trig[i])
+ continue;
iio_trigger_unregister(st->trig[i]);
iio_trigger_free(st->trig[i]);
}
--
2.56.0