Skip to contentSkip to conversation

staging: rtl8192u: add error handling for usb_alloc_urb

PATCH Superseded Zhouyang Jia 5 participants 12 messages Jun 11, 2018 – Jun 16, 2018 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: rtl8192u: add error handling for usb_alloc_urb

When usb_alloc_urb fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling usb_alloc_urb.

---
 drivers/staging/rtl8192u/r8192U_core.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/staging/rtl8192u/r8192U_core.c +3 -0
index 7a0dbc0..3f09615 100644
@@ -1666,6 +1666,9 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 		void *oldaddr, *newaddr;
 
 		priv->rx_urb[16] = usb_alloc_urb(0, GFP_KERNEL);
+		if (!priv->rx_urb[16])
+			return -ENOMEM;
+
 		priv->oldaddr = kmalloc(16, GFP_KERNEL);
 		if (!priv->oldaddr)
 			return -ENOMEM;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 15 lines · Zhouyang Jia

When usb_alloc_urb fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling usb_alloc_urb.

---
 drivers/staging/rtl8192u/r8192U_core.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/staging/rtl8192u/r8192U_core.c +0 -0
index 7a0dbc0..3f09615 100644

drivers/staging/rtl8192u/r8192U_core.c static short rtl8192_usb_initendpoints(struct net_device *dev) · line 1671

Quoted 6 lines
drivers/staging/rtl8192u/r8192U_core.c +3 -0
@@ -1666,6 +1666,9 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 		void *oldaddr, *newaddr;
 
 		priv->rx_urb[16] = usb_alloc_urb(0, GFP_KERNEL);
+		if (!priv->rx_urb[16])
+			return -ENOMEM;
+

You just leaked memory :(

Well, this whole function leaks memory on the error paths, like here:

drivers/staging/rtl8192u/r8192U_core.c static short rtl8192_usb_initendpoints(struct net_device *dev) · line 1674

Quoted 3 lines
drivers/staging/rtl8192u/r8192U_core.c +0 -0
 		priv->oldaddr = kmalloc(16, GFP_KERNEL);
 		if (!priv->oldaddr)
 			return -ENOMEM;

So can you fix this all up at the same time?

thanks,

greg k-h

#2 lore raw

PATCH v2 1/1 staging: rtl8192u: add error handling for usb_alloc_urb

In reply to Zhouyang Jia

When usb_alloc_urb fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling usb_alloc_urb.

---
v1->v2:
- Fix memory leak.

---
 drivers/staging/rtl8192u/r8192U_core.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/staging/rtl8192u/r8192U_core.c +15 -3
index 7a0dbc0..6afab4e 100644
@@ -1648,13 +1648,17 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 #ifndef JACKSON_NEW_RX
 	for (i = 0; i < (MAX_RX_URB + 1); i++) {
 		priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
-		if (!priv->rx_urb[i])
+		if (!priv->rx_urb[i]) {
+			kfree(priv->rx_urb);
 			return -ENOMEM;
+		}
 
 		priv->rx_urb[i]->transfer_buffer =
 			kmalloc(RX_URB_SIZE, GFP_KERNEL);
-		if (!priv->rx_urb[i]->transfer_buffer)
+		if (!priv->rx_urb[i]->transfer_buffer) {
+			kfree(priv->rx_urb);
 			return -ENOMEM;
+		}
 
 		priv->rx_urb[i]->transfer_buffer_length = RX_URB_SIZE;
 	}
@@ -1666,9 +1670,17 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 		void *oldaddr, *newaddr;
 
 		priv->rx_urb[16] = usb_alloc_urb(0, GFP_KERNEL);
+		if (!priv->rx_urb[16]) {
+			kfree(priv->rx_urb);
+			return -ENOMEM;
+		}
+
 		priv->oldaddr = kmalloc(16, GFP_KERNEL);
-		if (!priv->oldaddr)
+		if (!priv->oldaddr) {
+			kfree(priv->rx_urb);
 			return -ENOMEM;
+		}
+
 		oldaddr = priv->oldaddr;
 		align = ((long)oldaddr) & 3;
 		if (align) {
Signature · 1 line
2.7.4
#3 lore raw
Quoted 18 lines · Zhouyang Jia

When usb_alloc_urb fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling usb_alloc_urb.

---
v1->v2:
- Fix memory leak.

---
 drivers/staging/rtl8192u/r8192U_core.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/staging/rtl8192u/r8192U_core.c +0 -0
index 7a0dbc0..6afab4e 100644

drivers/staging/rtl8192u/r8192U_core.c static short rtl8192_usb_initendpoints(struct net_device *dev) · line 1654

Quoted 8 lines
drivers/staging/rtl8192u/r8192U_core.c +3 -1
@@ -1648,13 +1648,17 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 #ifndef JACKSON_NEW_RX
 	for (i = 0; i < (MAX_RX_URB + 1); i++) {
 		priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
-		if (!priv->rx_urb[i])
+		if (!priv->rx_urb[i]) {
+			kfree(priv->rx_urb);
 			return -ENOMEM;
+		}

{sigh}

No, you are still leaking memory on all of these changes that you just made :(

greg k-h

#4 lore raw
Quoted 14 lines · Greg Kroah-Hartman
Quoted 13 lines · Zhouyang Jia

When usb_alloc_urb fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling usb_alloc_urb.

---
v1->v2:
- Fix memory leak.

---
 drivers/staging/rtl8192u/r8192U_core.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
Quoted 11 lines

drivers/staging/rtl8192u/r8192U_core.c static short rtl8192_usb_initendpoints(struct net_device *dev) · line 1652

Quoted 11 lines
drivers/staging/rtl8192u/r8192U_core.c +2 -1
index 7a0dbc0..6afab4e 100644
@@ -1648,13 +1648,17 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 #ifndef JACKSON_NEW_RX
      for (i = 0; i < (MAX_RX_URB + 1); i++) {
              priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
-             if (!priv->rx_urb[i])
+             if (!priv->rx_urb[i]) {
+                     kfree(priv->rx_urb);

You're freeing rx_urb, which holds all the pointers to allocated memory. You'll need to free each item of the array before freeing the array itself:

for (i = 0; i < (MAX_RX_URB + 1); i++)

    kfree(priv->rx_urb[i]);

kfree(priv->rx_urb);

I think you need some kind of helper to do this, and you can call into it from your error paths...

-Kees

Quoted 6 lines

{sigh}

No, you are still leaking memory on all of these changes that you just made :(

greg k-h

Signature · 2 lines
Kees Cook
Pixel Security
#5 lore raw
Quoted 32 lines · Kees Cook
Quoted 14 lines · Greg Kroah-Hartman
Quoted 13 lines · Zhouyang Jia

When usb_alloc_urb fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling usb_alloc_urb.

---
v1->v2:
- Fix memory leak.

---
 drivers/staging/rtl8192u/r8192U_core.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
Quoted 11 lines

drivers/staging/rtl8192u/r8192U_core.c static short rtl8192_usb_initendpoints(struct net_device *dev) · line 1652

Quoted 11 lines
drivers/staging/rtl8192u/r8192U_core.c +2 -1
index 7a0dbc0..6afab4e 100644
@@ -1648,13 +1648,17 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 #ifndef JACKSON_NEW_RX
      for (i = 0; i < (MAX_RX_URB + 1); i++) {
              priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
-             if (!priv->rx_urb[i])
+             if (!priv->rx_urb[i]) {
+                     kfree(priv->rx_urb);

You're freeing rx_urb, which holds all the pointers to allocated memory. You'll need to free each item of the array before freeing the array itself:

Quoted 6 lines

for (i = 0; i < (MAX_RX_URB + 1); i++)

    kfree(priv->rx_urb[i]);

kfree(priv->rx_urb);

I think you need some kind of helper to do this, and you can call into it from your error paths...

(Though if you do this, rx_urb must be zero-initialized, so change the kmalloc_array() to kcalloc()...)

-Kees

Quoted 15 lines

-Kees

Quoted 6 lines

{sigh}

No, you are still leaking memory on all of these changes that you just made :(

greg k-h

--
Kees Cook
Pixel Security

Signature · 2 lines
Kees Cook
Pixel Security
#6 lore raw

PATCH v3 1/1 staging: rtl8192u: add error handling for usb_alloc_urb

In reply to Zhouyang Jia

When usb_alloc_urb fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling usb_alloc_urb.

---
v1->v2:
- Fix memory leak.
v2->v3:
- Release memory in error path.

---
 drivers/staging/rtl8192u/r8192U_core.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
drivers/staging/rtl8192u/r8192U_core.c +15 -9
index 7a0dbc0..1c980e9 100644
@@ -1649,12 +1649,12 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 	for (i = 0; i < (MAX_RX_URB + 1); i++) {
 		priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
 		if (!priv->rx_urb[i])
-			return -ENOMEM;
+			goto out_release_mem;
 
 		priv->rx_urb[i]->transfer_buffer =
 			kmalloc(RX_URB_SIZE, GFP_KERNEL);
 		if (!priv->rx_urb[i]->transfer_buffer)
-			return -ENOMEM;
+			goto out_release_mem;
 
 		priv->rx_urb[i]->transfer_buffer_length = RX_URB_SIZE;
 	}
@@ -1666,9 +1666,13 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 		void *oldaddr, *newaddr;
 
 		priv->rx_urb[16] = usb_alloc_urb(0, GFP_KERNEL);
+		if (!priv->rx_urb[16])
+			goto out_release_mem;
+
 		priv->oldaddr = kmalloc(16, GFP_KERNEL);
 		if (!priv->oldaddr)
-			return -ENOMEM;
+			goto out_release_mem;
+
 		oldaddr = priv->oldaddr;
 		align = ((long)oldaddr) & 3;
 		if (align) {
@@ -1686,17 +1690,19 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 	priv->pp_rxskb = kcalloc(MAX_RX_URB, sizeof(struct sk_buff *),
 				 GFP_KERNEL);
 	if (!priv->pp_rxskb) {
-		kfree(priv->rx_urb);
-
-		priv->pp_rxskb = NULL;
-		priv->rx_urb = NULL;
-
 		DMESGE("Endpoint Alloc Failure");
-		return -ENOMEM;
+		goto out_release_mem;
 	}
 
 	netdev_dbg(dev, "End of initendpoints\n");
 	return 0;
+
+out_release_mem:
+	for (i = 0; i < (MAX_RX_URB + 1); i++)
+		kfree(priv->rx_urb[i]);
+	kfree(priv->rx_urb);
+	priv->rx_urb = NULL;
+	return -ENOMEM;
 }
 
 #ifdef THOMAS_BEACON
Signature · 1 line
2.7.4
#7 lore raw
Thank you for the patch! Yet something to improve:

Hi,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on staging/staging-testing]
[also build test ERROR on v4.17 next-20180615]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/linux-kernel-owner-vger-kernel-org/staging-rtl8192u-add-error-handling-for-usb_alloc_urb/20180616-012944
config: xtensa-allyesconfig (attached as .config)
compiler: xtensa-linux-gcc (GCC) 8.1.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=8.1.0 make.cross ARCH=xtensa

All errors (new ones prefixed by >>):

drivers/staging//rtl8192u/r8192U_core.c: In function 'rtl8192_usb_initendpoints':

>> drivers/staging//rtl8192u/r8192U_core.c:1701:7: error: 'i' undeclared (first use in this function)
     for (i = 0; i < (MAX_RX_URB + 1); i++)
          ^

drivers/staging//rtl8192u/r8192U_core.c:1701:7: note: each undeclared identifier is reported only once for each function it appears in

vim +/i +1701 drivers/staging//rtl8192u/r8192U_core.c

1688
1689 memset(priv->rx_urb, 0, sizeof(struct urb *) * MAX_RX_URB);
1690 priv->pp_rxskb = kcalloc(MAX_RX_URB, sizeof(struct sk_buff *),
1691 GFP_KERNEL);
1692 if (!priv->pp_rxskb) {
1693 DMESGE("Endpoint Alloc Failure");
1694 goto out_release_mem;
1695 }
1696
1697 netdev_dbg(dev, "End of initendpoints\n");
1698 return 0;
1699
1700 out_release_mem:

> 1701		for (i = 0; i < (MAX_RX_URB + 1); i++)

1702 kfree(priv->rx_urb[i]);
1703 kfree(priv->rx_urb);
1704 priv->rx_urb = NULL;
1705 return -ENOMEM;
1706 }
1707

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#8 lore raw
In reply to Zhouyang Jia
Quoted 20 lines · Zhouyang Jia

When usb_alloc_urb fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling usb_alloc_urb.

---
v1->v2:
- Fix memory leak.
v2->v3:
- Release memory in error path.

---
 drivers/staging/rtl8192u/r8192U_core.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
drivers/staging/rtl8192u/r8192U_core.c +0 -0
index 7a0dbc0..1c980e9 100644

drivers/staging/rtl8192u/r8192U_core.c static short rtl8192_usb_initendpoints(struct net_device *dev) · line 1652

Quoted 5 lines
drivers/staging/rtl8192u/r8192U_core.c +1 -1
@@ -1649,12 +1649,12 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
        for (i = 0; i < (MAX_RX_URB + 1); i++) {
                priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
                if (!priv->rx_urb[i])
-                       return -ENOMEM;
+                       goto out_release_mem;

You need to use kcalloc() above here for priv->rx_urb, otherwise you may free random garbage. :)

-Kees

Quoted 53 lines
drivers/staging/rtl8192u/r8192U_core.c +14 -9
                priv->rx_urb[i]->transfer_buffer =
                        kmalloc(RX_URB_SIZE, GFP_KERNEL);
                if (!priv->rx_urb[i]->transfer_buffer)
-                       return -ENOMEM;
+                       goto out_release_mem;
 
                priv->rx_urb[i]->transfer_buffer_length = RX_URB_SIZE;
        }
@@ -1666,9 +1666,13 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
                void *oldaddr, *newaddr;
 
                priv->rx_urb[16] = usb_alloc_urb(0, GFP_KERNEL);
+               if (!priv->rx_urb[16])
+                       goto out_release_mem;
+
                priv->oldaddr = kmalloc(16, GFP_KERNEL);
                if (!priv->oldaddr)
-                       return -ENOMEM;
+                       goto out_release_mem;
+
                oldaddr = priv->oldaddr;
                align = ((long)oldaddr) & 3;
                if (align) {
@@ -1686,17 +1690,19 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
        priv->pp_rxskb = kcalloc(MAX_RX_URB, sizeof(struct sk_buff *),
                                 GFP_KERNEL);
        if (!priv->pp_rxskb) {
-               kfree(priv->rx_urb);
-
-               priv->pp_rxskb = NULL;
-               priv->rx_urb = NULL;
-
                DMESGE("Endpoint Alloc Failure");
-               return -ENOMEM;
+               goto out_release_mem;
        }
 
        netdev_dbg(dev, "End of initendpoints\n");
        return 0;
+
+out_release_mem:
+       for (i = 0; i < (MAX_RX_URB + 1); i++)
+               kfree(priv->rx_urb[i]);
+       kfree(priv->rx_urb);
+       priv->rx_urb = NULL;
+       return -ENOMEM;
 }
 
 #ifdef THOMAS_BEACON
--

2.7.4

Signature · 2 lines
Kees Cook
Pixel Security
#9 lore raw

PATCH v4 1/1 staging: rtl8192u: add error handling for usb_alloc_urb

In reply to Zhouyang Jia

When usb_alloc_urb fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling usb_alloc_urb.

---
v1->v2:
- Fix memory leak.
v2->v3:
- Release memory in error path.
v3->v4:
- Use kcalloc instead of kmalloc_array.

---
 drivers/staging/rtl8192u/r8192U_core.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
drivers/staging/rtl8192u/r8192U_core.c +17 -10
index 7a0dbc0..d15ee4f 100644
@@ -1639,8 +1639,9 @@ short rtl8192_tx(struct net_device *dev, struct sk_buff *skb)
 static short rtl8192_usb_initendpoints(struct net_device *dev)
 {
 	struct r8192_priv *priv = ieee80211_priv(dev);
+	int i;
 
-	priv->rx_urb = kmalloc(sizeof(struct urb *) * (MAX_RX_URB + 1),
+	priv->rx_urb = kcalloc(MAX_RX_URB + 1, sizeof(struct urb *),
 			       GFP_KERNEL);
 	if (!priv->rx_urb)
 		return -ENOMEM;
@@ -1649,12 +1650,12 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 	for (i = 0; i < (MAX_RX_URB + 1); i++) {
 		priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
 		if (!priv->rx_urb[i])
-			return -ENOMEM;
+			goto out_release_mem;
 
 		priv->rx_urb[i]->transfer_buffer =
 			kmalloc(RX_URB_SIZE, GFP_KERNEL);
 		if (!priv->rx_urb[i]->transfer_buffer)
-			return -ENOMEM;
+			goto out_release_mem;
 
 		priv->rx_urb[i]->transfer_buffer_length = RX_URB_SIZE;
 	}
@@ -1666,9 +1667,13 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 		void *oldaddr, *newaddr;
 
 		priv->rx_urb[16] = usb_alloc_urb(0, GFP_KERNEL);
+		if (!priv->rx_urb[16])
+			goto out_release_mem;
+
 		priv->oldaddr = kmalloc(16, GFP_KERNEL);
 		if (!priv->oldaddr)
-			return -ENOMEM;
+			goto out_release_mem;
+
 		oldaddr = priv->oldaddr;
 		align = ((long)oldaddr) & 3;
 		if (align) {
@@ -1686,17 +1691,19 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 	priv->pp_rxskb = kcalloc(MAX_RX_URB, sizeof(struct sk_buff *),
 				 GFP_KERNEL);
 	if (!priv->pp_rxskb) {
-		kfree(priv->rx_urb);
-
-		priv->pp_rxskb = NULL;
-		priv->rx_urb = NULL;
-
 		DMESGE("Endpoint Alloc Failure");
-		return -ENOMEM;
+		goto out_release_mem;
 	}
 
 	netdev_dbg(dev, "End of initendpoints\n");
 	return 0;
+
+out_release_mem:
+	for (i = 0; i < (MAX_RX_URB + 1); i++)
+		kfree(priv->rx_urb[i]);
+	kfree(priv->rx_urb);
+	priv->rx_urb = NULL;
+	return -ENOMEM;
 }
 
 #ifdef THOMAS_BEACON
Signature · 1 line
2.7.4
#10 lore raw

I was actually OK with v1 on the theory that everything else leaked and so this didn't really introduce anything new... :P

Quoted 14 lines · Zhouyang Jia
drivers/staging/rtl8192u/r8192U_core.c +2 -1
@@ -1639,8 +1639,9 @@ short rtl8192_tx(struct net_device *dev, struct sk_buff *skb)
 static short rtl8192_usb_initendpoints(struct net_device *dev)
 {
 	struct r8192_priv *priv = ieee80211_priv(dev);
+	int i;
 
-	priv->rx_urb = kmalloc(sizeof(struct urb *) * (MAX_RX_URB + 1),
+	priv->rx_urb = kcalloc(MAX_RX_URB + 1, sizeof(struct urb *),
 			       GFP_KERNEL);
 	if (!priv->rx_urb)
 		return -ENOMEM;

drivers/staging/rtl8192u/r8192U_core.c static short rtl8192_usb_initendpoints(struct net_device *dev) · line 1656

Quoted 8 lines
drivers/staging/rtl8192u/r8192U_core.c +1 -1
@@ -1649,12 +1650,12 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 	for (i = 0; i < (MAX_RX_URB + 1); i++) {
 		priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
 		if (!priv->rx_urb[i])
-			return -ENOMEM;
+			goto out_release_mem;
 
 		priv->rx_urb[i]->transfer_buffer =
 			kmalloc(RX_URB_SIZE, GFP_KERNEL);

You need to free priv->rx_urb[i]->transfer_buffer as well and there are several other resources which are also not freed.

regards,
dan carpenter

#11 lore raw

PATCH v5 1/1 staging: rtl8192u: add error handling for usb_alloc_urb

In reply to Zhouyang Jia

When usb_alloc_urb fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling usb_alloc_urb, and fixes memory leaks in error paths.

---
v1->v2:
- Fix memory leak.
v2->v3:
- Release memory in error path.
v3->v4:
- Use kcalloc instead of kmalloc_array.
v4->v5:
- Free priv->rx_urb[i]->transfer_buffer and priv->oldaddr.

---
 drivers/staging/rtl8192u/r8192U_core.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)
drivers/staging/rtl8192u/r8192U_core.c +24 -10
index 7a0dbc0..9413f29 100644
@@ -1639,8 +1639,9 @@ short rtl8192_tx(struct net_device *dev, struct sk_buff *skb)
 static short rtl8192_usb_initendpoints(struct net_device *dev)
 {
 	struct r8192_priv *priv = ieee80211_priv(dev);
+	int i;
 
-	priv->rx_urb = kmalloc(sizeof(struct urb *) * (MAX_RX_URB + 1),
+	priv->rx_urb = kcalloc(MAX_RX_URB + 1, sizeof(struct urb *),
 			       GFP_KERNEL);
 	if (!priv->rx_urb)
 		return -ENOMEM;
@@ -1649,12 +1650,12 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 	for (i = 0; i < (MAX_RX_URB + 1); i++) {
 		priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
 		if (!priv->rx_urb[i])
-			return -ENOMEM;
+			goto out_release_urb;
 
 		priv->rx_urb[i]->transfer_buffer =
 			kmalloc(RX_URB_SIZE, GFP_KERNEL);
 		if (!priv->rx_urb[i]->transfer_buffer)
-			return -ENOMEM;
+			goto out_release_urb;
 
 		priv->rx_urb[i]->transfer_buffer_length = RX_URB_SIZE;
 	}
@@ -1666,9 +1667,13 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 		void *oldaddr, *newaddr;
 
 		priv->rx_urb[16] = usb_alloc_urb(0, GFP_KERNEL);
+		if (!priv->rx_urb[16])
+			goto out_release_urb;
+
 		priv->oldaddr = kmalloc(16, GFP_KERNEL);
 		if (!priv->oldaddr)
-			return -ENOMEM;
+			goto out_release_urb;
+
 		oldaddr = priv->oldaddr;
 		align = ((long)oldaddr) & 3;
 		if (align) {
@@ -1686,17 +1691,26 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
 	priv->pp_rxskb = kcalloc(MAX_RX_URB, sizeof(struct sk_buff *),
 				 GFP_KERNEL);
 	if (!priv->pp_rxskb) {
-		kfree(priv->rx_urb);
-
-		priv->pp_rxskb = NULL;
-		priv->rx_urb = NULL;
-
 		DMESGE("Endpoint Alloc Failure");
-		return -ENOMEM;
+		goto out_release_oldaddr;
 	}
 
 	netdev_dbg(dev, "End of initendpoints\n");
 	return 0;
+
+out_release_oldaddr:
+	kfree(priv->oldaddr);
+
+out_release_urb:
+	for (i = 0; i < (MAX_RX_URB + 1); i++) {
+		if (priv->rx_urb[i]) {
+			kfree(priv->rx_urb[i]->transfer_buffer);
+			kfree(priv->rx_urb[i]);
+		}
+	}
+	kfree(priv->rx_urb);
+	priv->rx_urb = NULL;
+	return -ENOMEM;
 }
 
 #ifdef THOMAS_BEACON
Signature · 1 line
2.7.4