Skip to content

Commit e9b24de

Browse files
dlechgroeck
authored andcommitted
hwmon: (ltc2991) Fix mixed signed/unsigned in DIV_ROUND_CLOSEST
Fix use of DIV_ROUND_CLOSEST where a possibly negative value is divided by an unsigned type by casting the unsigned type to the signed type of the same size (st->r_sense_uohm[channel] has type of u32). The docs on the DIV_ROUND_CLOSEST macro explain that dividing a negative value by an unsigned type is undefined behavior. The actual behavior is that it converts both values to unsigned before doing the division, for example: int ret = DIV_ROUND_CLOSEST(-100, 3U); results in ret == 1431655732 instead of -33. Fixes: 2b9ea42 ("hwmon: Add driver for ltc2991") Signed-off-by: David Lechner <[email protected]> Link: https://lore.kernel.org/r/20250115-hwmon-ltc2991-fix-div-round-closest-v1-1-b4929667e457@baylibre.com Signed-off-by: Guenter Roeck <[email protected]>
1 parent b46ba47 commit e9b24de

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

drivers/hwmon/ltc2991.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static int ltc2991_get_curr(struct ltc2991_state *st, u32 reg, int channel,
125125

126126
/* Vx-Vy, 19.075uV/LSB */
127127
*val = DIV_ROUND_CLOSEST(sign_extend32(reg_val, 14) * 19075,
128-
st->r_sense_uohm[channel]);
128+
(s32)st->r_sense_uohm[channel]);
129129

130130
return 0;
131131
}

0 commit comments

Comments
 (0)