lscpu: do not use atoi()

Addresses: https://github.com/karelzak/util-linux/issues/1358
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2021-06-22 17:06:46 +02:00
parent 55cbb788f8
commit 24c17c6294
2 changed files with 24 additions and 3 deletions

View File

@ -300,7 +300,15 @@ static char *key_cleanup(char *str, int *keynum)
}
if (i < sz) {
*keynum = atoi(str + i);
char *end = NULL, *p = str + i;
int n;
errno = 0;
n = strtol(p, &end, 10);
if (errno || !end || end == p)
return str;
*keynum = n;
str[i] = '\0';
rtrim_whitespace((unsigned char *)str);
}
@ -486,7 +494,15 @@ int lscpu_read_cpuinfo(struct lscpu_cxt *cxt)
case CPUINFO_LINE_CPU:
if (pattern->id == PAT_PROCESSOR) {
/* switch CPU */
int id = keynum >= 0 ? keynum : atoi(value);
int id = 0;
if (keynum >= 0)
id = keynum;
else {
uint64_t n;
if (ul_strtou64(value, &n, 10) == 0 && n <= INT_MAX)
id = n;
}
if (pr->curr_cpu && pr->curr_type)
lscpu_cpu_set_type(pr->curr_cpu, pr->curr_type);

View File

@ -190,7 +190,12 @@ static int cputype_read_topology(struct lscpu_cxt *cxt, struct lscpu_cputype *ct
fclose(fd);
}
ct->nthreads_per_core = ct->mtid ? atoi(ct->mtid) + 1 : nthreads;
ct->nthreads_per_core = nthreads;
if (ct->mtid) {
uint64_t x;
if (ul_strtou64(ct->mtid, &x, 10) == 0 && x <= ULONG_MAX)
ct->nthreads_per_core = (size_t) x + 1;
}
if (!sw_topo) {
ct->ncores_per_socket = ct->nsockets ? ct->ncores / ct->nsockets : 0;