From f3d1325a1a73a28ab08b25a25cc312da379f97b8 Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Thu, 25 Aug 2022 13:38:58 +0900 Subject: [PATCH] util: kernel: Use proper data type for the id of cpu stat To represent 'cpu' case in '/proc/stat' which has summation for all cpus, the number of cpu is set to -1, but data type of number is unsigned int. This corrects data type and change the name of member from 'cpu' to 'id' for understanding meaning easily. In addition, since 'id' is used as index of cpu stat array, it also checks that the index is in the proper range. Change-Id: I7e368176257c695d59420e7b4906d5bdb6d8aadf Signed-off-by: Dongwoo Lee --- include/util/kernel.h | 2 +- src/util/kernel.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/util/kernel.h b/include/util/kernel.h index a1d13d9..585ea1d 100644 --- a/include/util/kernel.h +++ b/include/util/kernel.h @@ -25,7 +25,7 @@ struct cpu_stat { char name[10]; - unsigned int cpu; + int id; int64_t user; int64_t system; int64_t nice; diff --git a/src/util/kernel.c b/src/util/kernel.c index 8226b7f..240ace0 100644 --- a/src/util/kernel.c +++ b/src/util/kernel.c @@ -92,14 +92,14 @@ static int __get_cpu_stat(FILE *fp, struct cpu_stat *data) goto err; } else if (strlen(data->name) > 3) { /* In case of 'cpu%u' string */ - ret = sscanf(data->name, "cpu%4u", &data->cpu); + ret = sscanf(data->name, "cpu%4d", &data->id); if (ret < 1) { ret = -EINVAL; goto err; } } else { /* In case of 'cpu' string */ - data->cpu = -1; + data->id = -1; } return 0; @@ -171,12 +171,12 @@ int kernel_get_per_cpu_stat(struct cpu_stat *cpus, int num_possible_cpus, if (__get_cpu_stat(fp, &cpu) < 0) continue; - if (cpu.cpu < 0) { + if (cpu.id < 0 || cpu.id > num_possible_cpus - 1) { ret = -EINVAL; goto err; } - memcpy(&cpus[cpu.cpu], &cpu, sizeof(struct cpu_stat)); + memcpy(&cpus[cpu.id], &cpu, sizeof(struct cpu_stat)); count++; } -- 2.34.1