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

From: Andy Shevchenko

Date: Thu Nov 19 2026 - 12:34:45 EST


On Thu, Nov 19, 2026 at 03:30:42PM +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 as an unsigned
> int within [1, 2222000000], and return -ERANGE otherwise.

...

> - int freq;
> + unsigned int freq;
> unsigned long long sample_time;

Make it reversed xmas tree while at it?

> + if (val < 0 || val2 < 0 || val > 2222)
> + return -ERANGE;

I would see it be grouped by variable

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

> /* sample_freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */
> - freq = val * 1000000 + val2 % 1000000;
> + freq = val * 1000000U + val2 % 1000000U;

While at it Use HZ_PER_MHZ and similar multipliers from units.h.

> + if (!freq || freq > 2222000000U)
> + return -ERANGE;

--
With Best Regards,
Andy Shevchenko