All of lore.kernel.org
 help / color / mirror / Atom feed
* [patch] hwmon: prevent some divide by zeros in FAN_TO_REG()
@ 2013-12-05 10:58 ` Dan Carpenter
  0 siblings, 0 replies; 11+ messages in thread
From: Dan Carpenter @ 2013-12-05 10:58 UTC (permalink / raw)
  To: kernel-janitors

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.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

diff --git a/drivers/hwmon/vt8231.c b/drivers/hwmon/vt8231.c
index 0e7017841f7d..923c18034a5f 100644
--- a/drivers/hwmon/vt8231.c
+++ b/drivers/hwmon/vt8231.c
@@ -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);
 }
diff --git a/drivers/hwmon/lm78.c b/drivers/hwmon/lm78.c
index 6cf6bff79003..fc4578195674 100644
--- a/drivers/hwmon/lm78.c
+++ b/drivers/hwmon/lm78.c
@@ -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);
 }
diff --git a/drivers/hwmon/sis5595.c b/drivers/hwmon/sis5595.c
index 1404e6319deb..811620fe63b4 100644
--- a/drivers/hwmon/sis5595.c
+++ b/drivers/hwmon/sis5595.c
@@ -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);
 }

^ permalink raw reply related	[flat|nested] 11+ messages in thread
* Re: [patch] hwmon: prevent some divide by zeros in FAN_TO_REG()
@ 2013-12-05 11:07 roger
  0 siblings, 0 replies; 11+ messages in thread
From: roger @ 2013-12-05 11:07 UTC (permalink / raw)
  To: kernel-janitors

On , Dan Carpenter wrote:
> 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.
> 
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> 
> diff --git a/drivers/hwmon/vt8231.c b/drivers/hwmon/vt8231.c
> index 0e7017841f7d..923c18034a5f 100644
> --- a/drivers/hwmon/vt8231.c
> +++ b/drivers/hwmon/vt8231.c
> @@ -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);
>  }

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

> diff --git a/drivers/hwmon/lm78.c b/drivers/hwmon/lm78.c
> index 6cf6bff79003..fc4578195674 100644
SNIP
>  		return 255;
>  	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
>  }


- Roger

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-12-05 14:29 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-05 10:58 [patch] hwmon: prevent some divide by zeros in FAN_TO_REG() Dan Carpenter
2013-12-05 10:58 ` [lm-sensors] " Dan Carpenter
2013-12-05 12:06 ` Jean Delvare
2013-12-05 12:06   ` [lm-sensors] " Jean Delvare
2013-12-05 12:59 ` Dan Carpenter
2013-12-05 12:59   ` [lm-sensors] " Dan Carpenter
2013-12-05 13:13 ` Dan Carpenter
2013-12-05 13:13   ` [lm-sensors] " Dan Carpenter
2013-12-05 14:29 ` Jean Delvare
2013-12-05 14:29   ` [lm-sensors] " Jean Delvare
  -- strict thread matches above, loose matches on Subject: below --
2013-12-05 11:07 roger

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.