From: Karel Zak Date: Tue, 22 Jun 2021 15:06:46 +0000 (+0200) Subject: lscpu: do not use atoi() X-Git-Tag: v2.37.1~39 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=cb75cf88b83d015553712284a4d24995a3e875b4;p=thirdparty%2Futil-linux.git lscpu: do not use atoi() Addresses: https://github.com/karelzak/util-linux/issues/1358 Signed-off-by: Karel Zak --- diff --git a/sys-utils/lscpu-cputype.c b/sys-utils/lscpu-cputype.c index 2cc0ca9298..c543a3276d 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 485c17485f..f4e8181b7d 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;