Skip to contentSkip to conversation

hwmon: prevent some divide by zeros in FAN_TO_REG()

PATCH Superseded Dan Carpenter 2 participants 5 messages Dec 5, 2013

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: prevent some divide by zeros in FAN_TO_REG()

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);
 }
drivers/hwmon/lm78.c +1 -1
index 6cf6bff79003..fc4578195674 100644
@@ -92,7 +92,7 @@ static inline u8 IN_TO_REG(unsigned long val)
 
 static inline u8 FAN_TO_REG(long rpm, int div)
 {
-	if (rpm <= 0)
+	if (rpm <= 0 || rpm * div = 0)
 		return 255;
 	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
 }
drivers/hwmon/sis5595.c +1 -1
index 1404e6319deb..811620fe63b4 100644
@@ -139,7 +139,7 @@ static inline u8 IN_TO_REG(unsigned long val)
 
 static inline u8 FAN_TO_REG(long rpm, int div)
 {
-	if (rpm <= 0)
+	if (rpm <= 0 || rpm * div = 0)
 		return 255;
 	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
 }
#1 lore raw

Hi Dan,

Quoted 2 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.

If you believe an overflow can happen (and indeed it can) then this isn't the way to handle it. Avoiding a divide by zero is certainly nice but properly handling the other overflow cases too would be better.

In practice, this means for the vt8231 driver:

	if (rpm = 0 || rpm > 1310720)
		return 0;

and for the lm78 and sis5595 drivers:

	if (rpm <= 0)
		return 255;
	if (rpm > 1350000)
		return 0;

That way you're certain to never overflow (the maximum value for div is 8), and insanely large values are handled properly instead of resulting in random register values.

Thanks,
Jean

Quoted 42 lines
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);
 }
drivers/hwmon/lm78.c +1 -1
index 6cf6bff79003..fc4578195674 100644
@@ -92,7 +92,7 @@ static inline u8 IN_TO_REG(unsigned long val)
 
 static inline u8 FAN_TO_REG(long rpm, int div)
 {
-	if (rpm <= 0)
+	if (rpm <= 0 || rpm * div = 0)
 		return 255;
 	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
 }
drivers/hwmon/sis5595.c +1 -1
index 1404e6319deb..811620fe63b4 100644
@@ -139,7 +139,7 @@ static inline u8 IN_TO_REG(unsigned long val)
 
 static inline u8 FAN_TO_REG(long rpm, int div)
 {
-	if (rpm <= 0)
+	if (rpm <= 0 || rpm * div = 0)
 		return 255;
 	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
 }
#2 lore raw
In reply to Dan Carpenter
Quoted 17 lines · Jean Delvare

Hi Dan,

Quoted 2 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.

If you believe an overflow can happen (and indeed it can) then this isn't the way to handle it. Avoiding a divide by zero is certainly nice but properly handling the other overflow cases too would be better.

In practice, this means for the vt8231 driver:

	if (rpm = 0 || rpm > 1310720)
		return 0;

and for the lm78 and sis5595 drivers:

Quoted 8 lines
	if (rpm <= 0)
		return 255;
	if (rpm > 1350000)
		return 0;

That way you're certain to never overflow (the maximum value for div is 8), and insanely large values are handled properly instead of resulting in random register values.

Yes. I will resend.

regards,
dan carpenter

#3 lore raw
In reply to Dan Carpenter
Quoted 15 lines · Jean Delvare

Hi Dan,

Quoted 2 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.

If you believe an overflow can happen (and indeed it can) then this isn't the way to handle it. Avoiding a divide by zero is certainly nice but properly handling the other overflow cases too would be better.

In practice, this means for the vt8231 driver:

	if (rpm = 0 || rpm > 1310720)
		return 0;
Quoted 6 lines

and for the lm78 and sis5595 drivers:

	if (rpm <= 0)
		return 255;
	if (rpm > 1350000)
		return 0;

For these two are you sure the return value shouldn't be 1?

regards,
dan carpenter

#4 lore raw
In reply to Dan Carpenter
Quoted 16 lines · Dan Carpenter
Quoted 15 lines · Jean Delvare

Hi Dan,

Quoted 2 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.

If you believe an overflow can happen (and indeed it can) then this isn't the way to handle it. Avoiding a divide by zero is certainly nice but properly handling the other overflow cases too would be better.

In practice, this means for the vt8231 driver:

	if (rpm = 0 || rpm > 1310720)
		return 0;
Quoted 8 lines
Quoted 6 lines

and for the lm78 and sis5595 drivers:

	if (rpm <= 0)
		return 255;
	if (rpm > 1350000)
		return 0;

For these two are you sure the return value shouldn't be 1?

Hmm, yes, you're right. That won't make a big difference in practice but returning 1 would be better.

Thanks,

Signature · 1 line
Jean Delvare