Re: [PATCH] iio: adc: stm32-adc: fix possible division by zero in processed channel
From: Andy Shevchenko
Date: Wed Sep 16 2026 - 03:36:23 EST
On Tue, Sep 15, 2026 at 06:10:40PM +0200, Fabrice Gasnier wrote:
> 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.
Something went very wrong with the indentation of the above.
...
> - 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;
> + }
Reuse of the *val makes code harder to follow. Add a temporary variable for
this and do something like this (also note other simplifications)
tmp_choose_good_name = *val;
...
if (mask == IIO_CHAN_INFO_PROCESSED) {
if (ret < 0)
return ret;
if (tmp == 0)
return -EINVAL;
*val = STM32_ADC_VREFINT_VOLTAGE * adc->vrefint.vrefint_cal / tmp;
}
> iio_device_release_direct(indio_dev);
> return ret;
--
With Best Regards,
Andy Shevchenko