Re: [PATCH v2 2/4] iio: proximity: hx9023s: Protect against division by zero in set_samp_freq

From: Yasin Lee

Date: Thu Feb 12 2026 - 11:48:43 EST


On Thu, Feb 12, 2026 at 6:56 PM Andy Shevchenko
<andriy.shevchenko@xxxxxxxxx> wrote:
>
> On Thu, Feb 12, 2026 at 02:26:53PM +0800, Yasin Lee wrote:
> > Avoid division by zero when sampling frequency is unspecified by
> > falling back to a default 100ms sampling period.
>
> ...
>
> > - period_ms = div_u64(NANO, (val * MEGA + val2));
> > + if (val || val2)
> > + period_ms = div_u64(NANO, (val * MEGA + val2));
>
> While at it, drop unneeded parentheses.
>
> > + else
> > + /* Fallback to a safe default sampling period */
> > + period_ms = 100;
>
> Not sure about this. Perhaps we should rather do
>
> if (!val || !val2)
> return -EINVAL;
>
> ?
>

Agreed. I will update this in v3 as below .
...
if (!val && !val2)
return -EINVAL;

period_ms = div_u64(NANO, val * MEGA + val2);
...

> --
> With Best Regards,
> Andy Shevchenko
>
>