Skip to contentSkip to conversation

rc: correctly handling failed allocation

PATCH In review Insu Yun 3 participants 3 messages Feb 16, 2016 – Mar 3, 2016

Subsystems (3)
lore
Share & tools
lore

Insu Yun

author
#0 lore raw

PATCH 1/1 rc: correctly handling failed allocation

Since rc_allocate_device() uses kmalloc,
it can returns NULL, so need to check,
otherwise, NULL derefenrece can be happened.

---
 drivers/media/rc/igorplugusb.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/rc/igorplugusb.c +3 -0
index b36e515..df37cd5 100644
@@ -191,6 +191,8 @@ static int igorplugusb_probe(struct usb_interface *intf,
 	usb_make_path(udev, ir->phys, sizeof(ir->phys));
 
 	rc = rc_allocate_device();
+	if (!rc)
+		goto fail;
 	rc->input_name = DRIVER_DESC;
 	rc->input_phys = ir->phys;
 	usb_to_input_id(udev, &rc->input_id);
@@ -213,6 +215,7 @@ static int igorplugusb_probe(struct usb_interface *intf,
 	ir->rc = rc;
 	ret = rc_register_device(rc);
 	if (ret) {
+fail:
 		dev_err(&intf->dev, "failed to register rc device: %d", ret);
 		rc_free_device(rc);
 		usb_free_urb(ir->urb);
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 3 lines · Insu Yun

Since rc_allocate_device() uses kmalloc,
it can returns NULL, so need to check,
otherwise, NULL derefenrece can be happened.

Thanks for catching that.

Quoted 10 lines
---
 drivers/media/rc/igorplugusb.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/rc/igorplugusb.c +0 -0
index b36e515..df37cd5 100644

drivers/media/rc/igorplugusb.c static int igorplugusb_probe(struct usb_interface *intf, · line 195

Quoted 5 lines
drivers/media/rc/igorplugusb.c +2 -0
@@ -191,6 +191,8 @@ static int igorplugusb_probe(struct usb_interface *intf,
 	usb_make_path(udev, ir->phys, sizeof(ir->phys));
 
 	rc = rc_allocate_device();
+	if (!rc)
+		goto fail;

At this point, ret is not initialized but will be used in the error path.

drivers/media/rc/igorplugusb.c static int igorplugusb_probe(struct usb_interface *intf, · line 217

Quoted 7 lines
drivers/media/rc/igorplugusb.c +0 -0
 	rc->input_name = DRIVER_DESC;
 	rc->input_phys = ir->phys;
 	usb_to_input_id(udev, &rc->input_id);
@@ -213,6 +215,7 @@ static int igorplugusb_probe(struct usb_interface *intf,
 	ir->rc = rc;
 	ret = rc_register_device(rc);
 	if (ret) {

I'm not sure how common it is to goto into another nesting level for an error path. Also I just noticed that the code is leaking the timer in the error path.

It might be better to put the "fail:" at the end after the last return for the successful case, and have a goto to it after both rc_allocate_device() and rc_register_device() in case they fail.

Quoted 6 lines
drivers/media/rc/igorplugusb.c +1 -0
+fail:
 		dev_err(&intf->dev, "failed to register rc device: %d", ret);
 		rc_free_device(rc);
 		usb_free_urb(ir->urb);
Signature · 1 line
1.9.1

Thanks
Sean

#2 lore raw

Em Tue, 16 Feb 2016 10:54:54 +0000
Sean Young <[email protected]> escreveu:

Quoted 19 lines · Insu Yun
Quoted 3 lines

Since rc_allocate_device() uses kmalloc,
it can returns NULL, so need to check,
otherwise, NULL derefenrece can be happened.

Thanks for catching that.

Quoted 12 lines
---
 drivers/media/rc/igorplugusb.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/rc/igorplugusb.c +0 -0
index b36e515..df37cd5 100644
@@ -191,6 +191,8 @@ static int igorplugusb_probe(struct usb_interface *intf,
 	usb_make_path(udev, ir->phys, sizeof(ir->phys));
 
Quoted 5 lines

drivers/media/rc/igorplugusb.c static int igorplugusb_probe(struct usb_interface *intf, · line 195

Quoted 3 lines
drivers/media/rc/igorplugusb.c +2 -0
 	rc = rc_allocate_device();
+	if (!rc)
+		goto fail;  

At this point, ret is not initialized but will be used in the error path.

Also, it should be setting "ret", like like:

	if (!rc) {
		ret = -ENOMEM;
		goto fail;
	}
Quoted 11 lines

drivers/media/rc/igorplugusb.c static int igorplugusb_probe(struct usb_interface *intf, · line 217

Quoted 8 lines
drivers/media/rc/igorplugusb.c +0 -0
 	rc->input_name = DRIVER_DESC;
 	rc->input_phys = ir->phys;
 	usb_to_input_id(udev, &rc->input_id);
@@ -213,6 +215,7 @@ static int igorplugusb_probe(struct usb_interface *intf,
 	ir->rc = rc;
 	ret = rc_register_device(rc);
 	if (ret) {  

I'm not sure how common it is to goto into another nesting level for an error path.

I can't remember a single case where we do that. Putting fail at the end is indeed the clearer way of handling it.

Quoted 20 lines

Also I just noticed that the code is leaking the timer in the error path.

It might be better to put the "fail:" at the end after the last return for the successful case, and have a goto to it after both rc_allocate_device() and rc_register_device() in case they fail.

Quoted 6 lines
drivers/media/rc/igorplugusb.c +1 -0
+fail:
 		dev_err(&intf->dev, "failed to register rc device: %d", ret);
 		rc_free_device(rc);
 		usb_free_urb(ir->urb);
Signature · 1 line
1.9.1  

Thanks
Sean
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Thanks,
Mauro