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

From: Arnav Kapoor

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


sx9360_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: 1cdb4c47f7f5 ("iio:proximity:sx9360: Add sx9360 support")
Signed-off-by: Arnav Kapoor <kapoorarnav43@xxxxxxxxx>
---
drivers/iio/proximity/sx9360.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/proximity/sx9360.c b/drivers/iio/proximity/sx9360.c
index 9a2796f67949..21c1801cc2ff 100644
--- a/drivers/iio/proximity/sx9360.c
+++ b/drivers/iio/proximity/sx9360.c
@@ -528,9 +528,14 @@ static int sx9360_write_thresh(struct sx_common_data *data, int _val)

static int sx9360_write_hysteresis(struct sx_common_data *data, int _val)
{
- unsigned int hyst, val = _val;
+ unsigned int hyst, val;
int ret, pthresh;

+ if (_val < 0)
+ return -EINVAL;
+
+ val = _val;
+
ret = sx9360_read_thresh(data, &pthresh);
if (ret < 0)
return ret;
--
2.53.0