hwmon: Prevent some divide by zeros in FAN_TO_REG()
authorDan Carpenter <dan.carpenter@oracle.com>
Thu, 12 Dec 2013 07:05:33 +0000 (08:05 +0100)
committerBen Hutchings <ben@decadent.org.uk>
Fri, 3 Jan 2014 04:33:30 +0000 (04:33 +0000)
commit 3806b45ba4655147a011df03242cc197ab986c43 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: Dan Carpenter <dan.carpenter@oracle.com>
Acked-by: Roger Lucas <vt8231@hiddenengine.co.uk>
Signed-off-by: Jean Delvare <khali@linux-fr.org>
[bwh: Backported to 3.2: adjust context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
drivers/hwmon/lm78.c
drivers/hwmon/sis5595.c
drivers/hwmon/vt8231.c

index 6df0b46..a42a7b0 100644 (file)
@@ -90,6 +90,8 @@ static inline u8 FAN_TO_REG(long rpm, int div)
 {
        if (rpm <= 0)
                return 255;
+       if (rpm > 1350000)
+               return 1;
        return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
 }
 
index 47d7ce9..5ab6953 100644 (file)
@@ -133,6 +133,8 @@ static inline u8 FAN_TO_REG(long rpm, int div)
 {
        if (rpm <= 0)
                return 255;
+       if (rpm > 1350000)
+               return 1;
        return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
 }
 
index db3b2e8..6df67a9 100644 (file)
@@ -139,7 +139,7 @@ static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
 */
 static inline u8 FAN_TO_REG(long rpm, int div)
 {
-       if (rpm == 0)
+       if (rpm <= 0 || rpm > 1310720)
                return 0;
        return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
 }