From e4e26428428e54c5d4a8d56e960646211b94bda6 Mon Sep 17 00:00:00 2001
From: Dengdui Huang <huangdengdui@huawei.com>
Date: Tue, 8 Apr 2025 16:30:55 +0800
Subject: [PATCH] net/hns3: fix memory leakage in failure path

[ upstream commit 11110038e09dac9a2db32032852020d40e5d125d ]

When the hns3_dfx_reg_fetch_data() function returns from processing
failure, cmd_descs is not freed, which leads to leakage.
This patch fit it.

By the way, this patch uses calloc to apply for heap memory instead
of hugepage memory.

Fixes: ef1fbd355451 ("net/hns3: add more registers to dump")
Cc: stable@dpdk.org

Signed-off-by: Dengdui Huang <huangdengdui@huawei.com>
---
 drivers/net/hns3/hns3_regs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/net/hns3/hns3_regs.c b/drivers/net/hns3/hns3_regs.c
index 37ac957..796e1b3 100644
--- a/drivers/net/hns3/hns3_regs.c
+++ b/drivers/net/hns3/hns3_regs.c
@@ -1270,7 +1270,7 @@ hns3_get_dfx_regs(struct hns3_hw *hw, struct rte_dev_reg_info *regs, uint32_t mo
 	for (i = 0; i < opcode_num; i++)
 		max_bd_num = RTE_MAX(bd_num_list[i], max_bd_num);
 
-	cmd_descs = rte_zmalloc(NULL, sizeof(*cmd_descs) * max_bd_num, 0);
+	cmd_descs = calloc(max_bd_num, sizeof(*cmd_descs));
 	if (cmd_descs == NULL)
 		return -ENOMEM;
 
@@ -1290,13 +1290,14 @@ hns3_get_dfx_regs(struct hns3_hw *hw, struct rte_dev_reg_info *regs, uint32_t mo
 		if (regs_num !=  hns3_reg_lists[i].entry_num) {
 			hns3_err(hw, "Query register number differ from the list for module %s!",
 				 hns3_get_name_by_module(i));
+			free(cmd_descs);
 			return -EINVAL;
 		}
 		hns3_fill_dfx_regs_name(hw, regs, hns3_reg_lists[i].reg_list, regs_num);
 		regs->length += regs_num;
 		data += regs_num;
 	}
-	rte_free(cmd_descs);
+	free(cmd_descs);
 
 	return ret;
 }
-- 
2.25.1

