[PATCH -next] ASoC: SOF: topology: Fix memory leak in sof_control_load()

From: Yu Liao
Date: Thu Mar 17 2022 - 22:07:34 EST


scontrol doesn't get freed when kstrdup returns NULL.
Fix by free iscontrol in that case.

scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
if (!scontrol)
return -ENOMEM;

scontrol->name = kstrdup(hdr->name, GFP_KERNEL);
if (!scontrol->name)
return -ENOMEM;

Signed-off-by: Yu Liao <liaoyu15@xxxxxxxxxx>
---
sound/soc/sof/topology.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/sound/soc/sof/topology.c b/sound/soc/sof/topology.c
index 367fbe2d5b31..88f34f43d693 100644
--- a/sound/soc/sof/topology.c
+++ b/sound/soc/sof/topology.c
@@ -905,8 +905,10 @@ static int sof_control_load(struct snd_soc_component *scomp, int index,
return -ENOMEM;

scontrol->name = kstrdup(hdr->name, GFP_KERNEL);
- if (!scontrol->name)
+ if (!scontrol->name) {
+ kfree(scontrol);
return -ENOMEM;
+ }

scontrol->scomp = scomp;
scontrol->access = kc->access;
--
2.25.1