From bc4420a4c0cb1fa85b15d671c7ebabbe534a0196 Mon Sep 17 00:00:00 2001
From: Peiyang Wang <wangpeiyang1@huawei.com>
Date: Wed, 21 Aug 2024 10:28:27 +0800
Subject: [PATCH 16/27] hikptool: nic: avoid array boundary exceeded in
 hikp_nic_info.c

In hikp_nic_info.c, when using pf id or die id, the value should be not
bigger than the max value. Otherwise, array boundary exceeded might be
occurred.

Signed-off-by: Peiyang Wang <wangpeiyang1@huawei.com>
---
 net/nic/nic_info/hikp_nic_info.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/net/nic/nic_info/hikp_nic_info.c b/net/nic/nic_info/hikp_nic_info.c
index 992bbf0..cd07b13 100644
--- a/net/nic/nic_info/hikp_nic_info.c
+++ b/net/nic/nic_info/hikp_nic_info.c
@@ -113,6 +113,11 @@ static void hikp_nic_info_print_cur_pf(const struct bdf_t *bdf)
 	uint8_t i;
 	int ret;
 
+	if (pf_id >= HIKP_MAX_PF_NUM) {
+		HIKP_ERROR_PRINT("pf_id(%u) is invalid.\n", pf_id);
+		return;
+	}
+
 	printf("Current function: pf%u\n", pf_id);
 	printf("\t%-16s %s\n", "pf mode:",
 	       g_info_param.info.pf_info[pf_id].pf_mode ? "X86" : "ARM");
@@ -148,6 +153,11 @@ static void hikp_nic_info_print_cur_die(void)
 {
 	uint8_t i;
 
+	if (g_info_param.info.pf_num > HIKP_MAX_PF_NUM) {
+		HIKP_ERROR_PRINT("pf_num(%u) is invalid.\n", g_info_param.info.pf_num);
+		return;
+	}
+
 	printf("Current die(chip%u-die%u) info:\n",
 	       g_info_param.info.chip_id, g_info_param.info.die_id);
 	printf("revision id: %s", g_info_param.revision_id);
@@ -186,6 +196,12 @@ static bool is_bus_id_accessed(void)
 {
 	uint8_t i;
 
+	if (g_info_param.accessed_die_num >= MAX_DIE_NUM) {
+		HIKP_ERROR_PRINT("accessed_die_num(%u) is invalid.\n",
+				 g_info_param.accessed_die_num);
+		return false;
+	}
+
 	for (i = 0; i < g_info_param.accessed_die_num; i++) {
 		if (g_info_param.accessed_bus_id[i] == g_info_param.target.bdf.bus_id)
 			return true;
-- 
2.45.0.windows.1

