Skip to content

Commit

Permalink
hwmon: (adc128d818) Fix underflows seen when writing limit attributes
Browse files Browse the repository at this point in the history
[ Upstream commit 8cad724 ]

DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large
negative number such as -9223372036854775808 is provided by the user.
Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.

Signed-off-by: Guenter Roeck <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
groeck authored and gregkh committed Sep 12, 2024
1 parent 3a986d1 commit 6891b11
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions drivers/hwmon/adc128d818.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ static ssize_t adc128_in_store(struct device *dev,

mutex_lock(&data->update_lock);
/* 10 mV LSB on limit registers */
regval = clamp_val(DIV_ROUND_CLOSEST(val, 10), 0, 255);
regval = DIV_ROUND_CLOSEST(clamp_val(val, 0, 2550), 10);
data->in[index][nr] = regval << 4;
reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr);
i2c_smbus_write_byte_data(data->client, reg, regval);
Expand Down Expand Up @@ -214,7 +214,7 @@ static ssize_t adc128_temp_store(struct device *dev,
return err;

mutex_lock(&data->update_lock);
regval = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
regval = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
data->temp[index] = regval << 1;
i2c_smbus_write_byte_data(data->client,
index == 1 ? ADC128_REG_TEMP_MAX
Expand Down

0 comments on commit 6891b11

Please sign in to comment.