mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ata:Fix memory leak for error path in ata_host_alloc()
@ 2024-08-22  3:17 Zheng Qixing
  2024-08-22  3:29 ` Zheng Qixing
  0 siblings, 1 reply; 2+ messages in thread
From: Zheng Qixing @ 2024-08-22  3:17 UTC (permalink / raw)
  To: dlemoal
  Cc: linux-ide, linux-kernel, yukuai3, yi.zhang, yangerkun, zhengqixing

From: Zheng Qixing <zhengqixing@huawei.com>

In ata_host_alloc(), if ata_port_alloc(host) fails to allocate memory
for a port, the allocated 'host' structure is not freed before returning
from the function. This results in a potential memory leak.

This patch adds a kfree(host) before the error handling code is executed
to ensure that the 'host' structure is properly freed in case of an
allocation failure.

Signed-off-by: Zheng Qixing <zhengqixing@huawei.com>
---
 drivers/ata/libata-core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index ee18c09c39b6..cb3ace759699 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -5610,9 +5610,10 @@ struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
 		struct ata_port *ap;
 
 		ap = ata_port_alloc(host);
-		if (!ap)
+		if (!ap) {
 			kfree(host);
 			goto err_out;
+		}
 
 		ap->port_no = i;
 		host->ports[i] = ap;
-- 
2.39.2


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

* Re: [PATCH] ata:Fix memory leak for error path in ata_host_alloc()
  2024-08-22  3:17 [PATCH] ata:Fix memory leak for error path in ata_host_alloc() Zheng Qixing
@ 2024-08-22  3:29 ` Zheng Qixing
  0 siblings, 0 replies; 2+ messages in thread
From: Zheng Qixing @ 2024-08-22  3:29 UTC (permalink / raw)
  To: Zheng Qixing, dlemoal
  Cc: linux-ide, linux-kernel, yukuai3, yi.zhang, yangerkun


在 2024/8/22 11:17, Zheng Qixing 写道:
> From: Zheng Qixing <zhengqixing@huawei.com>
>
> In ata_host_alloc(), if ata_port_alloc(host) fails to allocate memory
> for a port, the allocated 'host' structure is not freed before returning
> from the function. This results in a potential memory leak.
>
> This patch adds a kfree(host) before the error handling code is executed
> to ensure that the 'host' structure is properly freed in case of an
> allocation failure.
>
> Signed-off-by: Zheng Qixing <zhengqixing@huawei.com>
> ---
>   drivers/ata/libata-core.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
> index ee18c09c39b6..cb3ace759699 100644
> --- a/drivers/ata/libata-core.c
> +++ b/drivers/ata/libata-core.c
> @@ -5610,9 +5610,10 @@ struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
>   		struct ata_port *ap;
>   
>   		ap = ata_port_alloc(host);
> -		if (!ap)
> +		if (!ap) {
>   			kfree(host);
>   			goto err_out;
> +		}
>   
>   		ap->port_no = i;
>   		host->ports[i] = ap;

Sorry, this patch is wrong, i will send a new version.


Zheng Qixing


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

end of thread, other threads:[~2024-08-22  3:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-22  3:17 [PATCH] ata:Fix memory leak for error path in ata_host_alloc() Zheng Qixing
2024-08-22  3:29 ` Zheng Qixing

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

all inboxes | Powered by JetHome®