diff mbox series

[2/5] hwmon: (lm90) Prevent integer overflow/underflow in hysteresis calculations

Message ID 20211210200136.1662127-3-linux@roeck-us.net (mailing list archive)
State Accepted
Headers show
Series hwmon: (lm90) Various fixes | expand

Commit Message

Guenter Roeck Dec. 10, 2021, 8:01 p.m. UTC
Commit b50aa49638c7 ("hwmon: (lm90) Prevent integer underflows of
temperature calculations") addressed a number of underflow situations
when writing temperature limits. However, it missed one situation, seen
when an attempt is made to set the hysteresis value to MAX_LONG and the
critical temperature limit is negative.

Use clamp_val() when setting the hysteresis temperature to ensure that
the provided value can never overflow or underflow.

Fixes: b50aa49638c7 ("hwmon: (lm90) Prevent integer underflows of temperature calculations")
Cc: Dmitry Osipenko <digetx@gmail.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/lm90.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Dmitry Osipenko Dec. 10, 2021, 9:08 p.m. UTC | #1
10.12.2021 23:01, Guenter Roeck пишет:
> Commit b50aa49638c7 ("hwmon: (lm90) Prevent integer underflows of
> temperature calculations") addressed a number of underflow situations
> when writing temperature limits. However, it missed one situation, seen
> when an attempt is made to set the hysteresis value to MAX_LONG and the
> critical temperature limit is negative.
> 
> Use clamp_val() when setting the hysteresis temperature to ensure that
> the provided value can never overflow or underflow.
> 
> Fixes: b50aa49638c7 ("hwmon: (lm90) Prevent integer underflows of temperature calculations")
> Cc: Dmitry Osipenko <digetx@gmail.com>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
>  drivers/hwmon/lm90.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/hwmon/lm90.c b/drivers/hwmon/lm90.c
> index b05d73c4fbe2..72969ea83d82 100644
> --- a/drivers/hwmon/lm90.c
> +++ b/drivers/hwmon/lm90.c
> @@ -1160,8 +1160,8 @@ static int lm90_set_temphyst(struct lm90_data *data, long val)
>  	else
>  		temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
>  
> -	/* prevent integer underflow */
> -	val = max(val, -128000l);
> +	/* prevent integer overflow/underflow */
> +	val = clamp_val(val, -128000l, 255000l);
>  
>  	data->temp_hyst = hyst_to_reg(temp - val);
>  	err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
> 

Reviewed-by: Dmitry Osipenko <digetx@gmail.com>
diff mbox series

Patch

diff --git a/drivers/hwmon/lm90.c b/drivers/hwmon/lm90.c
index b05d73c4fbe2..72969ea83d82 100644
--- a/drivers/hwmon/lm90.c
+++ b/drivers/hwmon/lm90.c
@@ -1160,8 +1160,8 @@  static int lm90_set_temphyst(struct lm90_data *data, long val)
 	else
 		temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
 
-	/* prevent integer underflow */
-	val = max(val, -128000l);
+	/* prevent integer overflow/underflow */
+	val = clamp_val(val, -128000l, 255000l);
 
 	data->temp_hyst = hyst_to_reg(temp - val);
 	err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,