Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] sound:Fix a memory leak related to variable data
@ 2024-03-23  8:04 LuMingYin
  2024-03-23  9:29 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: LuMingYin @ 2024-03-23  8:04 UTC (permalink / raw)
  To: linux-sound, linux-kernel; +Cc: tiwai, perex, minhuadotchen, LuMingYin

In the file /linux/sound/core/control_compat.c, a pointer named 'data' is defined at line 82. This pointer allocates a block of dynamic memory using the 'kzalloc' function at line 85. When the if statement at line 86 returns false, it indicates successful allocation of the dynamic memory area pointed to by 'data'. However, when the if statement at line 90 returns true, the program returns at line 91. During this process, the dynamic memory area pointed to by 'data' is neither used as in the switch statement at line 108 nor deallocated, leading to a memory leak bug. The if statement at line 95 also has the same issue. This commit fixes this problem.

Signed-off-by: LuMingYin <lumingyindetect@163.com>
---
 sound/core/control_compat.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/sound/core/control_compat.c b/sound/core/control_compat.c
index 934bb945e702..32113eb06f68 100644
--- a/sound/core/control_compat.c
+++ b/sound/core/control_compat.c
@@ -87,13 +87,19 @@ static int snd_ctl_elem_info_compat(struct snd_ctl_file *ctl,
 		return -ENOMEM;
 
 	/* copy id */
-	if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
+	if (copy_from_user(&data->id, &data32->id, sizeof(data->id))){
+		kfree(data);
+		data = NULL;
 		return -EFAULT;
+	}
 	/* we need to copy the item index.
 	 * hope this doesn't break anything..
 	 */
-	if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
+	if (get_user(data->value.enumerated.item, &data32->value.enumerated.item)){
+		kfree(data);
+		data = NULL;
 		return -EFAULT;
+	}
 
 	err = snd_ctl_elem_info(ctl, data);
 	if (err < 0)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-23  9:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-23  8:04 [PATCH] sound:Fix a memory leak related to variable data LuMingYin
2024-03-23  9:29 ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox