]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
Use *array() allocation functions where appropriate
authorAlejandro Colomar <alx@kernel.org>
Sat, 4 Feb 2023 20:47:01 +0000 (21:47 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 24 Feb 2023 02:28:43 +0000 (20:28 -0600)
This prevents overflow from multiplication.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
18 files changed:
lib/commonio.c
lib/groupmem.c
lib/gshadow.c
lib/sgetgrent.c
lib/sgroupio.c
lib/subordinateio.c
libmisc/addgrps.c
libmisc/env.c
libmisc/find_new_uid.c
libmisc/list.c
src/gpasswd.c
src/groups.c
src/id.c
src/newgrp.c
src/newusers.c
src/su.c
src/useradd.c
src/usermod.c

index 697bcb4f8f36603d83d25e748237874ea8c8b0b5..4113b5ce42b8de058f0a53033688b55d0ee605cd 100644 (file)
@@ -635,7 +635,7 @@ commonio_sort (struct commonio_db *db, int (*cmp) (const void *, const void *))
                return 0;
        }
 
-       entries = malloc (n * sizeof (struct commonio_entry *));
+       entries = mallocarray (n, sizeof (struct commonio_entry *));
        if (entries == NULL) {
                return -1;
        }
index 7969d8c4d571448fef0871ed6b16f0e1eae83901..e5f5e9cc8822afb5a106a50e05e7970dd77daff5 100644 (file)
@@ -46,7 +46,7 @@
        for (i = 0; grent->gr_mem[i]; i++);
 
        /*@-mustfreeonly@*/
-       gr->gr_mem = (char **) malloc ((i + 1) * sizeof (char *));
+       gr->gr_mem = (char **) mallocarray (i + 1, sizeof (char *));
        /*@=mustfreeonly@*/
        if (NULL == gr->gr_mem) {
                gr_free(gr);
index c98f20366dd24c6e0dcf6a132f876f3cdd2b958f..2d0db28ba2f4df61b44a732a5997b16167511cfb 100644 (file)
@@ -117,7 +117,7 @@ void endsgent (void)
        size_t len = strlen (string) + 1;
 
        if (len > sgrbuflen) {
-               char *buf = (char *) realloc (sgrbuf, sizeof (char) * len);
+               char *buf = (char *) reallocarray (sgrbuf, len, sizeof (char));
                if (NULL == buf) {
                        return NULL;
                }
index 5a368e97ad21222e7994dc4c42d346e4768c6848..dee9cb601b78f3c74530a828510c95f1e9000c43 100644 (file)
@@ -44,7 +44,7 @@ static char **list (char *s)
                   member name, or terminating NULL).  */
                if (i >= size) {
                        size = i + 100; /* at least: i + 1 */
-                       members = reallocf (members, size * sizeof (char *));
+                       members = reallocarrayf (members, size, sizeof(char *));
                        if (!members)
                                return NULL;
                }
index ab584c34b03980becad9a3a1ed15ab648945f966..4b4c020dad4f3206cc1346949ae1bde43413e9bc 100644 (file)
@@ -49,7 +49,7 @@
 
        for (i = 0; NULL != sgent->sg_adm[i]; i++);
        /*@-mustfreeonly@*/
-       sg->sg_adm = (char **) malloc ((i + 1) * sizeof (char *));
+       sg->sg_adm = (char **) mallocarray (i + 1, sizeof (char *));
        /*@=mustfreeonly@*/
        if (NULL == sg->sg_adm) {
                free (sg->sg_passwd);
@@ -74,7 +74,7 @@
 
        for (i = 0; NULL != sgent->sg_mem[i]; i++);
        /*@-mustfreeonly@*/
-       sg->sg_mem = (char **) malloc ((i + 1) * sizeof (char *));
+       sg->sg_mem = (char **) mallocarray (i + 1, sizeof (char *));
        /*@=mustfreeonly@*/
        if (NULL == sg->sg_mem) {
                for (i = 0; NULL != sg->sg_adm[i]; i++) {
index d538f12cfd5a3d449578f7cc98ef7c8bc73552e9..7bb7c3b2c436a237d2caf509fd4e5298ab87000a 100644 (file)
@@ -319,7 +319,7 @@ static bool append_range(struct subid_range **ranges, const struct subordinate_r
                        return false;
        } else {
                struct subid_range *alloced;
-               alloced = realloc(*ranges, (n + 1) * (sizeof(struct subid_range)));
+               alloced = reallocarray(*ranges, n + 1, sizeof(struct subid_range));
                if (!alloced)
                        return false;
                *ranges = alloced;
@@ -911,7 +911,7 @@ static int append_uids(uid_t **uids, const char *owner, int n)
                        return n;
        }
 
-       ret = realloc(*uids, (n + 1) * sizeof(uid_t));
+       ret = reallocarray(*uids, n + 1, sizeof(uid_t));
        if (!ret) {
                free(*uids);
                return -1;
index 21ebda8ddc3ca05d88b600b12fcd6c66b0d7abe6..32f8af012eccd0e100552e5346553b357f1ef417 100644 (file)
@@ -46,7 +46,7 @@ int add_groups (const char *list)
 
        i = 16;
        for (;;) {
-               grouplist = (gid_t *) malloc (i * sizeof (GETGROUPS_T));
+               grouplist = (gid_t *) mallocarray (i, sizeof (GETGROUPS_T));
                if (NULL == grouplist) {
                        return -1;
                }
@@ -88,7 +88,7 @@ int add_groups (const char *list)
                        fputs (_("Warning: too many groups\n"), shadow_logfd);
                        break;
                }
-               tmp = (gid_t *) realloc (grouplist, (size_t)(ngroups + 1) * sizeof (GETGROUPS_T));
+               tmp = (gid_t *) reallocarray (grouplist, (size_t)ngroups + 1, sizeof (GETGROUPS_T));
                if (NULL == tmp) {
                        free (grouplist);
                        return -1;
index 640c96a579dc507f3d75a97560472d507150680c..5b4eec13e1b7aa8a3713e3626648edba3ca25c95 100644 (file)
@@ -60,7 +60,7 @@ static const char *const noslash[] = {
  */
 void initenv (void)
 {
-       newenvp = (char **) xmalloc (NEWENVP_STEP * sizeof (char *));
+       newenvp = (char **) xmallocarray (NEWENVP_STEP, sizeof (char *));
        *newenvp = NULL;
 }
 
index 09885236b8676df975bfee0071e2f7c9d1bbb20f..1be646f2b3512b0cb5194add0bd1a71c6fe07782 100644 (file)
@@ -231,7 +231,7 @@ int find_new_uid(bool sys_user,
         */
 
        /* Create an array to hold all of the discovered UIDs */
-       used_uids = malloc (sizeof (bool) * (uid_max +1));
+       used_uids = mallocarray (uid_max + 1, sizeof (bool));
        if (NULL == used_uids) {
                fprintf (log_get_logfd(),
                         _("%s: failed to allocate memory: %s\n"),
index 9470b06cccb2b8297d69557622719bf72ad53ab2..2b8f155aa6681811069a093154f363c03a8b3dd4 100644 (file)
@@ -44,7 +44,7 @@
         * old entries, and the new entries as well.
         */
 
-       tmp = (char **) xmalloc ((i + 2) * sizeof member);
+       tmp = (char **) xmallocarray (i + 2, sizeof member);
 
        /*
         * Copy the original list to the new list, then append the
@@ -98,7 +98,7 @@
         * old entries.
         */
 
-       tmp = (char **) xmalloc ((j + 1) * sizeof member);
+       tmp = (char **) xmallocarray (j + 1, sizeof member);
 
        /*
         * Copy the original list except the deleted members to the
 
        for (i = 0; NULL != list[i]; i++);
 
-       tmp = (char **) xmalloc ((i + 1) * sizeof (char *));
+       tmp = (char **) xmallocarray (i + 1, sizeof (char *));
 
        i = 0;
        while (NULL != *list) {
@@ -210,7 +210,7 @@ bool is_on_list (char *const *list, const char *member)
         * Allocate the array we're going to store the pointers into.
         */
 
-       array = (char **) xmalloc (sizeof (char *) * i);
+       array = (char **) xmallocarray (i, sizeof (char *));
 
        /*
         * Empty list is special - 0 members, not 1 empty member.  --marekm
index a1739fcec37ab442f31f7f36c3a72db93d613dd1..cb78c2aa0f68d49329501612c40d6cdce32826df 100644 (file)
@@ -834,7 +834,7 @@ static void get_group (struct group *gr)
 
                        sg->sg_mem = dup_list (gr->gr_mem);
 
-                       sg->sg_adm = (char **) xmalloc (sizeof (char *) * 2);
+                       sg->sg_adm = (char **) xmallocarray (2, sizeof (char *));
 #ifdef FIRST_MEMBER_IS_ADMIN
                        if (sg->sg_mem[0]) {
                                sg->sg_adm[0] = xstrdup (sg->sg_mem[0]);
index 12bd224bb3947b9565992d487c9730b54f468202..bfa548fde05392e842a9ef0d2b8f9bb3f73e600b 100644 (file)
@@ -88,7 +88,7 @@ int main (int argc, char **argv)
        GETGROUPS_T *groups;
 
        sys_ngroups = sysconf (_SC_NGROUPS_MAX);
-       groups = (GETGROUPS_T *) malloc (sizeof (GETGROUPS_T) * sys_ngroups);
+       groups = (GETGROUPS_T *) mallocarray (sys_ngroups, sizeof (GETGROUPS_T));
 
        (void) setlocale (LC_ALL, "");
        (void) bindtextdomain (PACKAGE, LOCALEDIR);
index 49521093c8a681e12c67be09e0eeb9c1a0387941..e5a75f4a2c385018868f64ef02aa0a078a1f84dd 100644 (file)
--- a/src/id.c
+++ b/src/id.c
@@ -63,7 +63,7 @@ static void usage (void)
         * work if the system library is recompiled.
         */
        sys_ngroups = sysconf (_SC_NGROUPS_MAX);
-       groups = (GETGROUPS_T *) malloc (sizeof (GETGROUPS_T) * sys_ngroups);
+       groups = (GETGROUPS_T *) mallocarray (sys_ngroups, sizeof (GETGROUPS_T));
 
        /*
         * See if the -a flag has been given to print out the concurrent
index c8dafe6e7742beec4f5d99bc83302796b3f1455b..87fd779629e88b117eca73451145a39cedd76624 100644 (file)
@@ -531,7 +531,7 @@ int main (int argc, char **argv)
        /* don't use getgroups(0, 0) - it doesn't work on some systems */
        i = 16;
        for (;;) {
-               grouplist = (GETGROUPS_T *) xmalloc (i * sizeof (GETGROUPS_T));
+               grouplist = (GETGROUPS_T *) xmallocarray (i, sizeof (GETGROUPS_T));
                ngroups = getgroups (i, grouplist);
                if (i > ngroups && !(ngroups == -1 && errno == EINVAL)) {
                        break;
index 670168e631328852a600e7494db9eb8bb61a7353..315ec3c6ee0d81ba476f095280f6d9b10ab9a0c2 100644 (file)
@@ -1200,9 +1200,9 @@ int main (int argc, char **argv)
 #ifdef USE_PAM
                /* keep the list of user/password for later update by PAM */
                nusers++;
-               lines     = reallocf (lines,     sizeof (lines[0])     * nusers);
-               usernames = reallocf (usernames, sizeof (usernames[0]) * nusers);
-               passwords = reallocf (passwords, sizeof (passwords[0]) * nusers);
+               lines     = reallocf (lines,     nusers, sizeof (lines[0]));
+               usernames = reallocf (usernames, nusers, sizeof (usernames[0]));
+               passwords = reallocf (passwords, nusers, sizeof (passwords[0]));
                if (lines == NULL || usernames == NULL || passwords == NULL) {
                        fprintf (stderr,
                                 _("%s: line %d: %s\n"),
index af1873ee4556f62ea3da645b24fb3ee5bb046435..f7777964692c3e0abbc6b642834f40315dceb79a 100644 (file)
--- a/src/su.c
+++ b/src/su.c
@@ -238,7 +238,7 @@ static void execve_shell (const char *shellname,
                while (NULL != args[n_args]) {
                        n_args++;
                }
-               targs = (char **) xmalloc ((n_args + 3) * sizeof (args[0]));
+               targs = (char **) xmallocarray (n_args + 3, sizeof (args[0]));
                targs[0] = "sh";
                targs[1] = "-";
                targs[2] = xstrdup (shellname);
index ffe8e11bd13652831e8a49972ffb4f5c1e7bb41b..b3698aa5aca24df65998143411f8afae6ddbabd0 100644 (file)
@@ -2539,7 +2539,7 @@ int main (int argc, char **argv)
 #endif
 
        sys_ngroups = sysconf (_SC_NGROUPS_MAX);
-       user_groups = (char **) xmalloc ((1 + sys_ngroups) * sizeof (char *));
+       user_groups = (char **) xmallocarray (1 + sys_ngroups, sizeof (char *));
        /*
         * Initialize the list to be empty
         */
index 787b1cc8c654951e19abb1d4cbf6be98d89a4e92..aa2d684fa9e4b0e585f45ef65ae67e1c667f0aa3 100644 (file)
@@ -2150,7 +2150,7 @@ int main (int argc, char **argv)
 #endif
 
        sys_ngroups = sysconf (_SC_NGROUPS_MAX);
-       user_groups = (char **) malloc (sizeof (char *) * (1 + sys_ngroups));
+       user_groups = (char **) mallocarray (sys_ngroups + 1, sizeof (char *));
        user_groups[0] = NULL;
 
        is_shadow_pwd = spw_file_present ();