From: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
To: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Maxime Coquelin" <mcoquelin.stm32@gmail.com>,
"Alexandre Torgue" <alexandre.torgue@foss.st.com>,
"Olivier Moysan" <olivier.moysan@foss.st.com>,
linux-iio@vger.kernel.org,
linux-stm32@st-md-mailman.stormreply.com,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, Sashiko <sashiko-bot@kernel.org>,
stable@vger.kernel.org
Subject: Re: [PATCH] iio: adc: stm32-adc: fix possible division by zero in processed channel
Date: Wed, 16 Sep 2026 12:23:38 +0200 [thread overview]
Message-ID: <bd47e39c-18e0-46ba-8cdf-9ba35f387793@foss.st.com> (raw)
In-Reply-To: <aqpGy9oQkDC-xZGW@ashevche-desk.local>
On 9/16/26 09:35, Andy Shevchenko wrote:
> 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.
Hi Andy,
Euh, sorry but I don't understand what's wrong with indentation in the
commit message ? Could you clarify ?
>
> ...
>
>> - 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;
In case ret is an error; this lead to evaluate (e.g. read) *val from the
(maybe uninitialized) stack. Probably not an issue? But would prefer to
keep reading it only when ret >= 0, as done currently with the if
condition above ? (e.g. evaluate ret, before *val)
> ...
> if (mask == IIO_CHAN_INFO_PROCESSED) {
> if (ret < 0)
> return ret;
> if (tmp == 0)
> return -EINVAL;
I've started with something similar before, but we can't return directly
here. Must call iio_device_release_direct() first. This would add more
lines.
So I chose to implement above ternary ret = ret < 0 ? ret : -EINVAL, to
fall-through.
Please advise on the preferred way,
Best Regards,
Fabrice
>
> *val = STM32_ADC_VREFINT_VOLTAGE * adc->vrefint.vrefint_cal / tmp;
> }
>
>> iio_device_release_direct(indio_dev);
>> return ret;
>
next prev parent reply other threads:[~2026-09-16 10:24 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-15 16:10 [PATCH] iio: adc: stm32-adc: fix possible division by zero in processed channel Fabrice Gasnier
2026-09-16 7:35 ` Andy Shevchenko
2026-09-16 10:23 ` Fabrice Gasnier [this message]
2026-09-16 11:00 ` Andy Shevchenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bd47e39c-18e0-46ba-8cdf-9ba35f387793@foss.st.com \
--to=fabrice.gasnier@foss.st.com \
--cc=alexandre.torgue@foss.st.com \
--cc=andriy.shevchenko@intel.com \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-stm32@st-md-mailman.stormreply.com \
--cc=mcoquelin.stm32@gmail.com \
--cc=nuno.sa@analog.com \
--cc=olivier.moysan@foss.st.com \
--cc=sashiko-bot@kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox