Skip to contentSkip to conversation

usb: dwc3: gadget: avoid memory leak when failing to allocate all eps

PATCH Superseded David Cohen 1 participant 2 messages Sep 12, 2013

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: dwc3: gadget: avoid memory leak when failing to allocate all eps

If dwc3_gadget_init_endpoint() fails after allocate some of the eps, we need to free their memory to avoid leak.

---
 drivers/usb/dwc3/gadget.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/usb/dwc3/gadget.c +8 -5
index f168eae..611bdba 100644
@@ -1707,8 +1707,13 @@ static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
 
 	for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
 		dep = dwc->eps[epnum];
+		/*
+		 * Because dwc was allocated by kzalloc() and eps are set
+		 * in ascending order, we can assume no extra one was
+		 * allocated after first dep == NULL.
+		 */
 		if (!dep)
-			continue;
+			break;
 		/*
 		 * Physical endpoints 0 and 1 are special; they form the
 		 * bi-directional USB endpoint 0.
@@ -2611,15 +2616,13 @@ int dwc3_gadget_init(struct dwc3 *dwc)
 	ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
 	if (ret) {
 		dev_err(dwc->dev, "failed to register udc\n");
-		goto err5;
+		goto err4;
 	}
 
 	return 0;
 
-err5:
-	dwc3_gadget_free_endpoints(dwc);
-
 err4:
+	dwc3_gadget_free_endpoints(dwc);
 	dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
 			dwc->ep0_bounce, dwc->ep0_bounce_addr);
 
Signature · 1 line
1.8.4.rc3
#1 lore raw

Hi Mr. Balbi, :)

Quoted 13 lines · David Cohen

If dwc3_gadget_init_endpoint() fails after allocate some of the eps, we need to free their memory to avoid leak.

---
  drivers/usb/dwc3/gadget.c | 13 ++++++++-----
  1 file changed, 8 insertions(+), 5 deletions(-)
drivers/usb/dwc3/gadget.c +0 -0
index f168eae..611bdba 100644

drivers/usb/dwc3/gadget.c static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) · line 1714

Quoted 8 lines
drivers/usb/dwc3/gadget.c +5 -0
@@ -1707,8 +1707,13 @@ static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
 
  	for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
  		dep = dwc->eps[epnum];
+		/*
+		 * Because dwc was allocated by kzalloc() and eps are set
+		 * in ascending order, we can assume no extra one was
+		 * allocated after first dep == NULL.
+		 */

Wrong assumption here. Please consider patch v2 instead.

BR, David Cohen