Re: [PATCH v3 3/6] iio: proximity: sx9324: reject negative hysteresis values

From: Jonathan Cameron

Date: Sun Nov 29 2026 - 19:39:57 EST


On Sat, 28 Nov 2026 17:24:51 +0530
Arnav Kapoor <kapoorarnav43@xxxxxxxxx> wrote:

> sx9324_write_hysteresis() copies the value into an unsigned int before
> comparing it with the threshold, so a negative value becomes a large
> one, passes the "val >= pthresh >> 2" check and sets the largest
> hysteresis instead of being rejected. The comparison is done in
> unsigned int, so this happens on both 32-bit and 64-bit architectures.
>
> Return -EINVAL for negative values, before converting them.
>
> Fixes: 4c18a890dff8 ("iio:proximity:sx9324: Add SX9324 support")
> Signed-off-by: Arnav Kapoor <kapoorarnav43@xxxxxxxxx>

Related to this, I don't see anything checking val2 at the caller of
this function. Should probably also reject val2 != 0 to avoid
odd things like -0.999999 being accepted as meaning 0.

This is an unrelated fix though so doesn't need to be in this patch.
> ---
> drivers/iio/proximity/sx9324.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c
> index 3f2f15d237e9..ef217f055472 100644
> --- a/drivers/iio/proximity/sx9324.c
> +++ b/drivers/iio/proximity/sx9324.c
> @@ -646,9 +646,14 @@ static int sx9324_write_thresh(struct sx_common_data *data,
> static int sx9324_write_hysteresis(struct sx_common_data *data,
> const struct iio_chan_spec *chan, int _val)
> {
> - unsigned int hyst, val = _val;
> + unsigned int hyst, val;
> int ret, pthresh;
>
> + if (_val < 0)
> + return -EINVAL;
> +
> + val = _val;
> +
> ret = sx9324_read_thresh(data, chan, &pthresh);
> if (ret < 0)
> return ret;