util: kernel: Use proper data type for the id of cpu stat 26/280226/1
authorDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 25 Aug 2022 04:38:58 +0000 (13:38 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 25 Aug 2022 10:53:10 +0000 (19:53 +0900)
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 <dwoo08.lee@samsung.com>
include/util/kernel.h
src/util/kernel.c

index a1d13d9ffbc4450eb7c70bf881285b435958be16..585ea1d72e29b72fe50fff0973c07d01d2b6077e 100644 (file)
@@ -25,7 +25,7 @@
 
 struct cpu_stat {
        char name[10];
-       unsigned int cpu;
+       int id;
        int64_t user;
        int64_t system;
        int64_t nice;
index 8226b7f38f600120cad34878f68a81fa015f5a62..240ace010ad6034bd5a46ba17ec7c4c070f0d284 100644 (file)
@@ -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++;
        }