Skip to content
Snippets Groups Projects
Commit e9b24deb authored by David Lechner's avatar David Lechner Committed by Guenter Roeck
Browse files

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: 2b9ea426 ("hwmon: Add driver for ltc2991")
Signed-off-by: default avatarDavid Lechner <dlechner@baylibre.com>
Link: https://lore.kernel.org/r/20250115-hwmon-ltc2991-fix-div-round-closest-v1-1-b4929667e457@baylibre.com


Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
parent b46ba47d
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment