Commit 57591712 authored by Dan Carpenter's avatar Dan Carpenter Committed by Greg Kroah-Hartman

hwmon: Prevent some divide by zeros in FAN_TO_REG()

commit 3806b45b upstream.

The "rpm * div" operations can overflow here, so this patch adds an
upper limit to rpm to prevent that.  Jean Delvare helped me with this
patch.
Signed-off-by: default avatarDan Carpenter <dan.carpenter@oracle.com>
Acked-by: default avatarRoger Lucas <vt8231@hiddenengine.co.uk>
Signed-off-by: default avatarJean Delvare <khali@linux-fr.org>
[bwh: Backported to 3.2: adjust context]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
Cc: Qiang Huang <h.huangqiang@huawei.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent e4b2585a
...@@ -94,6 +94,8 @@ static inline u8 FAN_TO_REG(long rpm, int div) ...@@ -94,6 +94,8 @@ static inline u8 FAN_TO_REG(long rpm, int div)
{ {
if (rpm <= 0) if (rpm <= 0)
return 255; return 255;
if (rpm > 1350000)
return 1;
return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254); return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
} }
......
...@@ -141,6 +141,8 @@ static inline u8 FAN_TO_REG(long rpm, int div) ...@@ -141,6 +141,8 @@ static inline u8 FAN_TO_REG(long rpm, int div)
{ {
if (rpm <= 0) if (rpm <= 0)
return 255; return 255;
if (rpm > 1350000)
return 1;
return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254); return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
} }
......
...@@ -145,7 +145,7 @@ static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 }; ...@@ -145,7 +145,7 @@ static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
*/ */
static inline u8 FAN_TO_REG(long rpm, int div) static inline u8 FAN_TO_REG(long rpm, int div)
{ {
if (rpm == 0) if (rpm <= 0 || rpm > 1310720)
return 0; return 0;
return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255); return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment