[PATCH] iio: adc: stm32-adc: fix possible division by zero in processed channel

Fabrice Gasnier fabrice.gasnier at foss.st.com
Tue Sep 15 09:10:40 PDT 2026


In case the conversion has failed or returned zero, processing *val
can lead to a division by zero.
Need to check for errors, or converted value is zero, before processing
the data.
In case converted value is zero, e.g. the Vrefint channel, this should
be considered as invalid in all case.

Fixes: 0e346b2cfa85 ("iio: adc: stm32-adc: add vrefint calibration support")
Reported-by: Sashiko <sashiko-bot at kernel.org>
Link: https://lore.kernel.org/all/20260911161555.244F31F000FF@smtp.kernel.org/
Cc: stable at vger.kernel.org
Signed-off-by: Fabrice Gasnier <fabrice.gasnier at foss.st.com>
---
 drivers/iio/adc/stm32-adc.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c
index 5c6c06b269be..d290c5a8f86d 100644
--- a/drivers/iio/adc/stm32-adc.c
+++ b/drivers/iio/adc/stm32-adc.c
@@ -1609,8 +1609,12 @@ static int stm32_adc_read_raw(struct iio_dev *indio_dev,
 		else
 			ret = -EINVAL;
 
-		if (mask == IIO_CHAN_INFO_PROCESSED)
-			*val = STM32_ADC_VREFINT_VOLTAGE * adc->vrefint.vrefint_cal / *val;
+		if (mask == IIO_CHAN_INFO_PROCESSED) {
+			if (ret >= 0 && *val)
+				*val = STM32_ADC_VREFINT_VOLTAGE * adc->vrefint.vrefint_cal / *val;
+			else
+				ret = ret < 0 ? ret : -EINVAL;
+		}
 
 		iio_device_release_direct(indio_dev);
 		return ret;

---
base-commit: 8d3ae59288f1e7d58d76558a6ee96d533bc5019f
change-id: 20260915-adc-fix-div0-ae38365abfeb

Best regards,
--  
Fabrice Gasnier <fabrice.gasnier at foss.st.com>




More information about the linux-arm-kernel mailing list