]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsmem: check errno after strto..()
authorKarel Zak <kzak@redhat.com>
Mon, 21 Jun 2021 13:29:33 +0000 (15:29 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 21 Jun 2021 13:29:33 +0000 (15:29 +0200)
Addresses: https://github.com/karelzak/util-linux/issues/1356
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/chmem.c
sys-utils/lsmem.c

index 2f231d6645f7049749529775e644759042a0c0b8..09d0af6c0c860039512cc4f796b55b4b58b4ad23 100644 (file)
@@ -262,9 +262,16 @@ static void read_info(struct chmem_desc *desc)
 
        desc->ndirs = scandir(_PATH_SYS_MEMORY, &desc->dirs, filter, versionsort);
        if (desc->ndirs <= 0)
-               err(EXIT_FAILURE, _("Failed to read %s"), _PATH_SYS_MEMORY);
+               goto fail;
        ul_path_read_buffer(desc->sysmem, line, sizeof(line), "block_size_bytes");
+
+       errno = 0;
        desc->block_size = strtoumax(line, NULL, 16);
+       if (errno)
+               goto fail;
+       return;
+fail:
+       err(EXIT_FAILURE, _("Failed to read %s"), _PATH_SYS_MEMORY);
 }
 
 static void parse_single_param(struct chmem_desc *desc, char *str)
index 0b3d3ed12cf8232d9f7fd19fa5a6999cede7f2b7..379d6169d82551c965964923a1b78efc5bb04596 100644 (file)
@@ -348,25 +348,32 @@ static int memory_block_get_node(struct lsmem *lsmem, char *name)
                        continue;
                if (!isdigit_string(de->d_name + 4))
                        continue;
+               errno = 0;
                node = strtol(de->d_name + 4, NULL, 10);
+               if (errno)
+                       continue;
                break;
        }
        closedir(dir);
        return node;
 }
 
-static void memory_block_read_attrs(struct lsmem *lsmem, char *name,
+static int memory_block_read_attrs(struct lsmem *lsmem, char *name,
                                    struct memory_block *blk)
 {
        char *line = NULL;
-       int i, x = 0;
+       int i, x = 0, rc = 0;
 
        memset(blk, 0, sizeof(*blk));
 
+       errno = 0;
        blk->count = 1;
        blk->state = MEMORY_STATE_UNKNOWN;
        blk->index = strtoumax(name + 6, NULL, 10); /* get <num> of "memory<num>" */
 
+       if (errno)
+               rc = -errno;
+
        if (ul_path_readf_s32(lsmem->sysmem, &x, "%s/removable", name) == 0)
                blk->removable = x == 1;
 
@@ -397,6 +404,8 @@ static void memory_block_read_attrs(struct lsmem *lsmem, char *name,
                }
                free(line);
        }
+
+       return rc;
 }
 
 static int is_mergeable(struct lsmem *lsmem, struct memory_block *blk)
@@ -451,7 +460,11 @@ static void read_info(struct lsmem *lsmem)
 
        if (ul_path_read_buffer(lsmem->sysmem, buf, sizeof(buf), "block_size_bytes") <= 0)
                err(EXIT_FAILURE, _("failed to read memory block size"));
+
+       errno = 0;
        lsmem->block_size = strtoumax(buf, NULL, 16);
+       if (errno)
+               err(EXIT_FAILURE, _("failed to read memory block size"));
 
        for (i = 0; i < lsmem->ndirs; i++) {
                memory_block_read_attrs(lsmem, lsmem->dirs[i]->d_name, &blk);