diff mbox series

[2/2] hwmon: (max6697) Fix underflow when writing limit attributes

Message ID 20240713213402.1085599-2-linux@roeck-us.net (mailing list archive)
State Accepted
Headers show
Series [1/2] hwmon: (max6697) Fix swapped temp{1,8} critical alarms | expand

Commit Message

Guenter Roeck July 13, 2024, 9:34 p.m. UTC
Using DIV_ROUND_CLOSEST() on an unbound value can result in underflows.
Indeed, module test scripts report:

temp1_max: Suspected underflow: [min=0, read 255000, written -9223372036854775808]
temp1_crit: Suspected underflow: [min=0, read 255000, written -9223372036854775808]

Fix by introducing an extra set of clamping.

Fixes: 5372d2d71c46 ("hwmon: Driver for Maxim MAX6697 and compatibles")
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/max6697.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Tzung-Bi Shih July 14, 2024, 7:07 a.m. UTC | #1
On Sat, Jul 13, 2024 at 02:34:02PM -0700, Guenter Roeck wrote:
> Using DIV_ROUND_CLOSEST() on an unbound value can result in underflows.
> Indeed, module test scripts report:
> 
> temp1_max: Suspected underflow: [min=0, read 255000, written -9223372036854775808]
> temp1_crit: Suspected underflow: [min=0, read 255000, written -9223372036854775808]
> 
> Fix by introducing an extra set of clamping.
> 
> Fixes: 5372d2d71c46 ("hwmon: Driver for Maxim MAX6697 and compatibles")
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>

Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
diff mbox series

Patch

diff --git a/drivers/hwmon/max6697.c b/drivers/hwmon/max6697.c
index 1111b2ea55ee..20981f9443dd 100644
--- a/drivers/hwmon/max6697.c
+++ b/drivers/hwmon/max6697.c
@@ -311,6 +311,7 @@  static ssize_t temp_store(struct device *dev,
 		return ret;
 
 	mutex_lock(&data->update_lock);
+	temp = clamp_val(temp, -1000000, 1000000);	/* prevent underflow */
 	temp = DIV_ROUND_CLOSEST(temp, 1000) + data->temp_offset;
 	temp = clamp_val(temp, 0, data->type == max6581 ? 255 : 127);
 	data->temp[nr][index] = temp;