Skip to contentSkip to conversation

mfd: Fix memory leak in mfd_add_devices()

PATCH In review Geert Uytterhoeven 2 participants 2 messages Oct 29, 2013 – Dec 4, 2013 Multifunction devices (MFD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mfd: Fix memory leak in mfd_add_devices()

If the first call to mfd_add_device() fails, no child devices have been registered to the parent yet, and thus mfd_remove_devices() won't find anything to remove nor free.
Hence the previously allocated array of atomic_t objects will leak.

Free the array instead of calling mfd_remove_devices() on failure during the first loop iteration to fix this.

---
 drivers/mfd/mfd-core.c |   12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/mfd/mfd-core.c +8 -4
index 8736f4539bc0..968775da638a 100644
@@ -187,7 +187,7 @@ int mfd_add_devices(struct device *parent, int id,
 		    int irq_base, struct irq_domain *domain)
 {
 	int i;
-	int ret = 0;
+	int ret;
 	atomic_t *cnts;
 
 	/* initialize reference counting for all cells */
@@ -200,12 +200,16 @@ int mfd_add_devices(struct device *parent, int id,
 		ret = mfd_add_device(parent, id, cells + i, cnts + i, mem_base,
 				     irq_base, domain);
 		if (ret)
-			break;
+			goto fail;
 	}
 
-	if (ret)
-		mfd_remove_devices(parent);
+	return 0;
 
+fail:
+	if (i)
+		mfd_remove_devices(parent);
+	else
+		kfree(cnts);
 	return ret;
 }
 EXPORT_SYMBOL(mfd_add_devices);
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 5 lines · Geert Uytterhoeven

If the first call to mfd_add_device() fails, no child devices have been registered to the parent yet, and thus mfd_remove_devices() won't find anything to remove nor free.
Hence the previously allocated array of atomic_t objects will leak.

Quoted 7 lines

Free the array instead of calling mfd_remove_devices() on failure during the first loop iteration to fix this.

---
 drivers/mfd/mfd-core.c |   12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

Hmm... I didn't see my reply to this, but it has been applied for a while.

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog