Skip to contentSkip to conversation

hwmon: (lm92) Prevent overflow problem when writing large limits

PATCH Superseded Axel Lin 1 participant 1 message Jul 31, 2014 Hardware monitoring

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Axel Lin

author
#0 lore raw

PATCH v2 2/2 hwmon: (lm92) Prevent overflow problem when writing large limits

On platforms with sizeof(int) < sizeof(long), writing a temperature limit larger than MAXINT will result in unpredictable limit values written to the chip. Avoid auto-conversion from long to int to fix the problem.
For set_temp_hyst, clamp the input values to the supported limits first to fix the problem.
Also uses clamp_val in TEMP_TO_REG to simplify the code a bit.

---
 drivers/hwmon/lm92.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/hwmon/lm92.c +3 -5
index d2060e2..481abe2 100644
@@ -74,12 +74,9 @@ static inline int TEMP_FROM_REG(s16 reg)
 	return reg / 8 * 625 / 10;
 }
 
-static inline s16 TEMP_TO_REG(int val)
+static inline s16 TEMP_TO_REG(long val)
 {
-	if (val <= -60000)
-		return -60000 * 10 / 625 * 8;
-	if (val >= 160000)
-		return 160000 * 10 / 625 * 8;
+	val = clamp_val(val, -60000, 160000);
 	return val * 10 / 625 * 8;
 }
 
@@ -206,6 +203,7 @@ static ssize_t set_temp_hyst(struct device *dev,
 	if (err)
 		return err;
 
+	val = clamp_val(val, -60000, 160000);
 	mutex_lock(&data->update_lock);
 	data->temp[t_hyst] = TEMP_FROM_REG(data->temp[attr->index]) - val;
 	i2c_smbus_write_word_swapped(client, LM92_REG_TEMP_HYST,
Signature · 9 lines
1.9.1




_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors