Re: [PATCH v4] iio: adc: ad4030: fix invalid oversampling_ratio validation

From: Andy Shevchenko

Date: Tue Sep 01 2026 - 07:14:13 EST


On Tue, Sep 01, 2026 at 11:42:43AM +0100, Salah Triki wrote:
> 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.

...

> - 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;

I still find '<= 0' confusing. Why not check against 1?
And I would rather see an additional comment.

What about

/* Reject unsupported modes */
if (avg_val > ad4030_average_modes[last_avg_idx])
return -EINVAL;

/* Avoid invalid values for logarithm since it's undefined */
if (avg_val < 1)
// In this case, when condition is split, if (avg_val <= 0) is also possible
return -EINVAL;

> + avg_log2 = ilog2(avg_val);

--
With Best Regards,
Andy Shevchenko