[PATCH] power: supply: cpcap-battery: Fix nvmem device leak in cpcap_battery_detect_battery_type()

Wentao Liang posted 1 patch 1 week ago
drivers/power/supply/cpcap-battery.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH] power: supply: cpcap-battery: Fix nvmem device leak in cpcap_battery_detect_battery_type()
Posted by Wentao Liang 1 week ago
cpcap_battery_detect_battery_type() takes a reference on the nvmem
device with nvmem_device_find(), but never releases it, so the
reference leaks on every call. Drop it once the battery ID has been
read.

Fixes: fd46821e85de ("power: supply: cpcap-battery: Add battery type auto detection for mapphone devices")
Cc: stable@vger.kernel.org
Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
---
 drivers/power/supply/cpcap-battery.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
index 7b7bdce3162f..b7ed4f2d3826 100644
--- a/drivers/power/supply/cpcap-battery.c
+++ b/drivers/power/supply/cpcap-battery.c
@@ -445,6 +445,9 @@ static void cpcap_battery_detect_battery_type(struct cpcap_battery_ddata *ddata)
 		dev_warn(ddata->dev, "Can not read battery nvmem device. Assuming generic lipo battery\n");
 	}
 
+	if (!IS_ERR_OR_NULL(nvmem))
+		nvmem_device_put(nvmem);
+
 	switch (battery_id) {
 	case CPCAP_BATTERY_EB41_HW4X_ID:
 		ddata->config = cpcap_battery_eb41_data;
-- 
2.34.1