diff mbox series

[1/7] power: supply: cpcap-battery: Fix division by zero

Message ID 20190407181254.64537-2-tony@atomide.com (mailing list archive)
State New, archived
Headers show
Series Some fixes and improvments for cpcap battery and charger | expand

Commit Message

Tony Lindgren April 7, 2019, 6:12 p.m. UTC
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")
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Tony Lindgren <tony@atomide.com>
---
 drivers/power/supply/cpcap-battery.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Pavel Machek April 8, 2019, 12:01 p.m. UTC | #1
On Sun 2019-04-07 11:12:48, Tony Lindgren wrote:
> 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")
> Cc: Pavel Machek <pavel@ucw.cz>
> Signed-off-by: Tony Lindgren <tony@atomide.com>

Acked-by: Pavel Machek <pavel@ucw.cz>
diff mbox series

Patch

diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
--- 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 */