Skip to contentSkip to conversation

hwmon: (ina3221) Clamp shunt resistor value from DT

PATCH Superseded Nicolin Chen 2 participants 2 messages Oct 8, 2018 Hardware monitoring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: (ina3221) Clamp shunt resistor value from DT

The input->shunt_resistor is int type while the value from DT is unsigned int. Meanwhile, a divide-by-zero error would happen if the value is 0. So this patch just simply clamps the value.

---
 drivers/hwmon/ina3221.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/hwmon/ina3221.c +3 -1
index c3a497aed345..ce8f0a8c4982 100644
@@ -536,8 +536,10 @@ static int ina3221_probe_child_from_dt(struct device *dev,
 	of_property_read_string(child, "label", &input->label);
 
 	/* Overwrite default shunt resistor value optionally */
-	if (!of_property_read_u32(child, "shunt-resistor-micro-ohms", &val))
+	if (!of_property_read_u32(child, "shunt-resistor-micro-ohms", &val)) {
+		val = clamp_val(val, 1, INT_MAX);
 		input->shunt_resistor = val;
+	}
 
 	return 0;
 }
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 4 lines · Nicolin Chen

The input->shunt_resistor is int type while the value from DT is unsigned int. Meanwhile, a divide-by-zero error would happen if the value is 0. So this patch just simply clamps the value.

I think it would be better to validate the numbers here instead of silently accepting bad values, and return -EINVAL if the property value is bad.

Thanks,
Guenter

Quoted 24 lines
---
 drivers/hwmon/ina3221.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/hwmon/ina3221.c +3 -1
index c3a497aed345..ce8f0a8c4982 100644
@@ -536,8 +536,10 @@ static int ina3221_probe_child_from_dt(struct device *dev,
 	of_property_read_string(child, "label", &input->label);
 
 	/* Overwrite default shunt resistor value optionally */
-	if (!of_property_read_u32(child, "shunt-resistor-micro-ohms", &val))
+	if (!of_property_read_u32(child, "shunt-resistor-micro-ohms", &val)) {
+		val = clamp_val(val, 1, INT_MAX);
 		input->shunt_resistor = val;
+	}
 
 	return 0;
 }
Signature · 1 line
2.17.1