All of lore.kernel.org
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Colin King <colin.king@canonical.com>
Cc: Wilken Gottwalt <wilken.gottwalt@posteo.net>,
	Jean Delvare <jdelvare@suse.com>,
	linux-hwmon@vger.kernel.org, kernel-janitors@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue
Date: Sat, 7 Nov 2020 19:36:50 -0800	[thread overview]
Message-ID: <20201108033650.GA133578@roeck-us.net> (raw)
In-Reply-To: <20201105115019.41735-1-colin.king@canonical.com>

On Thu, Nov 05, 2020 at 11:50:19AM +0000, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The shifting of the u8 integer data[3] by 24 bits to the left will
> be promoted to a 32 bit signed int and then sign-extended to a
> long. In the event that the top bit of data[3] is set then all
> then all the upper 32 bits of a 64 bit long end up as also being
> set because of the sign-extension. Fix this by casting data[3] to
> a long before the shift.
> 
> Addresses-Coverity: ("Unintended sign extension")
> Fixes: ce15cd2cee8b ("hwmon: add Corsair PSU HID controller driver")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

I wasn't sure if I am going to get another version of this patch.
Either case, I applied this now.

Guenter

> ---
>  drivers/hwmon/corsair-psu.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/hwmon/corsair-psu.c b/drivers/hwmon/corsair-psu.c
> index e92d0376e7ac..5d19a888231a 100644
> --- a/drivers/hwmon/corsair-psu.c
> +++ b/drivers/hwmon/corsair-psu.c
> @@ -241,7 +241,7 @@ static int corsairpsu_get_value(struct corsairpsu_data *priv, u8 cmd, u8 rail, l
>  	 * the LINEAR11 conversion are the watts values which are about 1200 for the strongest psu
>  	 * supported (HX1200i)
>  	 */
> -	tmp = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
> +	tmp = ((long)data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
>  	switch (cmd) {
>  	case PSU_CMD_IN_VOLTS:
>  	case PSU_CMD_IN_AMPS:

WARNING: multiple messages have this Message-ID (diff)
From: Guenter Roeck <linux@roeck-us.net>
To: Colin King <colin.king@canonical.com>
Cc: Wilken Gottwalt <wilken.gottwalt@posteo.net>,
	Jean Delvare <jdelvare@suse.com>,
	linux-hwmon@vger.kernel.org, kernel-janitors@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue
Date: Sun, 08 Nov 2020 03:36:50 +0000	[thread overview]
Message-ID: <20201108033650.GA133578@roeck-us.net> (raw)
In-Reply-To: <20201105115019.41735-1-colin.king@canonical.com>

On Thu, Nov 05, 2020 at 11:50:19AM +0000, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The shifting of the u8 integer data[3] by 24 bits to the left will
> be promoted to a 32 bit signed int and then sign-extended to a
> long. In the event that the top bit of data[3] is set then all
> then all the upper 32 bits of a 64 bit long end up as also being
> set because of the sign-extension. Fix this by casting data[3] to
> a long before the shift.
> 
> Addresses-Coverity: ("Unintended sign extension")
> Fixes: ce15cd2cee8b ("hwmon: add Corsair PSU HID controller driver")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

I wasn't sure if I am going to get another version of this patch.
Either case, I applied this now.

Guenter

> ---
>  drivers/hwmon/corsair-psu.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/hwmon/corsair-psu.c b/drivers/hwmon/corsair-psu.c
> index e92d0376e7ac..5d19a888231a 100644
> --- a/drivers/hwmon/corsair-psu.c
> +++ b/drivers/hwmon/corsair-psu.c
> @@ -241,7 +241,7 @@ static int corsairpsu_get_value(struct corsairpsu_data *priv, u8 cmd, u8 rail, l
>  	 * the LINEAR11 conversion are the watts values which are about 1200 for the strongest psu
>  	 * supported (HX1200i)
>  	 */
> -	tmp = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
> +	tmp = ((long)data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
>  	switch (cmd) {
>  	case PSU_CMD_IN_VOLTS:
>  	case PSU_CMD_IN_AMPS:

  parent reply	other threads:[~2020-11-08  3:36 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-05 11:50 [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue Colin King
2020-11-05 11:50 ` Colin King
2020-11-05 12:32 ` Wilken Gottwalt
2020-11-05 12:32   ` Wilken Gottwalt
2020-11-05 12:44   ` Colin Ian King
2020-11-05 12:44     ` Colin Ian King
2020-11-05 14:15   ` Guenter Roeck
2020-11-05 14:15     ` Guenter Roeck
2020-11-05 14:59     ` Wilken Gottwalt
2020-11-05 14:59       ` Wilken Gottwalt
2020-11-08  3:36 ` Guenter Roeck [this message]
2020-11-08  3:36   ` 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=20201108033650.GA133578@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=colin.king@canonical.com \
    --cc=jdelvare@suse.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=wilken.gottwalt@posteo.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.