Skip to contentSkip to conversation

pcm030-audio-fabric: Fix a memory leaking bug in pcm030_fabric_probe()

PATCH In review Gen Zhang 2 participants 2 messages May 24, 2019

Subsystems (2)
lore
Share & tools
lore

Gen Zhang

author
#0 lore raw

PATCH 1/1 pcm030-audio-fabric: Fix a memory leaking bug in pcm030_fabric_probe()

In pcm030_fabric_probe(), 'pdata->codec_device' is allocated by platform_device_alloc(). When this allocation fails, ENOMEM is returned. However, 'pdata' is allocated by devm_kzalloc() before this site. We should free 'pdata' before function ends to prevent memory leaking.

Similarly, we should free 'pdata' when 'pdata->codec_device' is NULL. And we should free 'pdata->codec_device' and 'pdata' when 'ret' is error to prevent memory leaking.

---

sound/soc/fsl/pcm030-audio-fabric.c +19 -5
index a7fe4ad..d2e6eae 100644
@@ -72,29 +72,43 @@ static int pcm030_fabric_probe(struct platform_device *op)
 	platform_np = of_parse_phandle(np, "asoc-platform", 0);
 	if (!platform_np) {
 		dev_err(&op->dev, "ac97 not registered\n");
-		return -ENODEV;
+		ret = -ENODEV;
+		goto out_free1;
 	}
 
 	for_each_card_prelinks(card, i, dai_link)
 		dai_link->platform_of_node = platform_np;
 
 	ret = request_module("snd-soc-wm9712");
-	if (ret)
+	if (ret) {
 		dev_err(&op->dev, "request_module returned: %d\n", ret);
+		goto out_free1;
+	}
 
 	pdata->codec_device = platform_device_alloc("wm9712-codec", -1);
-	if (!pdata->codec_device)
+	if (!pdata->codec_device) {
 		dev_err(&op->dev, "platform_device_alloc() failed\n");
+		ret = -ENOMEM;
+		goto out_free1;
+	}
 
 	ret = platform_device_add(pdata->codec_device);
-	if (ret)
+	if (ret) {
 		dev_err(&op->dev, "platform_device_add() failed: %d\n", ret);
+		goto out_free2;
+	}
 
 	ret = snd_soc_register_card(card);
-	if (ret)
+	if (ret) {
 		dev_err(&op->dev, "snd_soc_register_card() failed: %d\n", ret);
+		goto out_free2;
+	}
 
 	platform_set_drvdata(op, pdata);
+out_free2:
+	platform_device_put(pdata->codec_device);
+out_free1:
+	devm_kfree(&op->dev, pdata);
 
 	return ret;
 }

---

#1 lore raw
Quoted 10 lines · Gen Zhang

In pcm030_fabric_probe(), 'pdata->codec_device' is allocated by platform_device_alloc(). When this allocation fails, ENOMEM is returned. However, 'pdata' is allocated by devm_kzalloc() before this site. We should free 'pdata' before function ends to prevent memory leaking.

Similarly, we should free 'pdata' when 'pdata->codec_device' is NULL. And we should free 'pdata->codec_device' and 'pdata' when 'ret' is error to prevent memory leaking.

I have seen several of these patches now, and this is not correct. I think you need to understand how devm_kzalloc() works.

Jon

Signature · 1 line
nvpublic