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

From: Jonathan Cameron

Date: Sat Dec 05 2026 - 19:35:17 EST


On Thu, 3 Dec 2026 13:51:27 +0200
Andy Shevchenko <andriy.shevchenko@xxxxxxxxx> wrote:

> On Wed, Dec 02, 2026 at 08:21:12PM +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 val and val2 up front so that the requested frequency lies
> > within (0, 2222] Hz with val2 in [0, MICROHZ_PER_HZ), compute freq as
> > an unsigned int using MICROHZ_PER_HZ, and return -ERANGE otherwise.
>
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxx>
I tweaked as suggested below.

Applied to the testing branch of iio.git. Getting fairly late
in the cycle + I'm going to be travelling over next few weeks, so
I'm going to be holding this for the merge window.

Thanks

Jonathan

>
> ...
>
> > +++ b/drivers/iio/proximity/isl29501.c
>
> > #include <linux/iio/trigger_consumer.h>
> > #include <linux/iio/buffer.h>
> > #include <linux/iio/triggered_buffer.h>
> > +#include <linux/units.h>
>
> Yes, the headers are mess in the driver, but please make them as
>
> #include <linux/kernel.h>
> #include <linux/module.h>
> #include <linux/i2c.h>
> #include <linux/err.h>
> #include <linux/units.h>
>
> #include <linux/iio/iio.h>
> #include <linux/iio/sysfs.h>
> #include <linux/iio/trigger_consumer.h>
> #include <linux/iio/buffer.h>
> #include <linux/iio/triggered_buffer.h>
>
> (this will be a minimal intrusion with a small step towards an improvement).
>