Skip to contentSkip to conversation

hwmon: (emc6w201) Fix DIV_ROUND_CLOSEST problem with unsigned divisors

PATCH Superseded Guenter Roeck 2 participants 3 messages Dec 19, 2012 Hardware monitoring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/1 hwmon: (emc6w201) Fix DIV_ROUND_CLOSEST problem with unsigned divisors

Result of DIV_ROUND_CLOSEST is undefined for negative dividends if the divisor variable type is unsigned. Fix by declaring dividend as unsigned.

---
 drivers/hwmon/emc6w201.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/emc6w201.c +2 -2
index a98c917..c450c7d 100644
@@ -208,10 +208,10 @@ static ssize_t set_in(struct device *dev, struct device_attribute *devattr,
 	int sf = to_sensor_dev_attr_2(devattr)->index;
 	int nr = to_sensor_dev_attr_2(devattr)->nr;
 	int err;
-	long val;
+	unsigned long val;
 	u8 reg;
 
-	err = kstrtol(buf, 10, &val);
+	err = kstrtoul(buf, 10, &val);
 	if (err < 0)
 		return err;
 
Signature · 7 lines
1.7.9.7


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#1 lore raw
Quoted 15 lines · Guenter Roeck

Result of DIV_ROUND_CLOSEST is undefined for negative dividends if the divisor variable type is unsigned. Fix by declaring dividend as unsigned.

---
 drivers/hwmon/emc6w201.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/emc6w201.c +0 -0
index a98c917..c450c7d 100644
@@ -208,10 +208,10 @@ static ssize_t set_in(struct device *dev, struct device_attribute *devattr,
 	int sf = to_sensor_dev_attr_2(devattr)->index;
 	int nr = to_sensor_dev_attr_2(devattr)->nr;

drivers/hwmon/emc6w201.c static ssize_t set_in(struct device *dev, struct device_attribute *devattr, · line 217

Quoted 10 lines
drivers/hwmon/emc6w201.c +2 -2
 	int err;
-	long val;
+	unsigned long val;
 	u8 reg;
 
-	err = kstrtol(buf, 10, &val);
+	err = kstrtoul(buf, 10, &val);
 	if (err < 0)
 		return err;
 

Nack, sorry. Negative voltage input values must be supported (and clamped to 0 if needs be). Rationale: the value written can result from applying a compute formula, so the user may not even realize he/she is trying to write a negative value.

Just change the type of nominal_mv to s16 or even int?

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
author maintainer
#2 lore raw
In reply to Guenter Roeck
Quoted 32 lines · Jean Delvare
Quoted 15 lines · Guenter Roeck

Result of DIV_ROUND_CLOSEST is undefined for negative dividends if the divisor variable type is unsigned. Fix by declaring dividend as unsigned.

---
 drivers/hwmon/emc6w201.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/emc6w201.c +0 -0
index a98c917..c450c7d 100644
@@ -208,10 +208,10 @@ static ssize_t set_in(struct device *dev, struct device_attribute *devattr,
 	int sf = to_sensor_dev_attr_2(devattr)->index;
 	int nr = to_sensor_dev_attr_2(devattr)->nr;

drivers/hwmon/emc6w201.c static ssize_t set_in(struct device *dev, struct device_attribute *devattr, · line 217

Quoted 10 lines
drivers/hwmon/emc6w201.c +2 -2
 	int err;
-	long val;
+	unsigned long val;
 	u8 reg;
 
-	err = kstrtol(buf, 10, &val);
+	err = kstrtoul(buf, 10, &val);
 	if (err < 0)
 		return err;
 

Nack, sorry. Negative voltage input values must be supported (and clamped to 0 if needs be). Rationale: the value written can result from applying a compute formula, so the user may not even realize he/she is trying to write a negative value.

Quoted 2 lines

Just change the type of nominal_mv to s16 or even int?

No problem, that was my other solution anyway. I'll send an updated patch in a minute.

Thanks,
Guenter

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