projects/linux·commit dbe7208c

power: supply: cpcap-battery: Fix division by zero

dbe7208c · Tony Lindgren <[email protected]> · 2019-04-07 11:12:48 -07:00

If called fast enough so samples do not increment, we can get
division by zero in kernel:

__div0
cpcap_battery_cc_raw_div
cpcap_battery_get_property
power_supply_get_property.part.1
power_supply_get_property
power_supply_show_property
power_supply_uevent

Fixes: 874b2adbed12 ("power: supply: cpcap-battery: Add a battery driver")
Signed-off-by: Tony Lindgren <[email protected]>
Acked-by: Pavel Machek <[email protected]>
Signed-off-by: Sebastian Reichel <[email protected]>
Commit
dbe7208c6c4aec083571f2ec742870a0d0edbea3
Parents
9e98c678

Changes

1 files changed · +3 −0

FileStatusAddDelContent
drivers/power/supply/cpcap-battery.cmodified+3−0View file

drivers/power/supply/cpcap-battery.c

View file
diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
index 08d5037..6887870 100644
--- a/drivers/power/supply/cpcap-battery.c
+++ b/drivers/power/supply/cpcap-battery.c
@@ -221,6 +221,9 @@ static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
int avg_current;
u32 cc_lsb;
+ if (!divider)
+ return 0;
+
sample &= 0xffffff; /* 24-bits, unsigned */
offset &= 0x7ff; /* 10-bits, signed */