Re: [PATCH v4] iio: adc: ad4030: fix invalid oversampling_ratio validation
From: Nuno Sá
Date: Tue Sep 01 2026 - 10:59:17 EST
On Tue, Sep 01, 2026 at 02:08:44PM +0300, Andy Shevchenko wrote:
> 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.
I realize here it does not matter much but I tend to agree that the correct
check is against 1
>
> 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;
>
Also tend to agree with the above but no strong feeling.
- Nuno Sá
> > + avg_log2 = ilog2(avg_val);
>
> --
> With Best Regards,
> Andy Shevchenko
>
>