[PATCH] hwmon: (lochnagar) Avoid oversized shifts for zero readings

From: Zhu Jun

Date: Wed Nov 11 2026 - 01:57:22 EST


A zero IEEE754 reading gives exp = -150 in float_to_long(). Both shifts
in the rounding expression then exceed the width of u64. UBSAN reports a
shift exponent of 149, and the conversion can return a nonzero reading.

Return zero when the required right shift is at least 64 bits. With the
supported precisions, the scaled mantissa is too small to round up in
this case.

Fixes: 4cdb56214746 ("hwmon: lochnagar: Add Lochnagar 2 hardware monitoring driver")
Signed-off-by: Zhu Jun <zhujun2@xxxxxxxxxxxxxxxxxxxx>
---
drivers/hwmon/lochnagar-hwmon.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/drivers/hwmon/lochnagar-hwmon.c b/drivers/hwmon/lochnagar-hwmon.c
index 24a255d4c46b..328589e19917 100644
--- a/drivers/hwmon/lochnagar-hwmon.c
+++ b/drivers/hwmon/lochnagar-hwmon.c
@@ -74,6 +74,8 @@ static long float_to_long(u32 data, u32 precision)

if (fls64(man) + exp > (int)sizeof(long) * 8 - 1)
result = LONG_MAX;
+ else if (exp <= -64)
+ result = 0;
else if (exp < 0)
result = (man + (1ull << (-exp - 1))) >> -exp;
else
--
2.51.0