From 19c524e4be9fcd9af5145dde9e11725e4c7eb1c2 Mon Sep 17 00:00:00 2001
From: Wenkai Lin <linwenkai6@hisilicon.com>
Date: Sat, 23 Jul 2022 16:50:34 +0800
Subject: [PATCH 167/183] uadk: fix atoi usage

atoi is called, use the functions like strtol
to transform string to integer instead.

Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
---
 v1/wd.c      | 23 +++++++++++++----------
 wd_mempool.c |  4 ++--
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/v1/wd.c b/v1/wd.c
index 2c435ba..6e081e6 100644
--- a/v1/wd.c
+++ b/v1/wd.c
@@ -109,21 +109,24 @@ static int get_raw_attr(const char *dev_root, const char *attr,
 
 static int get_int_attr(struct dev_info *dinfo, const char *attr)
 {
-	int size;
-	char buf[MAX_ATTR_STR_SIZE];
+	char buf[MAX_ATTR_STR_SIZE] = {'\0'};
+	int ret;
 
 	/*
 	 * The signed int max number is INT_MAX 10bit char "4294967295"
 	 * When the value is bigger than INT_MAX, it returns INT_MAX
 	 */
-	size = get_raw_attr(dinfo->dev_root, attr, buf, MAX_ATTR_STR_SIZE);
-	if (size < 0)
-		return size;
-	else if (size >= INT_MAX_SIZE)
-		return INT_MAX;
-	/* Handing the read string's end tails '\n' to '\0' */
-	buf[size] = '\0';
-	return atoi((char *)buf);
+	ret = get_raw_attr(dinfo->dev_root, attr, buf, MAX_ATTR_STR_SIZE - 1);
+	if (ret < 0)
+		return ret;
+
+	ret = strtol(buf, NULL, 10);
+	if (errno == ERANGE) {
+		WD_ERR("failed to strtol %s, out of range!\n", buf);
+		return -errno;
+	}
+
+	return ret;
 }
 
 /*
diff --git a/wd_mempool.c b/wd_mempool.c
index 6143a69..b27d28c 100644
--- a/wd_mempool.c
+++ b/wd_mempool.c
@@ -620,8 +620,8 @@ void wd_blockpool_destroy(handle_t blkpool)
 
 static int get_value_from_sysfs(const char *path, ssize_t path_size)
 {
+	char buf[MAX_ATTR_STR_SIZE] = {'\0'};
 	char dev_path[PATH_MAX];
-	char buf[MAX_ATTR_STR_SIZE];
 	char *ptrRet = NULL;
 	ssize_t size;
 	int fd, ret;
@@ -638,7 +638,7 @@ static int get_value_from_sysfs(const char *path, ssize_t path_size)
 		goto err_open;
 	}
 
-	size = read(fd, buf, sizeof(buf));
+	size = read(fd, buf, MAX_ATTR_STR_SIZE - 1);
 	if (size <= 0) {
 		WD_ERR("failed to read %s!\n", dev_path);
 		goto err_read;
-- 
2.27.0

