]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
treewide: use (x)reallocarray() when applicable
authorThomas Weißschuh <thomas@t-8ch.de>
Fri, 22 Sep 2023 09:56:42 +0000 (11:56 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Fri, 22 Sep 2023 09:56:42 +0000 (11:56 +0200)
reallocarray() prevents overflow of the multiplication.
It also avoids issues with operator precedence like in libmount/src/context.c:

    pids = realloc(cxt->children, sizeof(pid_t) * cxt->nchildren + 1);

This only allocated one additional byte, and not enough space for
another child.

Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
31 files changed:
disk-utils/mkfs.cramfs.c
lib/buffer.c
lib/colors.c
lib/loopdev.c
lib/strv.c
libblkid/src/partitions/partitions.c
libmount/src/cache.c
libmount/src/context.c
libmount/src/utils.c
libsmartcols/src/grouping.c
libsmartcols/src/line.c
login-utils/lslogins.c
login-utils/su-common.c
misc-utils/findmnt-verify.c
misc-utils/findmnt.c
misc-utils/getopt.c
misc-utils/logger.c
misc-utils/lsblk-mnt.c
misc-utils/lsfd.c
sys-utils/irq-common.c
sys-utils/lscpu-cputype.c
sys-utils/lscpu-topology.c
sys-utils/lsmem.c
sys-utils/swapon-common.c
term-utils/script-playutils.c
term-utils/script.c
text-utils/col.c
text-utils/column.c
text-utils/more.c
text-utils/rev.c
text-utils/ul.c

index ca1d0410c0be77d1df4e4951001391e7724bb91d..cdb7b90afc4d62500a787ab8d9ebbad86f51600b 100644 (file)
@@ -495,7 +495,7 @@ static unsigned int write_directory_structure(struct entry *entry, char *base, u
                        if (entry->child) {
                                if (stack_entries >= stack_size) {
                                        stack_size *= 2;
-                                       entry_stack = xrealloc(entry_stack, stack_size * sizeof(struct entry *));
+                                       entry_stack = xreallocarray(entry_stack, stack_size, sizeof(struct entry *));
                                }
                                entry_stack[stack_entries] = entry;
                                stack_entries++;
index fda2fc8aa580ced63733f9efef5c6fa15699cc2b..49aa20c166e1dd7e99a95685609132d0e7acfc8f 100644 (file)
@@ -49,7 +49,7 @@ int ul_buffer_is_empty(struct ul_buffer *buf)
 int ul_buffer_save_pointer(struct ul_buffer *buf, unsigned short ptr_idx)
 {
        if (ptr_idx >= buf->nptrs) {
-               char **tmp = realloc(buf->ptrs, (ptr_idx + 1) * sizeof(char *));
+               char **tmp = reallocarray(buf->ptrs, ptr_idx + 1, sizeof(char *));
 
                if (!tmp)
                        return -EINVAL;
index 532e339502f4e5643f5fac1dc23c141249e8c430..2d4ba2fdba5243e46464f9e8e89251b0d275ebab 100644 (file)
@@ -380,8 +380,8 @@ static int colors_add_scheme(struct ul_color_ctl *cc,
 
        /* enlarge the array */
        if (cc->nschemes == cc->schemes_sz) {
-               void *tmp = realloc(cc->schemes, (cc->nschemes + 10)
-                                       * sizeof(struct ul_color_scheme));
+               void *tmp = reallocarray(cc->schemes, cc->nschemes + 10,
+                                        sizeof(struct ul_color_scheme));
                if (!tmp)
                        goto err;
                cc->schemes = tmp;
index 323f7bd50d39b92cebf9a51477621e68e4705fb4..dae499f256fa07c8b00a9913cd45c9ad81f733b4 100644 (file)
@@ -495,7 +495,7 @@ static int loop_scandir(const char *dirname, int **ary, int hasprefix)
 
                        arylen += 1;
 
-                       tmp = realloc(*ary, arylen * sizeof(int));
+                       tmp = reallocarray(*ary, arylen, sizeof(int));
                        if (!tmp) {
                                free(*ary);
                                *ary = NULL;
index c306e3816a988833aaffbc27c61189ec088b9d25..a6a22ee8e3f299fff3bf3e2553a38546fbe2552d 100644 (file)
@@ -265,7 +265,7 @@ int strv_push(char ***l, char *value) {
         if (m < n)
                 return -ENOMEM;
 
-        c = realloc(*l, sizeof(char *) * m);
+        c = reallocarray(*l, m, sizeof(char *));
         if (!c)
                 return -ENOMEM;
 
index b142ff7d8343cce161b5e9d863e01dd568ae40de..6a11c5069802d26128683c7a29e87c89b970b133 100644 (file)
@@ -436,8 +436,8 @@ static blkid_partition new_partition(blkid_partlist ls, blkid_parttable tab)
                /* Linux kernel has DISK_MAX_PARTS=256, but it's too much for
                 * generic Linux machine -- let start with 32 partitions.
                 */
-               void *tmp = realloc(ls->parts, (ls->nparts_max + 32) *
-                                       sizeof(struct blkid_struct_partition));
+               void *tmp = reallocarray(ls->parts, ls->nparts_max + 32,
+                                        sizeof(struct blkid_struct_partition));
                if (!tmp)
                        return NULL;
                ls->parts = tmp;
index 2505919c99c135719531adf95addae6bd0be141e..459441a8b1ad45ec3c9b03105640b96d9bf7035f 100644 (file)
@@ -202,7 +202,7 @@ static int cache_add_entry(struct libmnt_cache *cache, char *key,
        if (cache->nents == cache->nallocs) {
                size_t sz = cache->nallocs + MNT_CACHE_CHUNKSZ;
 
-               e = realloc(cache->ents, sz * sizeof(struct mnt_cache_entry));
+               e = reallocarray(cache->ents, sz, sizeof(struct mnt_cache_entry));
                if (!e)
                        return -ENOMEM;
                cache->ents = e;
index 0cd320190e314c992bab894246081250e3a0e34b..7efbd6193d5ea44ee60826f85171e72b9cb3e616 100644 (file)
@@ -2850,7 +2850,7 @@ static int mnt_context_add_child(struct libmnt_context *cxt, pid_t pid)
        if (!cxt)
                return -EINVAL;
 
-       pids = realloc(cxt->children, sizeof(pid_t) * cxt->nchildren + 1);
+       pids = reallocarray(cxt->children, cxt->nchildren + 1, sizeof(pid_t));
        if (!pids)
                return -ENOMEM;
 
index 3817b392711c89e3f272749d34fe853f0f6da729..c20c949a8443f234f0ff08b4e8f77da89840037b 100644 (file)
@@ -526,7 +526,7 @@ static int add_filesystem(char ***filesystems, char *name)
 
        if (n == 0 || !((n + 1) % MYCHUNK)) {
                size_t items = ((n + 1 + MYCHUNK) / MYCHUNK) * MYCHUNK;
-               char **x = realloc(*filesystems, items * sizeof(char *));
+               char **x = reallocarray(*filesystems, items, sizeof(char *));
 
                if (!x)
                        goto err;
index 0b27cb2e48d65862505850b84a5ab397578cba8f..0f6fe78a400369b14b7e76d5b67ead00912f9726 100644 (file)
@@ -278,7 +278,7 @@ static struct libscols_group **grpset_locate_freespace(struct libscols_table *tb
        DBG(TAB, ul_debugobj(tb, "   realocate grpset [sz: old=%zu, new=%zu, new_chunks=%d]",
                                tb->grpset_size, tb->grpset_size + wanted, chunks));
 
-       tmp = realloc(tb->grpset, (tb->grpset_size + wanted) * sizeof(struct libscols_group *));
+       tmp = reallocarray(tb->grpset, tb->grpset_size + wanted, sizeof(struct libscols_group *));
        if (!tmp)
                return NULL;
 
index cab99c51dad3cb9f9f2718376ff7d3f520df2400..813e6179a027fc6f5c900733fff957b3e9db83d7 100644 (file)
@@ -136,7 +136,7 @@ int scols_line_alloc_cells(struct libscols_line *ln, size_t n)
 
        DBG(LINE, ul_debugobj(ln, "alloc %zu cells", n));
 
-       ce = realloc(ln->cells, n * sizeof(struct libscols_cell));
+       ce = reallocarray(ln->cells, n, sizeof(struct libscols_cell));
        if (!ce)
                return -errno;
 
index ea5afb5ba6b1605fae15c51ddc510cbc8d9a0eed..02c6e0adffd2281147634cf2365c3b973d6fa14c 100644 (file)
@@ -505,7 +505,7 @@ static int parse_utmpx(const char *path, size_t *nrecords, struct utmpx **record
                        break;
                }
                if (i == imax)
-                       ary = xrealloc(ary, (imax *= 2) * sizeof(struct utmpx));
+                       ary = xreallocarray(ary, imax *= 2, sizeof(struct utmpx));
                ary[i] = *u;
        }
 
@@ -993,7 +993,7 @@ static int get_ulist(struct lslogins_control *ctl, char *logins, char *groups)
                        (*ar)[i++] = xstrdup(u);
 
                        if (i == *arsiz)
-                               *ar = xrealloc(*ar, sizeof(char *) * (*arsiz += 32));
+                               *ar = xreallocarray(*ar, *arsiz += 32, sizeof(char *));
                }
                ctl->ulist_on = 1;
        }
@@ -1018,7 +1018,7 @@ static int get_ulist(struct lslogins_control *ctl, char *logins, char *groups)
                                (*ar)[i++] = xstrdup(u);
 
                                if (i == *arsiz)
-                                       *ar = xrealloc(*ar, sizeof(char *) * (*arsiz += 32));
+                                       *ar = xreallocarray(*ar, *arsiz += 32, sizeof(char *));
                        }
                }
                ctl->ulist_on = 1;
index b6749204840f444fb73a6c48a23f4841348017b2..c5c0102e5ea9047997f082c9f026e32a6c886460 100644 (file)
@@ -1019,7 +1019,7 @@ static gid_t add_supp_group(const char *name, gid_t **groups, size_t *ngroups)
 
        DBG(MISC, ul_debug("add %s group [name=%s, GID=%d]", name, gr->gr_name, (int) gr->gr_gid));
 
-       *groups = xrealloc(*groups, sizeof(gid_t) * (*ngroups + 1));
+       *groups = xreallocarray(*groups, *ngroups + 1, sizeof(gid_t));
        (*groups)[*ngroups] = gr->gr_gid;
        (*ngroups)++;
 
index 3543c36e24817a7f8a50e1e00fb2aed42ed26679..7281c8df51cb2ec80a0f2f248b2d712f7d3c124d 100644 (file)
@@ -301,7 +301,7 @@ static int add_filesystem(struct verify_context *vfy, const char *name)
 
        if (vfy->fs_alloc == 0 || vfy->fs_num + 1 <= vfy->fs_alloc) {
                vfy->fs_alloc = ((vfy->fs_alloc + 1 + MYCHUNK) / MYCHUNK) * MYCHUNK;
-               vfy->fs_ary = xrealloc(vfy->fs_ary, vfy->fs_alloc * sizeof(char *));
+               vfy->fs_ary = xreallocarray(vfy->fs_ary, vfy->fs_alloc, sizeof(char *));
        }
 
        vfy->fs_ary[vfy->fs_num] = xstrdup(name);
index 5a8da5dd653fd649b581b7663adb4a2d50e13dfe..cb091201b5278f3810c75fb29d9dc8dab44e9cc0 100644 (file)
@@ -901,7 +901,7 @@ static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)),
 
 static char **append_tabfile(char **files, int *nfiles, char *filename)
 {
-       files = xrealloc(files, sizeof(char *) * (*nfiles + 1));
+       files = xreallocarray(files, *nfiles + 1, sizeof(char *));
        files[(*nfiles)++] = filename;
        return files;
 }
index 977b72560074ad1c521adaa418654370e41d3db4..f3621116711058adff9754ba4f57184463ab517e 100644 (file)
@@ -256,9 +256,9 @@ static void add_longopt(struct getopt_control *ctl, const char *name, int has_ar
 
        if (ctl->long_options_nr == ctl->long_options_length) {
                ctl->long_options_length += REALLOC_INCREMENT;
-               ctl->long_options = xrealloc(ctl->long_options,
-                                            sizeof(struct option) *
-                                            ctl->long_options_length);
+               ctl->long_options = xreallocarray(ctl->long_options,
+                                                 ctl->long_options_length,
+                                                 sizeof(struct option));
        }
        if (name) {
                /* Not for init! */
index 8174d55e0f92837c6013cb4c29c62d8b8cdd5af9..6d21fcae18fb979b8de0dd3ca7eb5fb93ef8387b 100644 (file)
@@ -375,7 +375,7 @@ static int journald_entry(struct logger_ctl *ctl, FILE *fp)
                        vectors *= 2;
                        if (IOV_MAX < vectors)
                                errx(EXIT_FAILURE, _("maximum input lines (%d) exceeded"), IOV_MAX);
-                       iovec = xrealloc(iovec, vectors * sizeof(struct iovec));
+                       iovec = xreallocarray(iovec, vectors, sizeof(struct iovec));
                }
                iovec[lines].iov_base = buf;
                iovec[lines].iov_len = sz;
index 9f6ba0dffd93c5cf0e816a33e4804b74a50000b9..f4ce67642ec017ee4111a45373dc8863b859d944 100644 (file)
@@ -60,8 +60,7 @@ static void add_filesystem(struct lsblk_device *dev, struct libmnt_fs *fs)
        assert(dev);
        assert(fs);
 
-       dev->fss = xrealloc(dev->fss, (dev->nfss + 1)
-                                       * sizeof(struct libmnt_fs *));
+       dev->fss = xreallocarray(dev->fss, dev->nfss + 1, sizeof(struct libmnt_fs *));
        dev->fss[dev->nfss] = fs;
        dev->nfss++;
        dev->is_mounted = 1;
index 0aaf18f12f353af421c6fae963c7344d1a4abd91..d4777e8f1ae81a02013d0178465b620dcf226a1e 100644 (file)
@@ -598,8 +598,8 @@ static void add_mnt_ns(ino_t id)
                nmax = (nspaces + 16) / 16 * 16;
        if (nmax <= nspaces + 1) {
                nmax += 16;
-               mnt_namespaces = xrealloc(mnt_namespaces,
-                                       sizeof(ino_t) * nmax);
+               mnt_namespaces = xreallocarray(mnt_namespaces,
+                                              nmax, sizeof(ino_t));
        }
        mnt_namespaces[nspaces++] = id;
 }
@@ -1687,7 +1687,7 @@ static void parse_pids(const char *str, pid_t **pids, int *count)
                errx(EXIT_FAILURE, _("out of range value for pid specification: %ld"), v);
 
        (*count)++;
-       *pids = xrealloc(*pids, (*count) * sizeof(**pids));
+       *pids = xreallocarray(*pids, *count, sizeof(**pids));
        (*pids)[*count - 1] = (pid_t)v;
 
        while (next && *next != '\0'
index 2d736a0fd1d13041e3caa4e0bdfb0c54f8dfc531..725a132814e1579df97e834ddc37caffd8367303 100644 (file)
@@ -328,8 +328,8 @@ static struct irq_stat *get_irqinfo(int softirq, size_t setsize, cpu_set_t *cpus
 
                if (stat->nr_irq == stat->nr_irq_info) {
                        stat->nr_irq_info *= 2;
-                       stat->irq_info = xrealloc(stat->irq_info,
-                                                 sizeof(*stat->irq_info) * stat->nr_irq_info);
+                       stat->irq_info = xreallocarray(stat->irq_info, stat->nr_irq_info,
+                                                      sizeof(*stat->irq_info));
                }
        }
        fclose(irqfile);
index c8f72ab8a112c25c6e43230d876c0ea8f2e13c27..6111f012ab37cfc29ce748504fe255a1848ec645 100644 (file)
@@ -109,8 +109,8 @@ struct lscpu_cputype *lscpu_cputype_get_default(struct lscpu_cxt *cxt)
 struct lscpu_cputype *lscpu_add_cputype(struct lscpu_cxt *cxt, struct lscpu_cputype *ct)
 {
        DBG(TYPE, ul_debugobj(ct, "add new"));
-       cxt->cputypes = xrealloc(cxt->cputypes, (cxt->ncputypes + 1)
-                               * sizeof(struct lscpu_cputype *));
+       cxt->cputypes = xreallocarray(cxt->cputypes, cxt->ncputypes + 1,
+                                     sizeof(struct lscpu_cputype *));
        cxt->cputypes[cxt->ncputypes] = ct;
        cxt->ncputypes++;
        lscpu_ref_cputype(ct);
@@ -434,8 +434,8 @@ static int cpuinfo_parse_cache(struct lscpu_cxt *cxt, int keynum, char *data)
                return 0;
 
        cxt->necaches++;
-       cxt->ecaches = xrealloc(cxt->ecaches,
-                               cxt->necaches * sizeof(struct lscpu_cache));
+       cxt->ecaches = xreallocarray(cxt->ecaches,
+                                    cxt->necaches, sizeof(struct lscpu_cache));
        cache = &cxt->ecaches[cxt->necaches - 1];
        memset(cache, 0 , sizeof(*cache));
 
index 754b3fcb4611e5a6ea5143985b57c62ca06bcc05..02a90d63f12f27ede3b57063f1d5a5bb1f6c212b 100644 (file)
@@ -274,8 +274,8 @@ static struct lscpu_cache *add_cache(struct lscpu_cxt *cxt,
        struct lscpu_cache *ca;
 
        cxt->ncaches++;
-       cxt->caches = xrealloc(cxt->caches,
-                              cxt->ncaches * sizeof(*cxt->caches));
+       cxt->caches = xreallocarray(cxt->caches,
+                                   cxt->ncaches, sizeof(*cxt->caches));
 
        ca = &cxt->caches[cxt->ncaches - 1];
        memset(ca, 0 , sizeof(*ca));
index 379d6169d82551c965964923a1b78efc5bb04596..0f6b91edbe4edb9ae89bf461e70203ba969c1c1b 100644 (file)
@@ -477,7 +477,7 @@ static void read_info(struct lsmem *lsmem)
                        continue;
                }
                lsmem->nblocks++;
-               lsmem->blocks = xrealloc(lsmem->blocks, lsmem->nblocks * sizeof(blk));
+               lsmem->blocks = xreallocarray(lsmem->blocks, lsmem->nblocks, sizeof(blk));
                *&lsmem->blocks[lsmem->nblocks - 1] = blk;
        }
 }
index bffedecfe82fe2fe1575ebf0790d0ba872e9c2f4..ceda7834261b224ee656ad60d619478c18a7db05 100644 (file)
@@ -85,7 +85,7 @@ static size_t ulct;
 
 void add_label(const char *label)
 {
-       llist = xrealloc(llist, (++llct) * sizeof(char *));
+       llist = xreallocarray(llist, ++llct, sizeof(char *));
        llist[llct - 1] = label;
 }
 
@@ -101,7 +101,7 @@ size_t numof_labels(void)
 
 void add_uuid(const char *uuid)
 {
-       ulist = xrealloc(ulist, (++ulct) * sizeof(char *));
+       ulist = xreallocarray(ulist, ++ulct, sizeof(char *));
        ulist[ulct - 1] = uuid;
 }
 
index cd598d2c6abe0a2b7b1f593ad69d30667a53b851..89656ab40383dfeb07b48cd2ab71d3230084ee8b 100644 (file)
@@ -165,7 +165,7 @@ static struct replay_log *replay_new_log(struct replay_setup *stp,
        assert(streams);
        assert(filename);
 
-       stp->logs = xrealloc(stp->logs, (stp->nlogs + 1) *  sizeof(*log));
+       stp->logs = xreallocarray(stp->logs, stp->nlogs + 1,  sizeof(*log));
        log = &stp->logs[stp->nlogs];
        stp->nlogs++;
 
index 7752c29140f1a09d92b85384db2fd2ed84e28787..9f53f3664074d70225da455457e8f8b494a3961e 100644 (file)
@@ -260,8 +260,8 @@ static struct script_log *log_associate(struct script_control *ctl,
        }
 
        /* add log to the stream */
-       stream->logs = xrealloc(stream->logs,
-                       (stream->nlogs + 1) * sizeof(log));
+       stream->logs = xreallocarray(stream->logs,
+                       stream->nlogs + 1, sizeof(log));
        stream->logs[stream->nlogs] = log;
        stream->nlogs++;
 
index afea8aa62582d4239a8cb9398630ef5deedb3667..2987db8ed504cb758ce387f3abe4f8e32a9f846c 100644 (file)
@@ -244,11 +244,11 @@ static void flush_line(struct col_ctl *ctl, struct col_line *l)
                 */
                if (sorted_size < l->l_lsize) {
                        sorted_size = l->l_lsize;
-                       sorted = xrealloc(sorted, sizeof(struct col_char) * sorted_size);
+                       sorted = xreallocarray(sorted, sorted_size, sizeof(struct col_char));
                }
                if (count_size <= l->l_max_col) {
                        count_size = l->l_max_col + 1;
-                       count = xrealloc(count, sizeof(size_t) * count_size);
+                       count = xreallocarray(count, count_size, sizeof(size_t));
                }
                memset(count, 0, sizeof(size_t) * l->l_max_col + 1);
                for (i = nchars, c = l->l_line; c && 0 < i; i--, c++)
index 6e6d13310f12ef0af6c26a0191b17be0ba110961..b2e62017a345504733e764a6d49478a27c76c17e 100644 (file)
@@ -585,7 +585,7 @@ static void add_entry(struct column_control *ctl, size_t *maxents, wchar_t *wcs)
 {
        if (ctl->nents <= *maxents) {
                *maxents += 1000;
-               ctl->ents = xrealloc(ctl->ents, *maxents * sizeof(wchar_t *));
+               ctl->ents = xreallocarray(ctl->ents, *maxents, sizeof(wchar_t *));
        }
        ctl->ents[ctl->nents] = wcs;
        ctl->nents++;
index 6026695fbca546981fafcbb22fc494d3fb684061..1dbe51334d64ecee17be5421b08f6129cb3a9bb6 100644 (file)
@@ -359,7 +359,7 @@ static void env_argscan(struct more_control *ctl, const char *s)
                env_argv[env_argc++] = tok;
                if (size < env_argc) {
                        size *= 2;
-                       env_argv = xrealloc(env_argv, sizeof(char *) * size);
+                       env_argv = xreallocarray(env_argv, size, sizeof(char *));
                }
        }
 
index fbf04d1a64418f7ceb33cf45a3d07dc524d9f93f..020f28a44f3d2331655119df2d4418f3b05b4d56 100644 (file)
@@ -182,7 +182,7 @@ int main(int argc, char *argv[])
                                /* So now we double the buffer size */
                                bufsiz *= 2;
 
-                               buf = xrealloc(buf, bufsiz * sizeof(wchar_t));
+                               buf = xreallocarray(buf, bufsiz, sizeof(wchar_t));
 
                                /* And fill the rest of the buffer */
                                len += read_line(sep, &buf[len], bufsiz/2, fp);
index d5bca457b65b2b87f716da4425742c9d4bfa8171..15d28ff404c2ed47e15b0aea01dc07c7fa8246ba 100644 (file)
@@ -141,7 +141,7 @@ static void need_column(struct ul_ctl *ctl, size_t new_max)
 
        while (new_max >= ctl->buflen) {
                ctl->buflen *= 2;
-               ctl->buf = xrealloc(ctl->buf, sizeof(struct ul_char) * ctl->buflen);
+               ctl->buf = xreallocarray(ctl->buf, ctl->buflen, sizeof(struct ul_char));
        }
 }