Skip to contentSkip to conversation

usb: gadget: f_tcm: memory leak on error in tcm_alloc_inst()

PATCH In review Dan Carpenter 2 participants 2 messages Dec 9, 2015 – Dec 10, 2015

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

PATCH 1/1 usb: gadget: f_tcm: memory leak on error in tcm_alloc_inst()

We need to kfree(opts) on error. Also it's nicer to allocate opts before taking the lock.

drivers/usb/gadget/function/f_tcm.c +5 -6
index ed47e40..0a26a2e 100644
@@ -2300,20 +2300,19 @@ static struct usb_function_instance *tcm_alloc_inst(void)
 	struct f_tcm_opts *opts;
 	int i;
 
-	mutex_lock(&tpg_instances_lock);
-	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
-	if (!opts) {
-		mutex_unlock(&tpg_instances_lock);
 
+	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+	if (!opts)
 		return ERR_PTR(-ENOMEM);
-	}
+
+	mutex_lock(&tpg_instances_lock);
 	for (i = 0; i < TPG_INSTANCES; ++i)
 		if (!tpg_instances[i].func_inst)
 			break;
 
 	if (i = TPG_INSTANCES) {
 		mutex_unlock(&tpg_instances_lock);
-
+		kfree(opts);
 		return ERR_PTR(-EBUSY);
 	}
 	tpg_instances[i].func_inst = &opts->func_inst;
#1 lore raw
Quoted 4 lines · Dan Carpenter

We need to kfree(opts) on error. Also it's nicer to allocate opts before taking the lock.

what's the base for this commit ? I don't have f_tcm.c anywhere.

Signature · 1 line
balbi