Skip to contentSkip to conversation

hwmon: (ina3221) Validate shunt resistor value from DT

PATCH Applied Nicolin Chen 2 participants 2 messages Oct 8, 2018 – Oct 10, 2018 Hardware monitoring

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hwmon: (ina3221) Validate 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 validates the value.

---
Changelog
v1->v2:
* Validate the value and error out

 drivers/hwmon/ina3221.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/hwmon/ina3221.c +7 -1
index c3a497aed345..4f3ed24efe8e 100644
@@ -536,8 +536,14 @@ 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)) {
+		if (val < 1 || val > INT_MAX) {
+			dev_err(dev, "invalid shunt resistor value %u of %s\n",
+				val, child->name);
+			return -EINVAL;
+		}
 		input->shunt_resistor = val;
+	}
 
 	return 0;
 }
Signature · 1 line
2.17.1
maintainer Applied
#1 lore raw
Quoted 5 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 validates the value.

Applied.

Thanks,
Guenter

Quoted 28 lines

---
Changelog
v1->v2:
* Validate the value and error out

 drivers/hwmon/ina3221.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/hwmon/ina3221.c +7 -1
index c3a497aed345..4f3ed24efe8e 100644
@@ -536,8 +536,14 @@ 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)) {
+		if (val < 1 || val > INT_MAX) {
+			dev_err(dev, "invalid shunt resistor value %u of %s\n",
+				val, child->name);
+			return -EINVAL;
+		}
 		input->shunt_resistor = val;
+	}
 
 	return 0;
 }