mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] hwmon: (ina3221) Validate shunt resistor value from DT
@ 2018-10-08 21:24 Nicolin Chen
  2018-10-10  0:37 ` Guenter Roeck
  0 siblings, 1 reply; 2+ messages in thread
From: Nicolin Chen @ 2018-10-08 21:24 UTC (permalink / raw)
  To: jdelvare, linux; +Cc: afd, linux-hwmon, linux-kernel

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.

Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
---
Changelog
v1->v2:
 * Validate the value and error out

 drivers/hwmon/ina3221.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c
index c3a497aed345..4f3ed24efe8e 100644
--- a/drivers/hwmon/ina3221.c
+++ b/drivers/hwmon/ina3221.c
@@ -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;
 }
-- 
2.17.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] hwmon: (ina3221) Validate shunt resistor value from DT
  2018-10-08 21:24 [PATCH v2] hwmon: (ina3221) Validate shunt resistor value from DT Nicolin Chen
@ 2018-10-10  0:37 ` Guenter Roeck
  0 siblings, 0 replies; 2+ messages in thread
From: Guenter Roeck @ 2018-10-10  0:37 UTC (permalink / raw)
  To: Nicolin Chen; +Cc: jdelvare, afd, linux-hwmon, linux-kernel

On Mon, Oct 08, 2018 at 02:24:51PM -0700, Nicolin Chen wrote:
> 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.
> 
> Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>

Applied.

Thanks,
Guenter

> ---
> Changelog
> v1->v2:
>  * Validate the value and error out
> 
>  drivers/hwmon/ina3221.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c
> index c3a497aed345..4f3ed24efe8e 100644
> --- a/drivers/hwmon/ina3221.c
> +++ b/drivers/hwmon/ina3221.c
> @@ -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;
>  }

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-10-10  0:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-08 21:24 [PATCH v2] hwmon: (ina3221) Validate shunt resistor value from DT Nicolin Chen
2018-10-10  0:37 ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®