linux-ide.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] ata: libata: Fix memory leak for error path in ata_host_alloc()
@ 2024-08-22  3:30 Zheng Qixing
  2024-08-22  3:37 ` Yu Kuai
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Zheng Qixing @ 2024-08-22  3:30 UTC (permalink / raw)
  To: dlemoal, cassel
  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>
---
Changes in v2:
 - error path is wrong in v1

 drivers/ata/libata-core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index e4023fc288ac..f27a18990c38 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -5663,8 +5663,10 @@ struct ata_host *ata_host_alloc(struct device *dev, int n_ports)
 	}
 
 	dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
-	if (!dr)
+	if (!dr) {
+		kfree(host);
 		goto err_out;
+	}
 
 	devres_add(dev, dr);
 	dev_set_drvdata(dev, host);
-- 
2.39.2


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

end of thread, other threads:[~2024-08-27  1:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-22  3:30 [PATCH v2] ata: libata: Fix memory leak for error path in ata_host_alloc() Zheng Qixing
2024-08-22  3:37 ` Yu Kuai
2024-08-23  1:10 ` Damien Le Moal
2024-08-26  2:49   ` Zheng Qixing
2024-08-26  4:02     ` Damien Le Moal
2024-08-27  1:46       ` Zheng Qixing
2024-08-23 10:12 ` Niklas Cassel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).