Skip to contentSkip to conversation

hwmon/dme1737: fix divide-by-0

PATCH In review Juerg Haefliger 3 participants 9 messages Jan 24, 2008 – Feb 1, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon/dme1737: fix divide-by-0

This patch fixes a possible divide-by-0.

Signed-off-by Juerg Haefliger <juergh at gmail.com>
drivers/hwmon/dme1737.c +2 -2
Index: linux-2.6.24-rc8/drivers/hwmon/dme1737.c
===================================================================
@@ -285,8 +285,8 @@
 
 static inline int FAN_TO_REG(int val, int tpc)
 {
-	return SENSORS_LIMIT((tpc == 0) ? 90000 * 60 / val : val / tpc,
-			     0, 0xffff);
+	return (val <= 0) ? 0xffff : SENSORS_LIMIT((tpc == 0) ?
+		90000 * 60 / val : val / tpc, 0, 0xfffe);
 }
 
 /* Fan TPC (tach pulse count)

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#1 lore raw

Hi Juerg,

Quoted 8 lines · Juerg Haefliger

This patch fixes a possible divide-by-0.

Signed-off-by Juerg Haefliger <juergh at gmail.com>

Index: linux-2.6.24-rc8/drivers/hwmon/dme1737.c
=================================> --- linux-2.6.24-rc8.orig/drivers/hwmon/dme1737.c 2008-01-24 09:04:24.000000000 -0800
+++ linux-2.6.24-rc8/drivers/hwmon/dme1737.c 2008-01-24 09:05:05.000000000 -0800

drivers/hwmon/dme1737.c line 292

Quoted 10 lines
drivers/hwmon/dme1737.c +2 -2
@@ -285,8 +285,8 @@
 
 static inline int FAN_TO_REG(int val, int tpc)
 {
-	return SENSORS_LIMIT((tpc = 0) ? 90000 * 60 / val : val / tpc,
-			     0, 0xffff);
+	return (val <= 0) ? 0xffff : SENSORS_LIMIT((tpc = 0) ?
+		90000 * 60 / val : val / tpc, 0, 0xfffe);
 }
 
 /* Fan TPC (tach pulse count)

While I agree that it fixes the division by 0, and the fix looks OK in legacy mode, I am not sure that it is correct in TPC mode. While register value 0xffff corresponds to a stalled fan in legacy mode, that's not the case in TPC mode according to the datasheet. In TPC mode, a stalled fan leads to a register value of 0x0000. So I believe that the driver should write 0x0000, not 0xffff, to the register if the user sets the limit to 0 RPM in TPC mode. This would lead to the following code (untested):

	if (tpc)
		return SENSORS_LIMIT(val / tpc, 0, 0xffff);
	else 
		return (val <= 0) ? 0xffff :
		       SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);

What do you think?

(I wish manufacturers could come up with more simple chips...)

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#2 lore raw
In reply to Juerg Haefliger

Hi jean,

Quoted 31 lines · Jean Delvare

Hi Juerg,

Quoted 8 lines · Juerg Haefliger

This patch fixes a possible divide-by-0.

Signed-off-by Juerg Haefliger <juergh at gmail.com>

Index: linux-2.6.24-rc8/drivers/hwmon/dme1737.c
=================================> > --- linux-2.6.24-rc8.orig/drivers/hwmon/dme1737.c 2008-01-24 09:04:24.000000000 -0800
+++ linux-2.6.24-rc8/drivers/hwmon/dme1737.c 2008-01-24 09:05:05.000000000 -0800

drivers/hwmon/dme1737.c line 292

Quoted 10 lines
drivers/hwmon/dme1737.c +2 -2
@@ -285,8 +285,8 @@
 
 static inline int FAN_TO_REG(int val, int tpc)
 {
-     return SENSORS_LIMIT((tpc = 0) ? 90000 * 60 / val : val / tpc,
-                          0, 0xffff);
+     return (val <= 0) ? 0xffff : SENSORS_LIMIT((tpc = 0) ?
+             90000 * 60 / val : val / tpc, 0, 0xfffe);
 }
 
 /* Fan TPC (tach pulse count)

While I agree that it fixes the division by 0, and the fix looks OK in legacy mode, I am not sure that it is correct in TPC mode. While register value 0xffff corresponds to a stalled fan in legacy mode, that's not the case in TPC mode according to the datasheet. In TPC mode, a stalled fan leads to a register value of 0x0000. So I believe that the driver should write 0x0000, not 0xffff, to the register if the user sets the limit to 0 RPM in TPC mode. This would lead to the following code (untested):

Quoted 7 lines
        if (tpc)
                return SENSORS_LIMIT(val / tpc, 0, 0xffff);
        else
                return (val <= 0) ? 0xffff :
                       SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);

What do you think?

Good catch, and this also applies for the conversion in the other direction. Updated patch will follow...

...juerg

Quoted 5 lines

(I wish manufacturers could come up with more simple chips...)

--
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#3 lore raw
In reply to Juerg Haefliger

This patch fixes a possible divide-by-0 and a minor bug in the FAN_FROM_REG macro (in TPC mode).

Signed-off-by Juerg Haefliger <juergh at gmail.com>
drivers/hwmon/dme1737.c +11 -4
Index: linux/drivers/hwmon/dme1737.c
===================================================================
@@ -279,14 +279,21 @@
 /* Fan input RPM */
 static inline int FAN_FROM_REG(int reg, int tpc)
 {
-	return (reg == 0 || reg == 0xffff) ? 0 :
-		(tpc == 0) ? 90000 * 60 / reg : tpc * reg;
+	if (tpc) {
+		return tpc * reg;
+	} else {
+		return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg;
+	}
 }
 
 static inline int FAN_TO_REG(int val, int tpc)
 {
-	return SENSORS_LIMIT((tpc == 0) ? 90000 * 60 / val : val / tpc,
-			     0, 0xffff);
+	if (tpc) {
+		return SENSORS_LIMIT(val / tpc, 0, 0xffff);
+	} else {
+		return (val <= 0) ? 0xffff :
+			SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
+	}
 }
 
 /* Fan TPC (tach pulse count)

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

Acked-by on v1 1/1
#4 lore raw
In reply to Juerg Haefliger

Hi Juerg,

Quoted 24 lines · Juerg Haefliger

This patch fixes a possible divide-by-0 and a minor bug in the FAN_FROM_REG macro (in TPC mode).

Signed-off-by Juerg Haefliger <juergh at gmail.com>

Index: linux/drivers/hwmon/dme1737.c
=================================> --- linux.orig/drivers/hwmon/dme1737.c 2008-01-24 09:04:24.000000000 -0800
+++ linux/drivers/hwmon/dme1737.c 2008-01-27 16:33:47.000000000 -0800

(quoted diff) +5 -3
@@ -279,14 +279,21 @@
 /* Fan input RPM */
 static inline int FAN_FROM_REG(int reg, int tpc)
 {
-	return (reg = 0 || reg = 0xffff) ? 0 :
-		(tpc = 0) ? 90000 * 60 / reg : tpc * reg;
+	if (tpc) {
+		return tpc * reg;
+	} else {
+		return (reg = 0 || reg = 0xffff) ? 0 : 90000 * 60 / reg;
+	}
 }
 
 static inline int FAN_TO_REG(int val, int tpc)
 {
-	return SENSORS_LIMIT((tpc = 0) ? 90000 * 60 / val : val / tpc,

Review line 299

Quoted 10 lines
(quoted diff) +6 -1
-			     0, 0xffff);
+	if (tpc) {
+		return SENSORS_LIMIT(val / tpc, 0, 0xffff);
+	} else {
+		return (val <= 0) ? 0xffff :
+			SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
+	}
 }
 
 /* Fan TPC (tach pulse count)

Looks good to me this time.

PS: Would you have some time to review one of my pending patches? For example:
http://lists.lm-sensors.org/pipermail/lm-sensors/2007-December/022143.html

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#5 lore raw
In reply to Juerg Haefliger

Hi Jean,

Quoted 37 lines · Jean Delvare

Hi Juerg,

Quoted 33 lines · Juerg Haefliger

This patch fixes a possible divide-by-0 and a minor bug in the FAN_FROM_REG macro (in TPC mode).

Signed-off-by Juerg Haefliger <juergh at gmail.com>

Index: linux/drivers/hwmon/dme1737.c
=================================> > --- linux.orig/drivers/hwmon/dme1737.c 2008-01-24 09:04:24.000000000 -0800
+++ linux/drivers/hwmon/dme1737.c 2008-01-27 16:33:47.000000000 -0800

(quoted diff) +11 -4
@@ -279,14 +279,21 @@
 /* Fan input RPM */
 static inline int FAN_FROM_REG(int reg, int tpc)
 {
-     return (reg = 0 || reg = 0xffff) ? 0 :
-             (tpc = 0) ? 90000 * 60 / reg : tpc * reg;
+     if (tpc) {
+             return tpc * reg;
+     } else {
+             return (reg = 0 || reg = 0xffff) ? 0 : 90000 * 60 / reg;
+     }
 }
 
 static inline int FAN_TO_REG(int val, int tpc)
 {
-     return SENSORS_LIMIT((tpc = 0) ? 90000 * 60 / val : val / tpc,
-                          0, 0xffff);
+     if (tpc) {
+             return SENSORS_LIMIT(val / tpc, 0, 0xffff);
+     } else {
+             return (val <= 0) ? 0xffff :
+                     SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
+     }
 }
 
Quoted 5 lines

drivers/hwmon/dme1737.c line 292

Quoted 1 line
drivers/hwmon/dme1737.c +0 -0
 /* Fan TPC (tach pulse count)

Looks good to me this time.

Thanks for the review.

Quoted 3 lines

PS: Would you have some time to review one of my pending patches? For example:
http://lists.lm-sensors.org/pipermail/lm-sensors/2007-December/022143.html

Riku just beat me to it:
http://lists.lm-sensors.org/pipermail/lm-sensors/2008-January/022362.html

Anything else you need reviewed?

...juerg

Quoted 3 lines

--
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#6 lore raw
In reply to Juerg Haefliger

Hi Juerg,

Quoted 9 lines · Juerg Haefliger
Quoted 3 lines · Jean Delvare

PS: Would you have some time to review one of my pending patches? For example:
http://lists.lm-sensors.org/pipermail/lm-sensors/2007-December/022143.html

Riku just beat me to it:
http://lists.lm-sensors.org/pipermail/lm-sensors/2008-January/022362.html

Anything else you need reviewed?

http://lists.lm-sensors.org/pipermail/lm-sensors/2007-December/022067.html

or

http://lists.lm-sensors.org/pipermail/lm-sensors/2008-January/022252.html

Thanks,
--
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#7 lore raw
In reply to Juerg Haefliger

Hi Juerg, Jean:

Quoted 36 lines · Jean Delvare

Hi Juerg,

Quoted 33 lines · Juerg Haefliger

This patch fixes a possible divide-by-0 and a minor bug in the FAN_FROM_REG macro (in TPC mode).

Signed-off-by Juerg Haefliger <juergh at gmail.com>

Index: linux/drivers/hwmon/dme1737.c
=================================> > --- linux.orig/drivers/hwmon/dme1737.c 2008-01-24 09:04:24.000000000 -0800
+++ linux/drivers/hwmon/dme1737.c 2008-01-27 16:33:47.000000000 -0800

(quoted diff) +11 -4
@@ -279,14 +279,21 @@
 /* Fan input RPM */
 static inline int FAN_FROM_REG(int reg, int tpc)
 {
-	return (reg = 0 || reg = 0xffff) ? 0 :
-		(tpc = 0) ? 90000 * 60 / reg : tpc * reg;
+	if (tpc) {
+		return tpc * reg;
+	} else {
+		return (reg = 0 || reg = 0xffff) ? 0 : 90000 * 60 / reg;
+	}
 }
 
 static inline int FAN_TO_REG(int val, int tpc)
 {
-	return SENSORS_LIMIT((tpc = 0) ? 90000 * 60 / val : val / tpc,
-			     0, 0xffff);
+	if (tpc) {
+		return SENSORS_LIMIT(val / tpc, 0, 0xffff);
+	} else {
+		return (val <= 0) ? 0xffff :
+			SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
+	}
 }
 
Quoted 5 lines

drivers/hwmon/dme1737.c line 292

Quoted 1 line
drivers/hwmon/dme1737.c +0 -0
 /* Fan TPC (tach pulse count)

Looks good to me this time.

Applied to hwmon-2.6.git/testing, thanks.

Signature · 8 lines
Mark M. Hoffman
[email protected]


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#8 lore raw
In reply to Juerg Haefliger

Hi Mark Jean,

Quoted 40 lines · Mark M. Hoffman

Hi Juerg, Jean:

Quoted 36 lines · Jean Delvare

Hi Juerg,

Quoted 33 lines · Juerg Haefliger

This patch fixes a possible divide-by-0 and a minor bug in the FAN_FROM_REG macro (in TPC mode).

Signed-off-by Juerg Haefliger <juergh at gmail.com>

Index: linux/drivers/hwmon/dme1737.c
=================================> > > --- linux.orig/drivers/hwmon/dme1737.c 2008-01-24 09:04:24.000000000 -0800
+++ linux/drivers/hwmon/dme1737.c 2008-01-27 16:33:47.000000000 -0800

(quoted diff) +11 -4
@@ -279,14 +279,21 @@
 /* Fan input RPM */
 static inline int FAN_FROM_REG(int reg, int tpc)
 {
-   return (reg = 0 || reg = 0xffff) ? 0 :
-           (tpc = 0) ? 90000 * 60 / reg : tpc * reg;
+   if (tpc) {
+           return tpc * reg;
+   } else {
+           return (reg = 0 || reg = 0xffff) ? 0 : 90000 * 60 / reg;
+   }
 }
 
 static inline int FAN_TO_REG(int val, int tpc)
 {
-   return SENSORS_LIMIT((tpc = 0) ? 90000 * 60 / val : val / tpc,
-                        0, 0xffff);
+   if (tpc) {
+           return SENSORS_LIMIT(val / tpc, 0, 0xffff);
+   } else {
+           return (val <= 0) ? 0xffff :
+                   SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
+   }
 }
 
Quoted 7 lines
Quoted 5 lines

drivers/hwmon/dme1737.c line 292

Quoted 1 line
drivers/hwmon/dme1737.c +0 -0
 /* Fan TPC (tach pulse count)

Looks good to me this time.

Applied to hwmon-2.6.git/testing, thanks.

Jeez, I'm definitely too slow :-) Mark, do you keep a list of patches awaiting review?

...juerg

Quoted 5 lines

--
Mark M. Hoffman
[email protected]

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors