From 198daf0e54215f76ddb62caa8bea41ff6625db40 Mon Sep 17 00:00:00 2001 From: WangFengTu Date: Sat, 27 Nov 2021 14:15:34 +0800 Subject: [PATCH 08/14] fix cpu variant get error Signed-off-by: WangFengTu --- src/utils/cutils/utils.c | 14 +++++--------- src/utils/cutils/utils_file.c | 6 +++++- src/utils/cutils/utils_file.h | 5 +++++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/utils/cutils/utils.c b/src/utils/cutils/utils.c index a3e192fe..511cde96 100644 --- a/src/utils/cutils/utils.c +++ b/src/utils/cutils/utils.c @@ -1299,12 +1299,11 @@ restart: static char *get_cpu_variant() { char *variant = NULL; - char *cpuinfo = NULL; + char cpuinfo[1024] = { 0 }; char *start_pos = NULL; char *end_pos = NULL; - cpuinfo = util_read_text_file("/proc/cpuinfo"); - if (cpuinfo == NULL) { + if (util_file2str("/proc/cpuinfo", cpuinfo, sizeof(cpuinfo)) < 0) { ERROR("read /proc/cpuinfo failed"); return NULL; } @@ -1312,7 +1311,7 @@ static char *get_cpu_variant() start_pos = strstr(cpuinfo, "CPU architecture"); if (start_pos == NULL) { ERROR("can not found the key \"CPU architecture\" when try to get cpu variant"); - goto out; + return NULL; } end_pos = strchr(start_pos, '\n'); if (end_pos != NULL) { @@ -1321,17 +1320,14 @@ static char *get_cpu_variant() start_pos = strchr(start_pos, ':'); if (start_pos == NULL) { ERROR("can not found delimiter \":\" when try to get cpu variant"); - goto out; + return NULL; } + start_pos += 1; // skip char ":" util_trim_newline(start_pos); start_pos = util_trim_space(start_pos); variant = util_strings_to_lower(start_pos); -out: - free(cpuinfo); - cpuinfo = NULL; - return variant; } diff --git a/src/utils/cutils/utils_file.c b/src/utils/cutils/utils_file.c index f4fa4ece..00825bea 100644 --- a/src/utils/cutils/utils_file.c +++ b/src/utils/cutils/utils_file.c @@ -815,7 +815,11 @@ char *util_add_path(const char *path, const char *name) return new_path; } -/* note: This function can only read small text file. */ +/* notes: + * 1. Do not use this function to read proc file because proc file in armv8 does not + * support fseek and the result of this function is nill string which is unexpected. + * 2. This function can only read small text file. + */ char *util_read_text_file(const char *path) { char *buf = NULL; diff --git a/src/utils/cutils/utils_file.h b/src/utils/cutils/utils_file.h index a7fbbb6b..1465ca7e 100644 --- a/src/utils/cutils/utils_file.h +++ b/src/utils/cutils/utils_file.h @@ -68,6 +68,11 @@ char *util_path_dir(const char *path); char *util_add_path(const char *path, const char *name); +/* notes: + * 1. Do not use this function to read proc file because proc file in armv8 does not + * support fseek and the result of this function is nill string which is unexpected. + * 2. This function can only read small text file. + */ char *util_read_text_file(const char *path); int64_t util_file_size(const char *filename); -- 2.25.1