public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Quentin Schulz <quentin.schulz@cherry.de>
To: Guenter Roeck <linux@roeck-us.net>, linux-hwmon@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Farouk Bouabid <farouk.bouabid@cherry.de>
Subject: Re: [PATCH 01/10] hwmon: (amc6821) Stop accepting invalid pwm values
Date: Mon, 1 Jul 2024 12:19:30 +0200	[thread overview]
Message-ID: <8fdb07b6-ef80-4faa-80cf-ea16839e9e98@cherry.de> (raw)
In-Reply-To: <20240628151346.1152838-2-linux@roeck-us.net>

Hi Guenter,

On 6/28/24 5:13 PM, Guenter Roeck wrote:
> The pwm value range is well defined from 0..255. Don't accept
> any values outside this range.
> 
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
>   drivers/hwmon/amc6821.c | 14 ++++++++++----
>   1 file changed, 10 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/hwmon/amc6821.c b/drivers/hwmon/amc6821.c
> index 9b02b304c2f5..3c614a0bd192 100644
> --- a/drivers/hwmon/amc6821.c
> +++ b/drivers/hwmon/amc6821.c
> @@ -360,8 +360,11 @@ static ssize_t pwm1_store(struct device *dev,
>   	if (ret)
>   		return ret;
>   
> +	if (val < 0 || val > 255)
> +		return -EINVAL;
> +

Why not use kstrtoul to avoid having to check for negative val? The same 
way that is done just below in this patch?

Additionally, why not using kstrtou8 so we don't have to do this check 
ourselves in the driver?

>   	mutex_lock(&data->update_lock);
> -	data->pwm1 = clamp_val(val , 0, 255);
> +	data->pwm1 = val;
>   	i2c_smbus_write_byte_data(client, AMC6821_REG_DCY, data->pwm1);
>   	mutex_unlock(&data->update_lock);
>   	return count;
> @@ -558,13 +561,16 @@ static ssize_t pwm1_auto_point_pwm_store(struct device *dev,
>   	struct amc6821_data *data = dev_get_drvdata(dev);
>   	struct i2c_client *client = data->client;
>   	int dpwm;
> -	long val;
> -	int ret = kstrtol(buf, 10, &val);
> +	unsigned long val;
> +	int ret = kstrtoul(buf, 10, &val);

Same remark concerning kstrtou8 use.

>   	if (ret)
>   		return ret;
>   
> +	if (val > 255)
> +		return -EINVAL;
> +
>   	mutex_lock(&data->update_lock);
> -	data->pwm1_auto_point_pwm[1] = clamp_val(val, 0, 254);

We're suddenly allowing 255 as a valid value.

I don't see 255 triggering an obvious divide-by-0 issue in the code, nor 
any limitation from a quick look at the datasheet. 254 was introduced in 
the introducing commit, as well as the other 255... so probably an 
oversight by the original author? In any case, I would make this a 
separate commit or at the very least make this explicit in the commit 
log to show this isn't an oversight **right now** and that this change 
was desired.

Cheers,
Quentin

  reply	other threads:[~2024-07-01 10:19 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-28 15:13 [PATCH 00/10] hwmon: (amc6821) Various improvements Guenter Roeck
2024-06-28 15:13 ` [PATCH 01/10] hwmon: (amc6821) Stop accepting invalid pwm values Guenter Roeck
2024-07-01 10:19   ` Quentin Schulz [this message]
2024-07-01 13:50     ` Guenter Roeck
2024-06-28 15:13 ` [PATCH 02/10] hwmon: (amc6821) Make reading and writing fan speed limits consistent Guenter Roeck
2024-07-01 11:05   ` Quentin Schulz
2024-07-01 14:11     ` Guenter Roeck
2024-07-01 14:37       ` Guenter Roeck
2024-07-01 16:13         ` Quentin Schulz
2024-07-01 17:21           ` Guenter Roeck
2024-07-01 18:05             ` Quentin Schulz
2024-07-01 19:11               ` Guenter Roeck
2024-06-28 15:13 ` [PATCH 03/10] hwmon: (amc6821) Rename fan1_div to fan1_pulses Guenter Roeck
2024-07-01 11:08   ` Quentin Schulz
2024-06-28 15:13 ` [PATCH 04/10] hwmon: (amc6821) Add support for fan1_target and pwm1_enable mode 4 Guenter Roeck
2024-07-01 11:23   ` Quentin Schulz
2024-07-01 15:26     ` Guenter Roeck
2024-07-01 16:29       ` Quentin Schulz
2024-07-01 17:31         ` Guenter Roeck
2024-06-28 15:13 ` [PATCH 05/10] hwmon: (amc2821) Reorder include files, drop unnecessary ones Guenter Roeck
2024-07-01 11:24   ` Quentin Schulz
2024-06-28 15:13 ` [PATCH 06/10] hwmon: (amc6821) Use tabs for column alignment in defines Guenter Roeck
2024-07-01 11:26   ` Quentin Schulz
2024-06-28 15:13 ` [PATCH 07/10] hwmon: (amc2821) Use BIT() and GENMASK() Guenter Roeck
2024-07-01 11:31   ` Quentin Schulz
2024-07-01 14:44     ` Guenter Roeck
2024-06-28 15:13 ` [PATCH 08/10] hwmon: (amc6821) Drop unnecessary enum chips Guenter Roeck
2024-07-01 11:36   ` Quentin Schulz
2024-07-01 14:47     ` Guenter Roeck
2024-06-28 15:13 ` [PATCH 09/10] hwmon: (amc6821) Convert to use regmap Guenter Roeck
2024-07-01 13:01   ` Quentin Schulz
2024-07-01 13:47     ` Guenter Roeck
2024-07-01 16:54       ` Quentin Schulz
2024-07-01 17:30         ` Guenter Roeck
2024-06-28 15:13 ` [PATCH 10/10] hwmon: (amc6821) Convert to with_info API Guenter Roeck
2024-07-01 17:46   ` Quentin Schulz
2024-07-01 18:24     ` Guenter Roeck

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=8fdb07b6-ef80-4faa-80cf-ea16839e9e98@cherry.de \
    --to=quentin.schulz@cherry.de \
    --cc=farouk.bouabid@cherry.de \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /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