]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lscpu: do not use atoi()
authorKarel Zak <kzak@redhat.com>
Tue, 22 Jun 2021 15:06:46 +0000 (17:06 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 20 Jul 2021 09:50:52 +0000 (11:50 +0200)
Addresses: https://github.com/karelzak/util-linux/issues/1358
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/lscpu-cputype.c
sys-utils/lscpu-topology.c

index 2cc0ca929863e5a1f27c5ed87198ea72c10aaf49..c543a3276df4f9442d2751e232fc8fd8b0b6f8fc 100644 (file)
@@ -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);
index 485c17485fc70a094f30ba6824ccb1657a815882..f4e8181b7db23236954930b34a244a3c60a32679 100644 (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;