diff mbox series

ide: pci: Fix memleak in ide_pci_init_two

Message ID 20201220070541.7515-1-dinghao.liu@zju.edu.cn
State New
Headers show
Series ide: pci: Fix memleak in ide_pci_init_two | expand

Commit Message

Dinghao Liu Dec. 20, 2020, 7:05 a.m. UTC
When do_ide_setup_pci_device() fails, host allocated
by ide_host_alloc() may not have been freed, which
leads to memleak.

Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
---
 drivers/ide/setup-pci.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Kari Argillander Dec. 20, 2020, 7:50 p.m. UTC | #1
On Sun, Dec 20, 2020 at 03:05:40PM +0800, Dinghao Liu wrote:
> When do_ide_setup_pci_device() fails, host allocated
> by ide_host_alloc() may not have been freed, which
> leads to memleak.
> 
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> ---
>  drivers/ide/setup-pci.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/ide/setup-pci.c b/drivers/ide/setup-pci.c
> index fdc8e813170c..c7da5368fcd4 100644
> --- a/drivers/ide/setup-pci.c
> +++ b/drivers/ide/setup-pci.c
> @@ -586,7 +586,7 @@ int ide_pci_init_two(struct pci_dev *dev1, struct pci_dev *dev2,
>  		 * do_ide_setup_pci_device() on the first device!
>  		 */
>  		if (ret < 0)
> -			goto out_free_bars;
> +			goto out_free_host;
>  
>  		/* fixup IRQ */
>  		if (ide_pci_is_in_compatibility_mode(pdev[i])) {
> @@ -597,11 +597,11 @@ int ide_pci_init_two(struct pci_dev *dev1, struct pci_dev *dev2,
>  	}
>  
>  	ret = ide_host_register(host, d, hws);
> -	if (ret)
> -		ide_host_free(host);
> -	else
> +	if (!ret)
>  		goto out;

Maybe 
	if (ret)
		goto out_free_host;

	return 0;

would be more clear here. But this is just small nit.

>  
> +out_free_host:
> +	ide_host_free(host);
>  out_free_bars:
>  	i = n_ports / 2;
>  	while (i--)
Dinghao Liu Dec. 23, 2020, 8:59 a.m. UTC | #2
> On Sun, Dec 20, 2020 at 03:05:40PM +0800, Dinghao Liu wrote:
> > When do_ide_setup_pci_device() fails, host allocated
> > by ide_host_alloc() may not have been freed, which
> > leads to memleak.
> > 
> > Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> > ---
> >  drivers/ide/setup-pci.c | 8 ++++----
> >  1 file changed, 4 insertions(+), 4 deletions(-)
> > 
> > diff --git a/drivers/ide/setup-pci.c b/drivers/ide/setup-pci.c
> > index fdc8e813170c..c7da5368fcd4 100644
> > --- a/drivers/ide/setup-pci.c
> > +++ b/drivers/ide/setup-pci.c
> > @@ -586,7 +586,7 @@ int ide_pci_init_two(struct pci_dev *dev1, struct pci_dev *dev2,
> >  		 * do_ide_setup_pci_device() on the first device!
> >  		 */
> >  		if (ret < 0)
> > -			goto out_free_bars;
> > +			goto out_free_host;
> >  
> >  		/* fixup IRQ */
> >  		if (ide_pci_is_in_compatibility_mode(pdev[i])) {
> > @@ -597,11 +597,11 @@ int ide_pci_init_two(struct pci_dev *dev1, struct pci_dev *dev2,
> >  	}
> >  
> >  	ret = ide_host_register(host, d, hws);
> > -	if (ret)
> > -		ide_host_free(host);
> > -	else
> > +	if (!ret)
> >  		goto out;
> 
> Maybe 
> 	if (ret)
> 		goto out_free_host;
> 
> 	return 0;
> 
> would be more clear here. But this is just small nit.
> 
> >  
> > +out_free_host:
> > +	ide_host_free(host);
> >  out_free_bars:
> >  	i = n_ports / 2;
> >  	while (i--)

Thank you for your advice and I will send a new patch soon.

Regards,
Dinghao
diff mbox series

Patch

diff --git a/drivers/ide/setup-pci.c b/drivers/ide/setup-pci.c
index fdc8e813170c..c7da5368fcd4 100644
--- a/drivers/ide/setup-pci.c
+++ b/drivers/ide/setup-pci.c
@@ -586,7 +586,7 @@  int ide_pci_init_two(struct pci_dev *dev1, struct pci_dev *dev2,
 		 * do_ide_setup_pci_device() on the first device!
 		 */
 		if (ret < 0)
-			goto out_free_bars;
+			goto out_free_host;
 
 		/* fixup IRQ */
 		if (ide_pci_is_in_compatibility_mode(pdev[i])) {
@@ -597,11 +597,11 @@  int ide_pci_init_two(struct pci_dev *dev1, struct pci_dev *dev2,
 	}
 
 	ret = ide_host_register(host, d, hws);
-	if (ret)
-		ide_host_free(host);
-	else
+	if (!ret)
 		goto out;
 
+out_free_host:
+	ide_host_free(host);
 out_free_bars:
 	i = n_ports / 2;
 	while (i--)