diff mbox

[2/2,-next] hid-lg4ff: add a kfree() to an error path

Message ID 20110818134343.GB21927@shale.localdomain (mailing list archive)
State New, archived
Delegated to: Jiri Kosina
Headers show

Commit Message

Dan Carpenter Aug. 18, 2011, 1:43 p.m. UTC
There is a small rare potential memory leak here.  But it's easy to
fix.

Signed-off-by: Dan Carpenter <error27@gmail.com>

--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Dan Carpenter Aug. 18, 2011, 4:24 p.m. UTC | #1
On Thu, Aug 18, 2011 at 07:59:32PM +0530, Zic Rim wrote:
> There is no leak here. If kzalloc fails , it means memory is not allocated.
> It is wrong to free something that is not allocated .

No.  The entry allocation succeeded and the entry->device_id
allocation failed.  Look again.

regards,
dan carpenter

--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jiri Kosina Aug. 23, 2011, 8:46 a.m. UTC | #2
On Fri, 19 Aug 2011, Dan Carpenter wrote:

> There is a small rare potential memory leak here.  Also Walter Harms
> points out that we can do a small cleanup as well by using kstrdup().
> 
> Signed-off-by: Dan Carpenter <error27@gmail.com>
> 
> diff --git a/drivers/hid/hid-lg4ff.c b/drivers/hid/hid-lg4ff.c
> index dc38c2d..cbb0089 100644
> --- a/drivers/hid/hid-lg4ff.c
> +++ b/drivers/hid/hid-lg4ff.c
> @@ -435,12 +435,12 @@ int lg4ff_init(struct hid_device *hid)
>  		hid_err(hid, "Cannot add device, insufficient memory.\n");
>  		return -ENOMEM;
>  	}
> -	entry->device_id = (char *)kzalloc(strlen((&hid->dev)->kobj.name) + 1, GFP_KERNEL);
> +	entry->device_id = kstrdup((&hid->dev)->kobj.name, GFP_KERNEL);
>  	if (!entry->device_id) {
>  		hid_err(hid, "Cannot set device_id, insufficient memory.\n");
> +		kfree(entry);
>  		return -ENOMEM;
>  	}
> -	strcpy(entry->device_id, (&hid->dev)->kobj.name);
>  	entry->min_range = lg4ff_devices[i].min_range;
>  	entry->max_range = lg4ff_devices[i].max_range;
>  	entry->set_range = lg4ff_devices[i].set_range;
> 

Applied, thanks.
diff mbox

Patch

diff --git a/drivers/hid/hid-lg4ff.c b/drivers/hid/hid-lg4ff.c
index dc38c2d..2b4bbcb 100644
--- a/drivers/hid/hid-lg4ff.c
+++ b/drivers/hid/hid-lg4ff.c
@@ -438,6 +438,7 @@  int lg4ff_init(struct hid_device *hid)
 	entry->device_id = (char *)kzalloc(strlen((&hid->dev)->kobj.name) + 1, GFP_KERNEL);
 	if (!entry->device_id) {
 		hid_err(hid, "Cannot set device_id, insufficient memory.\n");
+		kfree(entry);
 		return -ENOMEM;
 	}
 	strcpy(entry->device_id, (&hid->dev)->kobj.name);