hwmon: prevent some divide by zeros in FAN_TO_REG()
Subsystems (4)
PATCH v2 1/1 hwmon: prevent some divide by zeros in FAN_TO_REG()
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 <[email protected]>
---
v2: cap "rpm" instead of just checking for divide by zero.
drivers/hwmon/vt8231.c +1 -1
@@ -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 <=0 || rpm >1310720 )return 0 ;return clamp_val (1310720 / (rpm * div),1 ,255 ); }
drivers/hwmon/lm78.c +2 -0
@@ -94,6 +94,8 @@ static inline u8 FAN_TO_REG(long rpm, int div) {if (rpm <=0 )return 255 ; +if (rpm >1350000 ) +return 1 ;return clamp_val ((1350000 + rpm * div /2 ) / (rpm * div),1 ,254 ); }
drivers/hwmon/sis5595.c +2 -0
@@ -141,6 +141,8 @@ static inline u8 FAN_TO_REG(long rpm, int div) {if (rpm <=0 )return 255 ; +if (rpm >1350000 ) +return 1 ;return clamp_val ((1350000 + rpm * div /2 ) / (rpm * div),1 ,254 ); }
Jean Delvare
AppliedQuoted 39 lines · Dan Carpenter
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 <[email protected]>---
v2: cap "rpm" instead of just checking for divide by zero.drivers/hwmon/vt8231.c +1
-1@@ -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 <=0 || rpm >1310720 )return 0 ;return clamp_val (1310720 / (rpm * div),1 ,255 ); }drivers/hwmon/lm78.c +2
-0@@ -94,6 +94,8 @@ static inline u8 FAN_TO_REG(long rpm, int div) {if (rpm <=0 )return 255 ; +if (rpm >1350000 ) +return 1 ;return clamp_val ((1350000 + rpm * div /2 ) / (rpm * div),1 ,254 ); }drivers/hwmon/sis5595.c +0
-0
drivers/hwmon/sis5595.c static inline u8 FAN_TO_REG(long rpm, int div) · line 148
Quoted 8 lines
drivers/hwmon/sis5595.c +2
-0@@ -141,6 +141,8 @@ static inline u8 FAN_TO_REG(long rpm, int div) {if (rpm <=0 )return 255 ; +if (rpm >1350000 ) +return 1 ;return clamp_val ((1350000 + rpm * div /2 ) / (rpm * div),1 ,254 ); }
Applied, thanks.
Signature · 1 line
Jean Delvare