ALSA: pci: Fix memory leak in snd_korg1212_create
Subsystems (3)
PATCH 1/1 ALSA: pci: Fix memory leak in snd_korg1212_create
In the implementation of snd_korg1212_create() the allocated memory for korg1212 is leaked in cases of error. Release korg1212 via snd_korg1212_free() if either of these calls fail:snd_korg1212_downloadDSPCode(), snd_pcm_new(), or snd_ctl_add().
--- sound/pci/korg1212/korg1212.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
sound/pci/korg1212/korg1212.c +10 -3
@@ -2367,8 +2367,10 @@ static int snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,mdelay (CARD_BOOT_DELAY_IN_MS ); -if (snd_korg1212_downloadDSPCode (korg1212)) +if (snd_korg1212_downloadDSPCode (korg1212)) { +snd_korg1212_free (korg1212);return -EBUSY ; + }K1212_DEBUG_PRINTK ("korg1212: dspMemPhy = %08x U[%08x], " "PlayDataPhy = %08x L[%08x]\n"
@@ -2383,8 +2385,11 @@ static int snd_korg1212_create(struct snd_card *card, struct pci_dev *pci, korg1212->RoutingTablePhy,LowerWordSwap (korg1212->RoutingTablePhy), korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy)); -if ((err =snd_pcm_new (korg1212->card,"korg1212" ,0 ,1 ,1 , &korg1212->pcm)) <0 ) + err =snd_pcm_new (korg1212->card,"korg1212" ,0 ,1 ,1 , &korg1212->pcm); +if (err <0 ) { +snd_korg1212_free (korg1212);return err; + } korg1212->pcm->private_data = korg1212; korg1212->pcm->private_free = snd_korg1212_free_pcm;
@@ -2398,8 +2403,10 @@ static int snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,for (i =0 ; i <ARRAY_SIZE (snd_korg1212_controls); i++) { err =snd_ctl_add (korg1212->card,snd_ctl_new1 (&snd_korg1212_controls[i], korg1212)); -if (err <0 ) +if (err <0 ) { +snd_korg1212_free (korg1212);return err; + } }snd_korg1212_proc_init (korg1212);
Signature · 1 line
2.17.1
…
Quoted 1 line
+++
b/sound/pci/korg1212/korg1212.c
…
sound/pci/korg1212/korg1212.c static int snd_korg1212_create(struct snd_card *card, struct pci_dev *pci, · line 2409
Quoted 9 lines
sound/pci/korg1212/korg1212.c +3
-1@@ -2398,8 +2403,10 @@ static int snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,for (i =0 ; i <ARRAY_SIZE (snd_korg1212_controls); i++) { err =snd_ctl_add (korg1212->card,snd_ctl_new1 (&snd_korg1212_controls[i], korg1212)); -if (err <0 ) +if (err <0 ) { +snd_korg1212_free (korg1212);return err; + }
I suggest to add a jump target according to the Linux coding style so that duplicate exception handling code can be reduced.
return 0;
+free_korg:
+ snd_korg1212_free(korg1212);
+ return err;
}
Regards,
Markus
Quoted 2 lines
I suggest to add a jump target according to the Linux coding style so that duplicate exception handling code can be reduced.
Do you find any information interesting from the update suggestion “ALSA: korg1212: Use common error handling code in two functions”?
https://lore.kernel.org/alsa-devel/[email protected]/
https://lore.kernel.org/patchwork/patch/851723/
https://lkml.org/lkml/2017/11/16/193
Regards,
Markus
Quoted 5 lines · Navid Emamdoost
In the implementation of
snd_korg1212_create()the allocated memory for korg1212 is leaked in cases of error. Release korg1212 viasnd_korg1212_free()if either of these calls fail:snd_korg1212_downloadDSPCode(),snd_pcm_new(), orsnd_ctl_add().
This also leads to the double-free. The code path is after snd_device_new() which has its own destructor callback.
thanks,
Takashi
Quoted 50 lines
Signed-off-by Navid Emamdoost <[email protected]>--- sound/pci/korg1212/korg1212.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)sound/pci/korg1212/korg1212.c +10
-3@@ -2367,8 +2367,10 @@ static int snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,mdelay (CARD_BOOT_DELAY_IN_MS ); -if (snd_korg1212_downloadDSPCode (korg1212)) +if (snd_korg1212_downloadDSPCode (korg1212)) { +snd_korg1212_free (korg1212);return -EBUSY ; + }K1212_DEBUG_PRINTK ("korg1212: dspMemPhy = %08x U[%08x], " "PlayDataPhy = %08x L[%08x]\n"@@ -2383,8 +2385,11 @@ static int snd_korg1212_create(struct snd_card *card, struct pci_dev *pci, korg1212->RoutingTablePhy,LowerWordSwap (korg1212->RoutingTablePhy), korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy)); -if ((err =snd_pcm_new (korg1212->card,"korg1212" ,0 ,1 ,1 , &korg1212->pcm)) <0 ) + err =snd_pcm_new (korg1212->card,"korg1212" ,0 ,1 ,1 , &korg1212->pcm); +if (err <0 ) { +snd_korg1212_free (korg1212);return err; + } korg1212->pcm->private_data = korg1212; korg1212->pcm->private_free = snd_korg1212_free_pcm;@@ -2398,8 +2403,10 @@ static int snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,for (i =0 ; i <ARRAY_SIZE (snd_korg1212_controls); i++) { err =snd_ctl_add (korg1212->card,snd_ctl_new1 (&snd_korg1212_controls[i], korg1212)); -if (err <0 ) +if (err <0 ) { +snd_korg1212_free (korg1212);return err; + } }snd_korg1212_proc_init (korg1212);Signature · 1 line
2.17.1
Re: [alsa-devel] ALSA: korg1212: Checking exception handling in snd_korg1212_create()
Quoted 1 line
The code path is after
snd_device_new()which has its own destructor callback.
Thanks for your reminder.
Can the properties of this programming interface be documented also together with the function declaration for the safer handling of ALSA components?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/sound/kernel-api/writing-an-alsa-driver.rst?idÖd5df1db6e9d7f8f76d2911707f7d5877251b02#n567
Can any more API information help to improve automatic source code analysis around similar functions?
Regards,
Markus
Re: [alsa-devel] ALSA: korg1212: Checking exception handling in snd_korg1212_create()
Quoted 8 lines · Markus Elfring
Quoted 2 lines
The code path is after
snd_device_new()which has its own destructor callback.Thanks for your reminder.
Can the properties of this programming interface be documented also together with the function declaration for the safer handling of ALSA components?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/sound/kernel-api/writing-an-alsa-driver.rst?idÖd5df1db6e9d7f8f76d2911707f7d5877251b02#n567
I can think of only adding some comment at each point mentioning that the resource will be managed by the device destructor.
Quoted 2 lines
Can any more API information help to improve automatic source code analysis around similar functions?
If anything we can add, let me know.
Takashi
Re: [alsa-devel] ALSA: korg1212: Checking exception handling in snd_korg1212_create()
Quoted 6 lines
Quoted 3 lines
Can the properties of this programming interface be documented also together with the function declaration for the safer handling of ALSA components?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/sound/kernel-api/writing-an-alsa-driver.rst?id=d6d5df1db6e9d7f8f76d2911707f7d5877251b02#n567I can think of only adding some comment at each point mentioning that the resource will be managed by the device destructor.
* Which places will be corresponding update candidates?
* Would you like to choose the wording?
Quoted 4 lines
Quoted 2 lines
Can any more API information help to improve automatic source code analysis around similar functions?
If anything we can add, let me know.
Will any tags become more helpful also for the software documentation?
Regards,
Markus
Re: [alsa-devel] ALSA: korg1212: Checking exception handling in snd_korg1212_create()
Quoted 9 lines · Markus Elfring
Quoted 7 lines
Quoted 4 lines
Can the properties of this programming interface be documented also together with the function declaration for the safer handling of ALSA components?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/sound/kernel-api/writing-an-alsa-driver.rst?idÖd5df1db6e9d7f8f76d2911707f7d5877251b02#n567I can think of only adding some comment at each point mentioning that the resource will be managed by the device destructor.
* Which places will be corresponding update candidates?
At each place where this kind of code is seen.
Quoted 1 line
* Would you like to choose the wording?
I myself don't mind as long as it's clearly understandable for human being.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Can any more API information help to improve automatic source code analysis around similar functions?
If anything we can add, let me know.
Will any tags become more helpful also for the software documentation?
It's my question.
thanks,
Takashi