提交 0fb620c4 编写于 作者: G Guenter Roeck

hwmon: (adm9240) Fix overflows seen when writing into limit attributes

Module test reports:

in0_min: Suspected overflow: [3320 vs. 0]
in0_max: Suspected overflow: [3320 vs. 0]
in4_min: Suspected overflow: [15938 vs. 0]
in4_max: Suspected overflow: [15938 vs. 0]
temp1_max: Suspected overflow: [127000 vs. 0]
temp1_max_hyst: Suspected overflow: [127000 vs. 0]
aout_output: Suspected overflow: [1250 vs. 0]

Code analysis reveals that the overflows are caused by conversions
from unsigned long to long to int, combined with multiplications on
passed values.
Reviewed-by: NJean Delvare <jdelvare@suse.de>
Signed-off-by: NGuenter Roeck <linux@roeck-us.net>
上级 1b109c49
...@@ -98,13 +98,15 @@ static inline unsigned int IN_FROM_REG(u8 reg, int n) ...@@ -98,13 +98,15 @@ static inline unsigned int IN_FROM_REG(u8 reg, int n)
static inline u8 IN_TO_REG(unsigned long val, int n) static inline u8 IN_TO_REG(unsigned long val, int n)
{ {
return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255); val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
return SCALE(val, 192, nom_mv[n]);
} }
/* temperature range: -40..125, 127 disables temperature alarm */ /* temperature range: -40..125, 127 disables temperature alarm */
static inline s8 TEMP_TO_REG(long val) static inline s8 TEMP_TO_REG(long val)
{ {
return clamp_val(SCALE(val, 1, 1000), -40, 127); val = clamp_val(val, -40000, 127000);
return SCALE(val, 1, 1000);
} }
/* two fans, each with low fan speed limit */ /* two fans, each with low fan speed limit */
...@@ -122,7 +124,8 @@ static inline unsigned int FAN_FROM_REG(u8 reg, u8 div) ...@@ -122,7 +124,8 @@ static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
/* analog out 0..1250mV */ /* analog out 0..1250mV */
static inline u8 AOUT_TO_REG(unsigned long val) static inline u8 AOUT_TO_REG(unsigned long val)
{ {
return clamp_val(SCALE(val, 255, 1250), 0, 255); val = clamp_val(val, 0, 1250);
return SCALE(val, 255, 1250);
} }
static inline unsigned int AOUT_FROM_REG(u8 reg) static inline unsigned int AOUT_FROM_REG(u8 reg)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册