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

From: Andy Shevchenko

Date: Mon Nov 23 2026 - 06:15:51 EST


On Sun, Nov 22, 2026 at 01:16:43PM +0530, Arnav Kapoor 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.

...

> 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;

You can validate assigned value instead as there are no side-effects.
But while I wrote it, I have noticed that there is actually one, we
modify the sign of the value. With that in mind maybe better to have

/*
* Assume only positive input, negative, after conversion to unsigned
* is unsupported. // or something like this
*/
val = _val;
if (val > INT_MAX)
return -EINVAL;


> ret = sx9324_read_thresh(data, chan, &pthresh);
> if (ret < 0)
> return ret;

--
With Best Regards,
Andy Shevchenko