[PATCH] hwmon: (tps23861) Cast unsigned temperature register value to signed

Murad Masimov posted 1 patch 1 year, 2 months ago
drivers/hwmon/tps23861.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] hwmon: (tps23861) Cast unsigned temperature register value to signed
Posted by Murad Masimov 1 year, 2 months ago
Since the temperature is supposed to be a signed value in this case, cast
unsigned raw register value to signed before performing the calculations.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: fff7b8ab2255 ("hwmon: add Texas Instruments TPS23861 driver")
Signed-off-by: Murad Masimov <m.masimov@maxima.ru>
---
 drivers/hwmon/tps23861.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hwmon/tps23861.c b/drivers/hwmon/tps23861.c
index dfcfb09d9f3c..80fb03f30c30 100644
--- a/drivers/hwmon/tps23861.c
+++ b/drivers/hwmon/tps23861.c
@@ -132,7 +132,7 @@ static int tps23861_read_temp(struct tps23861_data *data, long *val)
 	if (err < 0)
 		return err;

-	*val = (regval * TEMPERATURE_LSB) - 20000;
+	*val = ((long)regval * TEMPERATURE_LSB) - 20000;

 	return 0;
 }
--
2.39.2
Re: [PATCH] hwmon: (tps23861) Cast unsigned temperature register value to signed
Posted by Guenter Roeck 1 year, 2 months ago
On Thu, Nov 21, 2024 at 08:36:03PM +0300, Murad Masimov wrote:
> Since the temperature is supposed to be a signed value in this case, cast
> unsigned raw register value to signed before performing the calculations.
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> Fixes: fff7b8ab2255 ("hwmon: add Texas Instruments TPS23861 driver")
> Signed-off-by: Murad Masimov <m.masimov@maxima.ru>

The patch is correct, but both subject and description miss the point.

The problem is that negative values resulting from subtracting 20000
from an unsigned value are translated to large positive values because
there is no sign extension from unsigned int to long. So the patch fixes
a real issue, but both subject and description make it sound like a
formality.

No need to resend, I'll fix it up myself.

Guenter

> ---
>  drivers/hwmon/tps23861.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/hwmon/tps23861.c b/drivers/hwmon/tps23861.c
> index dfcfb09d9f3c..80fb03f30c30 100644
> --- a/drivers/hwmon/tps23861.c
> +++ b/drivers/hwmon/tps23861.c
> @@ -132,7 +132,7 @@ static int tps23861_read_temp(struct tps23861_data *data, long *val)
>  	if (err < 0)
>  		return err;
> 
> -	*val = (regval * TEMPERATURE_LSB) - 20000;
> +	*val = ((long)regval * TEMPERATURE_LSB) - 20000;
> 
>  	return 0;
>  }
> --
> 2.39.2
>