hid-lg4ff: add a kfree() to an error path
Subsystems (2)
PATCH 2/2 hid-lg4ff: add a kfree() to an error path
There is a small rare potential memory leak here. But it's easy to fix.
drivers/hid/hid-lg4ff.c +1 -0
@@ -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);
A message that is not in the archive (sent off-list or not yet received): CAMvewQJigdY81gPrpndqV=FxN+cEeif4NkzSbBZKCO4z3guQew@mail.gmail.com
Quoted 2 lines · Zic Rim
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v2 2/2 hid-lg4ff: add a kfree() to an error path
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().
drivers/hid/hid-lg4ff.c +2 -2
@@ -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;
Jiri Kosina
AppliedQuoted 15 lines · Dan Carpenter
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 <[email protected]>drivers/hid/hid-lg4ff.c +0
-1@@ -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 );
drivers/hid/hid-lg4ff.c int lg4ff_init(struct hid_device *hid) · line 446
Quoted 10 lines
drivers/hid/hid-lg4ff.c +2
-1+ 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.
Signature · 2 lines
Jiri Kosina SUSE Labs