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

From: Arnav Kapoor

Date: Sun Nov 22 2026 - 02:47:21 EST


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>
---
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 57fe78089167..35f0cca5ede3 100644
--- a/drivers/iio/proximity/sx9324.c
+++ b/drivers/iio/proximity/sx9324.c
@@ -636,9 +636,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;
--
2.53.0