Linux Hardware Monitor development
 help / color / mirror / Atom feed
* [PATCH 1/2] hwmon: (max16065) Fix overflows seen when writing limits
@ 2024-07-21 19:35 Guenter Roeck
  2024-07-21 19:35 ` [PATCH 2/2] hwmon: (max16065) Fix alarm attributes Guenter Roeck
  2024-07-22  3:30 ` [PATCH 1/2] hwmon: (max16065) Fix overflows seen when writing limits Tzung-Bi Shih
  0 siblings, 2 replies; 7+ messages in thread
From: Guenter Roeck @ 2024-07-21 19:35 UTC (permalink / raw)
  To: Hardware Monitoring; +Cc: Guenter Roeck

Writing large limits resulted in overflows as reported by module tests.

in0_lcrit: Suspected overflow: [max=5538, read 0, written 2147483647]
in0_crit: Suspected overflow: [max=5538, read 0, written 2147483647]
in0_min: Suspected overflow: [max=5538, read 0, written 2147483647]

Fix the problem by clamping prior to multiplications and the use of
DIV_ROUND_CLOSEST, and by using consistent variable types.

Fixes: f5bae2642e3d ("hwmon: Driver for MAX16065 System Manager and compatibles")
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/max16065.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/hwmon/max16065.c b/drivers/hwmon/max16065.c
index 7ce9a89f93a0..5b2a174c6bad 100644
--- a/drivers/hwmon/max16065.c
+++ b/drivers/hwmon/max16065.c
@@ -114,9 +114,10 @@ static inline int LIMIT_TO_MV(int limit, int range)
 	return limit * range / 256;
 }
 
-static inline int MV_TO_LIMIT(int mv, int range)
+static inline int MV_TO_LIMIT(unsigned long mv, int range)
 {
-	return clamp_val(DIV_ROUND_CLOSEST(mv * 256, range), 0, 255);
+	mv = clamp_val(mv, 0, ULONG_MAX / 256);
+	return DIV_ROUND_CLOSEST(clamp_val(mv * 256, 0, range * 255), range);
 }
 
 static inline int ADC_TO_CURR(int adc, int gain)
-- 
2.39.2


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

end of thread, other threads:[~2024-07-22 13:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-21 19:35 [PATCH 1/2] hwmon: (max16065) Fix overflows seen when writing limits Guenter Roeck
2024-07-21 19:35 ` [PATCH 2/2] hwmon: (max16065) Fix alarm attributes Guenter Roeck
2024-07-22  3:30   ` Tzung-Bi Shih
2024-07-22  3:48     ` Guenter Roeck
2024-07-22  5:47       ` Tzung-Bi Shih
2024-07-22 13:45         ` Guenter Roeck
2024-07-22  3:30 ` [PATCH 1/2] hwmon: (max16065) Fix overflows seen when writing limits Tzung-Bi Shih

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox