Skip to contentSkip to conversation

firmware: Fix memory leak in error path

PATCH Superseded Firo Yang 2 participants 2 messages Jun 6, 2015

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Firo Yang

author
#0 lore raw

PATCH v2 1/1 firmware: Fix memory leak in error path

Eliminate a memory leak by unifying the error handling code at the end of the function.

---
 drivers/base/firmware_class.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
drivers/base/firmware_class.c +14 -5
index 8c3aa3c..bf06fb2 100644
@@ -1299,6 +1299,7 @@ request_firmware_nowait(
 	const char *name, struct device *device, gfp_t gfp, void *context,
 	void (*cont)(const struct firmware *fw, void *context))
 {
+	int rc;
 	struct firmware_work *fw_work;
 
 	fw_work = kzalloc(sizeof(struct firmware_work), gfp);
@@ -1307,8 +1308,11 @@ request_firmware_nowait(
 
 	fw_work->module = module;
 	fw_work->name = kstrdup_const(name, gfp);
-	if (!fw_work->name)
-		return -ENOMEM;
+	if (!fw_work->name) {
+		rc = -ENOMEM;
+		goto err_dup;
+	}
+
 	fw_work->device = device;
 	fw_work->context = context;
 	fw_work->cont = cont;
@@ -1316,15 +1320,20 @@ request_firmware_nowait(
 		(uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
 
 	if (!try_module_get(module)) {
-		kfree_const(fw_work->name);
-		kfree(fw_work);
-		return -EFAULT;
+		rc = -EFAULT;
+		goto err_module;
 	}
 
 	get_device(fw_work->device);
 	INIT_WORK(&fw_work->work, request_firmware_work_func);
 	schedule_work(&fw_work->work);
 	return 0;
+
+err_module:
+	kfree_const(fw_work->name);
+err_dup:
+	kfree(fw_work);
+	return rc;
 }
 EXPORT_SYMBOL(request_firmware_nowait);
 
Signature · 1 line
2.4.2
#1 lore raw
Quoted 41 lines · Firo Yang

Eliminate a memory leak by unifying the error handling code at the end of the function.

---
 drivers/base/firmware_class.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
drivers/base/firmware_class.c +6 -5
index 8c3aa3c..bf06fb2 100644
@@ -1299,6 +1299,7 @@ request_firmware_nowait(
 	const char *name, struct device *device, gfp_t gfp, void *context,
 	void (*cont)(const struct firmware *fw, void *context))
 {
+	int rc;
 	struct firmware_work *fw_work;
 
 	fw_work = kzalloc(sizeof(struct firmware_work), gfp);
@@ -1307,8 +1308,11 @@ request_firmware_nowait(
 
 	fw_work->module = module;
 	fw_work->name = kstrdup_const(name, gfp);
-	if (!fw_work->name)
-		return -ENOMEM;
+	if (!fw_work->name) {
+		rc = -ENOMEM;
+		goto err_dup;
+	}
+
 	fw_work->device = device;
 	fw_work->context = context;
 	fw_work->cont = cont;
@@ -1316,15 +1320,20 @@ request_firmware_nowait(
 		(uevent ? FW_OPT_UEVENT : FW_OPT_USERHELPER);
 
 	if (!try_module_get(module)) {
-		kfree_const(fw_work->name);
-		kfree(fw_work);
-		return -EFAULT;

drivers/base/firmware_class.c request_firmware_nowait( · line 1332

Quoted 10 lines
drivers/base/firmware_class.c +4 -0
+		rc = -EFAULT;
+		goto err_module;
 	}
 
 	get_device(fw_work->device);
 	INIT_WORK(&fw_work->work, request_firmware_work_func);
 	schedule_work(&fw_work->work);
 	return 0;
+
+err_module:

It is considered to be safer that the label reflect what happens at the destination, and not the reason for reaching the label.

julia

Quoted 15 lines
drivers/base/firmware_class.c +4 -0
+	kfree_const(fw_work->name);
+err_dup:
+	kfree(fw_work);
+	return rc;
 }
 EXPORT_SYMBOL(request_firmware_nowait);
 
Signature · 6 lines
2.4.2

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