From cb75cf88b83d015553712284a4d24995a3e875b4 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 22 Jun 2021 17:06:46 +0200 Subject: [PATCH] lscpu: do not use atoi() Addresses: https://github.com/karelzak/util-linux/issues/1358 Signed-off-by: Karel Zak --- sys-utils/lscpu-cputype.c | 20 ++++++++++++++++++-- sys-utils/lscpu-topology.c | 7 ++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/sys-utils/lscpu-cputype.c b/sys-utils/lscpu-cputype.c index 2cc0ca929..c543a3276 100644 --- a/sys-utils/lscpu-cputype.c +++ b/sys-utils/lscpu-cputype.c @@ -298,7 +298,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); } @@ -484,7 +492,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); diff --git a/sys-utils/lscpu-topology.c b/sys-utils/lscpu-topology.c index 485c17485..f4e8181b7 100644 --- a/sys-utils/lscpu-topology.c +++ b/sys-utils/lscpu-topology.c @@ -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;