[PATCH] hwmon: (it87) Clamp temperatures before rounding
From: Zhu Jun
Date: Wed Nov 11 2026 - 01:53:15 EST
TEMP_TO_REG() adds or subtracts 500 before clamping the result. Values
near LONG_MAX or LONG_MIN can overflow, causing a high temperature limit
to select the lowest register value, or vice versa.
Clamp the requested temperature to the signed 8-bit range before
rounding to whole degrees.
Fixes: 1da177e4c3f41 ("Linux-2.6.12-rc2")
Signed-off-by: Zhu Jun <zhujun2@xxxxxxxxxxxxxxxxxxxx>
---
drivers/hwmon/it87.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/hwmon/it87.c b/drivers/hwmon/it87.c
index b8ad1ed043b3..036e0b7ce01b 100644
--- a/drivers/hwmon/it87.c
+++ b/drivers/hwmon/it87.c
@@ -771,8 +771,8 @@ static inline u16 FAN16_TO_REG(long rpm)
#define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
1350000 / ((val) * 2))
-#define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
- ((val) + 500) / 1000), -128, 127))
+#define TEMP_TO_REG(val) \
+ DIV_ROUND_CLOSEST(clamp_val((val), -128000, 127000), 1000)
#define TEMP_FROM_REG(val) ((val) * 1000)
static u8 pwm_to_reg(const struct it87_data *data, long val)
--
2.51.0