From: Salah Triki <salah.triki@gmail.com>
To: "Nuno Sá" <nuno.sa@analog.com>,
"Michael Hennerich" <Michael.Hennerich@analog.com>,
"Esteban Blanc" <eblanc@baylibre.com>,
"Jonathan Cameron" <jic23@kernel.org>,
"David Lechner" <dlechner@baylibre.com>,
"Andy Shevchenko" <andy@kernel.org>
Cc: linux@analog.com, linux-iio@vger.kernel.org,
linux-kernel@vger.kernel.org, Salah Triki <salah.triki@gmail.com>
Subject: [PATCH v4] iio: adc: ad4030: fix invalid oversampling_ratio validation
Date: Tue, 1 Sep 2026 11:42:43 +0100 [thread overview]
Message-ID: <20260901104243.48732-1-salah.triki@gmail.com> (raw)
In ad4030_set_avg_frame_len(), the logarithm is calculated before input
validation. Passing zero or negative values leads to an undefined result
from ilog2().
Validate that the input is strictly positive prior to computing its
logarithm to ensure only valid values are processed.
Fixes: 949abd1ca5a4 ("iio: adc: ad4030: add averaging support")
Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Salah Triki <salah.triki@gmail.com>
---
Changes in v4:
- Reordered local variables to enforce strict reversed Christmas tree layout
(per Andy Shevchenko).
- Rephrased commit log in plain English without C-specific terms
(per Andy Shevchenko).
- Converted AI assistance note into a formal Assisted-by tag
(per Andy Shevchenko).
Changes in v3:
- Dropped the !is_power_of_2() check to preserve standard IIO attribute
rounding behavior, per feedback from David Lechner and Jonathan Cameron.
Changes in v2:
- Added note stating the issue was identified with assistance from
Claude AI and verified manually.
- Removed initialization of avg_log2 at declaration.
drivers/iio/adc/ad4030.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/adc/ad4030.c b/drivers/iio/adc/ad4030.c
index 9c5f19321e3b..17b0ca7f1cce 100644
--- a/drivers/iio/adc/ad4030.c
+++ b/drivers/iio/adc/ad4030.c
@@ -746,14 +746,16 @@ static int ad4030_set_chan_calibbias(struct iio_dev *indio_dev,
static int ad4030_set_avg_frame_len(struct iio_dev *dev, int avg_val)
{
struct ad4030_state *st = iio_priv(dev);
- unsigned int avg_log2 = ilog2(avg_val);
unsigned int last_avg_idx = ARRAY_SIZE(ad4030_average_modes) - 1;
+ unsigned int avg_log2;
int freq_hz;
int ret;
- if (avg_val < 0 || avg_val > ad4030_average_modes[last_avg_idx])
+ if (avg_val <= 0 || avg_val > ad4030_average_modes[last_avg_idx])
return -EINVAL;
+ avg_log2 = ilog2(avg_val);
+
if (st->offload_trigger) {
/*
* The sample averaging and sampling frequency configurations
--
2.43.0
next reply other threads:[~2026-09-01 10:42 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-01 10:42 Salah Triki [this message]
2026-09-01 11:08 ` [PATCH v4] iio: adc: ad4030: fix invalid oversampling_ratio validation Andy Shevchenko
2026-09-01 14:38 ` Nuno Sá
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=20260901104243.48732-1-salah.triki@gmail.com \
--to=salah.triki@gmail.com \
--cc=Michael.Hennerich@analog.com \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=eblanc@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@analog.com \
--cc=nuno.sa@analog.com \
/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