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

Yasin Lee posted 5 patches 1 day, 23 hours ago
[PATCH 2/5] iio: proximity: hx9023s: Protect against division by zero in set_samp_freq
Posted by Yasin Lee 1 day, 23 hours ago
Avoid division by zero when sampling frequency is unspecified by
falling back to a default 100ms sampling period.

Signed-off-by: Yasin Lee <yasin.lee.x@gmail.com>
---
 drivers/iio/proximity/hx9023s.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index ad839db6b326..eb4902d18d74 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -719,7 +719,11 @@ static int hx9023s_set_samp_freq(struct hx9023s_data *data, int val, int val2)
 	struct device *dev = regmap_get_device(data->regmap);
 	unsigned int i, period_ms;
 
-	period_ms = div_u64(NANO, (val * MEGA + val2));
+	if (!val && !val2)
+		/* Fallback to a safe default sampling period */
+		period_ms = 100;
+	else
+		period_ms = div_u64(NANO, (val * MEGA + val2));
 
 	for (i = 0; i < ARRAY_SIZE(hx9023s_samp_freq_table); i++) {
 		if (period_ms == hx9023s_samp_freq_table[i])

-- 
2.43.0
Re: [PATCH 2/5] iio: proximity: hx9023s: Protect against division by zero in set_samp_freq
Posted by Andy Shevchenko 1 day, 18 hours ago
On Mon, Feb 09, 2026 at 11:37:03AM +0800, Yasin Lee wrote:
> Avoid division by zero when sampling frequency is unspecified by
> falling back to a default 100ms sampling period.

Fixes tag?

...

> +	if (!val && !val2)

What's wrong with the positive conditional?

	if (val || val2)
		...
	else
		...

> +		/* Fallback to a safe default sampling period */
> +		period_ms = 100;
> +	else
> +		period_ms = div_u64(NANO, (val * MEGA + val2));

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH 2/5] iio: proximity: hx9023s: Protect against division by zero in set_samp_freq
Posted by Yasin Lee 10 hours ago
On Mon, Feb 9, 2026 at 4:46 PM Andy Shevchenko
<andriy.shevchenko@intel.com> wrote:
>
> On Mon, Feb 09, 2026 at 11:37:03AM +0800, Yasin Lee wrote:
> > Avoid division by zero when sampling frequency is unspecified by
> > falling back to a default 100ms sampling period.
>
> Fixes tag?
>
 This is a proactive fix for an original implementation issue I found
as maintainer.

> ...
>
> > +     if (!val && !val2)
>
> What's wrong with the positive conditional?
>
Agreed. Positive conditional will be present in v2.

>         if (val || val2)
>                 ...
>         else
>                 ...
>
> > +             /* Fallback to a safe default sampling period */
> > +             period_ms = 100;
> > +     else
> > +             period_ms = div_u64(NANO, (val * MEGA + val2));
>
> --
> With Best Regards,
> Andy Shevchenko
>
>
Re: [PATCH 2/5] iio: proximity: hx9023s: Protect against division by zero in set_samp_freq
Posted by Andy Shevchenko 9 hours ago
On Wed, Feb 11, 2026 at 12:29:15AM +0800, Yasin Lee wrote:
> On Mon, Feb 9, 2026 at 4:46 PM Andy Shevchenko
> <andriy.shevchenko@intel.com> wrote:
> >
> > On Mon, Feb 09, 2026 at 11:37:03AM +0800, Yasin Lee wrote:
> > > Avoid division by zero when sampling frequency is unspecified by
> > > falling back to a default 100ms sampling period.
> >
> > Fixes tag?

>  This is a proactive fix for an original implementation issue I found as
>  maintainer.

Cool, any objections to use Fixes tag?

-- 
With Best Regards,
Andy Shevchenko