Skip to contentSkip to conversation

Input: xpad - fix memory leak

PATCH In review Namhyung Kim 2 participants 4 messages Dec 1, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Input: xpad - fix memory leak

xpad->bdata was dynamically allocated but never freed. Since it is used for xpad->bulk_out->transfer_buffer, set URB_FREE_BUFFER flag in order to get freed when usb_free_urb() called.

---
 drivers/input/joystick/xpad.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
drivers/input/joystick/xpad.c +1 -0
index f9fb7fa..f885902 100644
@@ -955,6 +955,7 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 		usb_fill_bulk_urb(xpad->bulk_out, udev,
 				usb_sndbulkpipe(udev, ep_irq_in->bEndpointAddress),
 				xpad->bdata, XPAD_PKT_LEN, xpad_bulk_out, xpad);
+		xpad->bulk_out->transfer_flags |= URB_FREE_BUFFER;
 	}
 
 	return 0;
Signature · 1 line
1.7.0.4
#1 lore raw

PATCH 2/2 Input: xpad - sanitize xpad_led_disconnect()

IMHO kfree() here looks very confusing. xpad_led->name is an array of char inside struct xpad_led and is not a dynamic memory itself. But kfree() works well because it is a first member of the struct so it points start address of the struct and frees the struct.

Change it to xpad_led for the correctness & readability and make xpad->led NULL for the safety.

Cc Jan Kratochvil <[email protected]>
---
 drivers/input/joystick/xpad.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
drivers/input/joystick/xpad.c +2 -1
index f885902..1ca49e8 100644
@@ -728,7 +728,8 @@ static void xpad_led_disconnect(struct usb_xpad *xpad)
 
 	if (xpad_led) {
 		led_classdev_unregister(&xpad_led->led_cdev);
-		kfree(xpad_led->name);
+		kfree(xpad_led);
+		xpad->led = NULL;
 	}
 }
 #else
Signature · 1 line
1.7.0.4
maintainer
#2 lore raw

Re: [PATCH 2/2] Input: xpad - sanitize xpad_led_disconnect()

Hi Namhyung,

Quoted 4 lines · Namhyung Kim

IMHO kfree() here looks very confusing. xpad_led->name is an array of char inside struct xpad_led and is not a dynamic memory itself. But kfree() works well because it is a first member of the struct so it points start address of the struct and frees the struct.

This has already been fixed in my tree, thanks.

Quoted 27 lines

Change it to xpad_led for the correctness & readability and make xpad->led NULL for the safety.

Cc Jan Kratochvil <[email protected]>
---
 drivers/input/joystick/xpad.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
drivers/input/joystick/xpad.c +2 -1
index f885902..1ca49e8 100644
@@ -728,7 +728,8 @@ static void xpad_led_disconnect(struct usb_xpad *xpad)
 
 	if (xpad_led) {
 		led_classdev_unregister(&xpad_led->led_cdev);
-		kfree(xpad_led->name);
+		kfree(xpad_led);
+		xpad->led = NULL;
 	}
 }
 #else
Signature · 1 line
1.7.0.4
Signature · 1 line
Dmitry
maintainer
#3 lore raw
In reply to Namhyung Kim
Quoted 4 lines · Namhyung Kim

xpad->bdata was dynamically allocated but never freed. Since it is used for xpad->bulk_out->transfer_buffer, set URB_FREE_BUFFER flag in order to get freed when usb_free_urb() called.

This is also already fixed in my tree (next branch, slated for 2.6.38).

Thanks.

Signature · 1 line
Dmitry