From: Guenter Roeck <linux@roeck-us.net>
To: Nicolin Chen <nicoleotsuka@gmail.com>
Cc: jdelvare@suse.com, afd@ti.com, linux-hwmon@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] hwmon: (ina3221) Clamp shunt resistor value from DT
Date: Mon, 8 Oct 2018 13:24:02 -0700 [thread overview]
Message-ID: <20181008202402.GA27003@roeck-us.net> (raw)
In-Reply-To: <20181008200537.22062-1-nicoleotsuka@gmail.com>
On Mon, Oct 08, 2018 at 01:05:37PM -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 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
> Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
> ---
> drivers/hwmon/ina3221.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c
> index c3a497aed345..ce8f0a8c4982 100644
> --- a/drivers/hwmon/ina3221.c
> +++ b/drivers/hwmon/ina3221.c
> @@ -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;
> }
> --
> 2.17.1
>
prev parent reply other threads:[~2018-10-08 20:24 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-08 20:05 [PATCH] hwmon: (ina3221) Clamp shunt resistor value from DT Nicolin Chen
2018-10-08 20:24 ` Guenter Roeck [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181008202402.GA27003@roeck-us.net \
--to=linux@roeck-us.net \
--cc=afd@ti.com \
--cc=jdelvare@suse.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nicoleotsuka@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox