diff options
author | Guenter Roeck <linux@roeck-us.net> | 2016-11-20 13:14:09 -0500 |
---|---|---|
committer | Guenter Roeck <linux@roeck-us.net> | 2016-12-09 09:09:23 -0500 |
commit | 0fb620c4334eab14e85b7f66389e9061a225fb7e (patch) | |
tree | 4e7434d74b559ff6055fc1c0b1442d4d097137e3 | |
parent | 1b109c49b72dc3cb8392bbc22bad662f71b8fd80 (diff) |
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: Jean Delvare <jdelvare@suse.de>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
-rw-r--r-- | drivers/hwmon/adm9240.c | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/drivers/hwmon/adm9240.c b/drivers/hwmon/adm9240.c index 2fe1828bd10b..72bf2489511e 100644 --- a/drivers/hwmon/adm9240.c +++ b/drivers/hwmon/adm9240.c | |||
@@ -98,13 +98,15 @@ static inline unsigned int IN_FROM_REG(u8 reg, int n) | |||
98 | 98 | ||
99 | static inline u8 IN_TO_REG(unsigned long val, int n) | 99 | static inline u8 IN_TO_REG(unsigned long val, int n) |
100 | { | 100 | { |
101 | return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255); | 101 | val = clamp_val(val, 0, nom_mv[n] * 255 / 192); |
102 | return SCALE(val, 192, nom_mv[n]); | ||
102 | } | 103 | } |
103 | 104 | ||
104 | /* temperature range: -40..125, 127 disables temperature alarm */ | 105 | /* temperature range: -40..125, 127 disables temperature alarm */ |
105 | static inline s8 TEMP_TO_REG(long val) | 106 | static inline s8 TEMP_TO_REG(long val) |
106 | { | 107 | { |
107 | return clamp_val(SCALE(val, 1, 1000), -40, 127); | 108 | val = clamp_val(val, -40000, 127000); |
109 | return SCALE(val, 1, 1000); | ||
108 | } | 110 | } |
109 | 111 | ||
110 | /* two fans, each with low fan speed limit */ | 112 | /* two fans, each with low fan speed limit */ |
@@ -122,7 +124,8 @@ static inline unsigned int FAN_FROM_REG(u8 reg, u8 div) | |||
122 | /* analog out 0..1250mV */ | 124 | /* analog out 0..1250mV */ |
123 | static inline u8 AOUT_TO_REG(unsigned long val) | 125 | static inline u8 AOUT_TO_REG(unsigned long val) |
124 | { | 126 | { |
125 | return clamp_val(SCALE(val, 255, 1250), 0, 255); | 127 | val = clamp_val(val, 0, 1250); |
128 | return SCALE(val, 255, 1250); | ||
126 | } | 129 | } |
127 | 130 | ||
128 | static inline unsigned int AOUT_FROM_REG(u8 reg) | 131 | static inline unsigned int AOUT_FROM_REG(u8 reg) |