From e6127d30663daef7cb847265006b592eebbfc152 Mon Sep 17 00:00:00 2001
From: Peiyang Wang <wangpeiyang1@huawei.com>
Date: Tue, 20 Aug 2024 17:02:33 +0800
Subject: [PATCH 13/27] hikptool: nic: check path before used it in function
 hikp_get_dir_name_of_device

check path before used it in hikp_get_dir_name_of_device

Signed-off-by: Peiyang Wang <wangpeiyang1@huawei.com>
---
 net/hikp_net_lib.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/net/hikp_net_lib.c b/net/hikp_net_lib.c
index 32b278b..4e1e305 100644
--- a/net/hikp_net_lib.c
+++ b/net/hikp_net_lib.c
@@ -238,13 +238,17 @@ int get_revision_id_by_bdf(const struct bdf_t *bdf, char *revision_id, size_t id
 
 static int hikp_get_dir_name_of_device(const char *path, size_t len, char *dir_name)
 {
+	char file_path[PATH_MAX] = { 0 }; /* PATH_MAX includes the \0 so +1 is not required */
 	struct dirent *ptr;
 	DIR *dir = NULL;
 
-	if (len > PCI_MAX_DIR_NAME_LEN)
+	if (len > PCI_MAX_DIR_NAME_LEN || strlen(path) > PATH_MAX)
 		return -EINVAL;
 
-	dir = opendir(path);
+	if (!realpath(path, file_path))
+		return -errno;
+
+	dir = opendir(file_path);
 	if (dir == NULL) {
 		HIKP_ERROR_PRINT("read path %s fail.\n", path);
 		return -EINVAL;
-- 
2.45.0.windows.1

