Re: [PATCH v2 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq
From: Andy Shevchenko
Date: Fri Nov 27 2026 - 06:17:30 EST
On Fri, Nov 27, 2026 at 03:06:46AM +0530, Arnav Kapoor wrote:
> hx9023s_set_samp_freq() passes val * MEGA + val2 to div_u64(), whose
> divisor is a u32, so the sum is truncated to 32 bits. MEGA is an
> unsigned long, so on 64-bit the product itself does not overflow, but
> the truncation does, and on 32-bit the product already wraps. Either
> way, any value for which val * MEGA + val2 is a multiple of 2^32 gives a
> zero divisor, e.g.
>
> echo 67108864 > sampling_frequency
>
> (67108864 * 10^6 = 15625 * 2^32) triggers a divide error, on both 32-bit
> and 64-bit architectures. Negative values wrap the same way (-67108864
> also gives 0), which the check added by commit a318cfc08537 ("iio:
> proximity: hx9023s: Protect against division by zero in set_samp_freq")
> does not catch, as it only rejects val == 0 && val2 == 0.
>
> Reject negative values, and reject frequencies of 1 kHz and above: they
> give a period of at most 1 ms, below the smallest entry (2 ms) of
> hx9023s_samp_freq_table[], so they never matched anyway. With them gone
> the divisor stays below NANO and fits in 32 bits, so a plain division is
> enough.
...
> - period_ms = div_u64(NANO, (val * MEGA + val2));
> + /*
> + * 1 kHz or more gives a period below the smallest table entry. Rejecting
> + * it here also keeps the divisor below NANO, so it fits in 32 bits.
> + */
> + if (val >= KILO)
if (val >= (1 * HZ_PER_KHZ))
> + return -EINVAL;
> +
> + period_ms = NANO / (val * MEGA + val2);
Can you find dedicated multipliers like
period_ms = (MSEC_PER_SEC * KILO) / (val * HZ_PER_KHZ * KILO + val2);
? And seems this KILO multiplier has to be explained, as I don't get
the precision used here. Or maybe something even better in your mind?
--
With Best Regards,
Andy Shevchenko