Skip to contentSkip to conversation

hwmon: prevent some divide by zeros in FAN_TO_REG()

PATCH roger 1 participant 1 message Dec 5, 2013 Kernel janitors

lore
Share & tools
lore

roger

author
#0 lore raw
Quoted 19 lines · Dan Carpenter

It's not enough to just test if "rpm" is zero, the "rpm * div" operation
could overflow and that could also lead to a divide by zero.

drivers/hwmon/vt8231.c +1 -1
index 0e7017841f7d..923c18034a5f 100644
@@ -145,7 +145,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 * div = 0)
 		return 0;
 	return clamp_val(1310720 / (rpm * div), 1, 255);
Quoted 1 line

}

Seems a reasonable improvement to me, so ACK'd.

Quoted 2 lines
drivers/hwmon/lm78.c +0 -0
index 6cf6bff79003..fc4578195674 100644

SNIP

drivers/hwmon/lm78.c

Quoted 3 lines
drivers/hwmon/lm78.c +0 -0
 		return 255;
 	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
 }

- Roger