Re: [PATCH v2 7/7] iio: proximity: isl29501: Fix division by zero and overflow in isl29501_set_freq()

From: Andy Shevchenko

Date: Mon Nov 23 2026 - 06:31:27 EST


On Mon, Nov 23, 2026 at 07:50:33AM +0000, Lee Jones wrote:
> isl29501_set_freq() computes freq = val * 1000000 + val2 % 1000000 in a
> signed 32-bit int and passes it directly to do_div() without checking
> for zero or negative inputs. Writing 0 to the sampling_frequency
> attribute triggers a division by zero in the kernel, while valid
> frequencies above 2147 Hz up to the sensor's 2222 Hz limit overflow
> signed int.
>
> Validate that val and val2 are non-negative, compute freq using
> HZ_PER_MHZ as an unsigned int within [1, 2222 * HZ_PER_MHZ], and return
> -ERANGE otherwise.

...

> - int freq;
> unsigned long long sample_time;
> + unsigned int freq;
> +
> + if (val < 0 || val > 2222 || val2 < 0)
> + return -ERANGE;
>
> /* sample_freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */
> - freq = val * 1000000 + val2 % 1000000;
> - sample_time = 2222ULL * 1000000ULL;
> + freq = val * HZ_PER_MHZ + val2 % HZ_PER_MHZ;
> + if (!freq || freq > 2222U * HZ_PER_MHZ)

But the second one is implied already by the val validation above, no?

> + return -ERANGE;
> +
> + sample_time = 2222ULL * HZ_PER_MHZ;

Taking this into consideration, perhaps not to touch these lines at all?
Just add a check for 0 above?

if (val < 0 || val > 2222 || val2 < 0 || (val == 0 && val2 == 0))

?

--
With Best Regards,
Andy Shevchenko