public inbox for linux-hwmon@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue
@ 2020-11-05 11:50 Colin King
  2020-11-05 12:32 ` Wilken Gottwalt
  2020-11-08  3:36 ` Guenter Roeck
  0 siblings, 2 replies; 6+ messages in thread
From: Colin King @ 2020-11-05 11:50 UTC (permalink / raw)
  To: Wilken Gottwalt, Jean Delvare, Guenter Roeck, linux-hwmon
  Cc: kernel-janitors, linux-kernel

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>
---
 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:
-- 
2.27.0


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

end of thread, other threads:[~2020-11-08  3:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-05 11:50 [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue Colin King
2020-11-05 12:32 ` Wilken Gottwalt
2020-11-05 12:44   ` Colin Ian King
2020-11-05 14:15   ` Guenter Roeck
2020-11-05 14:59     ` Wilken Gottwalt
2020-11-08  3:36 ` Guenter Roeck

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