Skip to contentSkip to conversation

hid-lg4ff: add a kfree() to an error path

PATCH Applied Dan Carpenter 2 participants 4 messages Aug 18, 2011 – Aug 23, 2011 2 messages not in the archive

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

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
index dc38c2d..2b4bbcb 100644
@@ -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

#2 lore raw
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]

#4 lore raw

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
index dc38c2d..cbb0089 100644
@@ -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
#5 lore raw
Quoted 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().

drivers/hid/hid-lg4ff.c +0 -1
index dc38c2d..cbb0089 100644
@@ -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