]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
Use safer allocation macros
authorAlejandro Colomar <alx@kernel.org>
Sat, 4 Feb 2023 21:41:18 +0000 (22:41 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 24 Feb 2023 02:28:43 +0000 (20:28 -0600)
Use of these macros, apart from the benefits mentioned in the commit
that adds the macros, has some other good side effects:

-  Consistency in getting the size of the object from sizeof(type),
   instead of a mix of sizeof(type) sometimes and sizeof(*p) other
   times.

-  More readable code: no casts, and no sizeof(), so also shorter lines
   that we don't need to cut.

-  Consistency in using array allocation calls for allocations of arrays
   of objects, even when the object size is 1.

Cc: Valentin V. Bartenev <vbartenev@gmail.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
44 files changed:
lib/commonio.c
lib/getdef.c
lib/groupio.c
lib/groupmem.c
lib/gshadow.c
lib/nss.c
lib/pwmem.c
lib/run_part.c
lib/sgetgrent.c
lib/sgroupio.c
lib/shadowmem.c
lib/sssd.c
lib/subordinateio.c
libmisc/addgrps.c
libmisc/agetpass.c
libmisc/copydir.c
libmisc/env.c
libmisc/find_new_gid.c
libmisc/find_new_uid.c
libmisc/idmapping.c
libmisc/list.c
libmisc/loginprompt.c
libmisc/mail.c
libmisc/obscure.c
libmisc/pam_pass_non_interactive.c
libmisc/prefix_flag.c
libmisc/setupenv.c
libmisc/utmp.c
libmisc/xgetXXbyYY.c
src/gpasswd.c
src/groupmems.c
src/groupmod.c
src/groups.c
src/id.c
src/login.c
src/newgrp.c
src/newusers.c
src/passwd.c
src/su.c
src/useradd.c
src/userdel.c
src/usermod.c
src/vipw.c
tests/libsubid/04_nss/libsubid_zzz.c

index 4113b5ce42b8de058f0a53033688b55d0ee605cd..40e6229871eebff341a3eeeed8997663fb0746e6 100644 (file)
@@ -21,6 +21,8 @@
 #include <errno.h>
 #include <stdio.h>
 #include <signal.h>
+
+#include "alloc.h"
 #include "nscd.h"
 #include "sssd.h"
 #ifdef WITH_TCB
@@ -240,11 +242,11 @@ int commonio_lock_nowait (struct commonio_db *db, bool log)
        }
        file_len = strlen(db->filename) + 11;/* %lu max size */
        lock_file_len = strlen(db->filename) + 6; /* sizeof ".lock" */
-       file = (char*)malloc(file_len);
+       file = MALLOCARRAY(file_len, char);
        if (file == NULL) {
                goto cleanup_ENOMEM;
        }
-       lock = (char*)malloc(lock_file_len);
+       lock = MALLOCARRAY(lock_file_len, char);
        if (lock == NULL) {
                goto cleanup_ENOMEM;
        }
@@ -513,7 +515,7 @@ int commonio_open (struct commonio_db *db, int mode)
        fcntl (fileno (db->fp), F_SETFD, FD_CLOEXEC);
 
        buflen = BUFLEN;
-       buf = (char *) malloc (buflen);
+       buf = MALLOCARRAY (buflen, char);
        if (NULL == buf) {
                goto cleanup_ENOMEM;
        }
@@ -524,7 +526,7 @@ int commonio_open (struct commonio_db *db, int mode)
                        size_t len;
 
                        buflen += BUFLEN;
-                       cp = (char *) realloc (buf, buflen);
+                       cp = REALLOCARRAY (buf, buflen, char);
                        if (NULL == cp) {
                                goto cleanup_buf;
                        }
@@ -558,7 +560,7 @@ int commonio_open (struct commonio_db *db, int mode)
                        }
                }
 
-               p = (struct commonio_entry *) malloc (sizeof *p);
+               p = MALLOC (struct commonio_entry);
                if (NULL == p) {
                        goto cleanup_entry;
                }
@@ -635,7 +637,7 @@ commonio_sort (struct commonio_db *db, int (*cmp) (const void *, const void *))
                return 0;
        }
 
-       entries = mallocarray (n, sizeof (struct commonio_entry *));
+       entries = MALLOCARRAY (n, struct commonio_entry *);
        if (entries == NULL) {
                return -1;
        }
@@ -954,7 +956,7 @@ int commonio_update (struct commonio_db *db, const void *eptr)
                return 1;
        }
        /* not found, new entry */
-       p = (struct commonio_entry *) malloc (sizeof *p);
+       p = MALLOC (struct commonio_entry);
        if (NULL == p) {
                db->ops->free (nentry);
                errno = ENOMEM;
@@ -991,7 +993,7 @@ int commonio_append (struct commonio_db *db, const void *eptr)
                return 0;
        }
        /* new entry */
-       p = (struct commonio_entry *) malloc (sizeof *p);
+       p = MALLOC (struct commonio_entry);
        if (NULL == p) {
                db->ops->free (nentry);
                errno = ENOMEM;
index 355215c4cca587444f626bf21cd4f59506e4fc0d..a2a7e484cd78247f513bcb2da4bd1b8da8c693dd 100644 (file)
 #ifdef USE_ECONF
 #include <libeconf.h>
 #endif
+
+#include "alloc.h"
 #include "getdef.h"
 #include "shadowlog_internal.h"
+
 /*
  * A configuration item definition.
  */
@@ -445,14 +448,14 @@ void setdef_config_file (const char* file)
        char* cp;
 
        len = strlen(file) + strlen(sysconfdir) + 2;
-       cp = malloc(len);
+       cp = MALLOCARRAY(len, char);
        if (cp == NULL)
                exit (13);
        snprintf(cp, len, "%s/%s", file, sysconfdir);
        sysconfdir = cp;
 #ifdef VENDORDIR
        len = strlen(file) + strlen(vendordir) + 2;
-       cp = malloc(len);
+       cp = MALLOCARRAY(len, char);
        if (cp == NULL)
                exit (13);
        snprintf(cp, len, "%s/%s", file, vendordir);
index f29ab643d81c2798524b331887d1372c717ed8c1..d2116af25d86ffdf0581a6f2e3bc393994fd9be8 100644 (file)
@@ -15,6 +15,7 @@
 #include <assert.h>
 #include <stdio.h>
 
+#include "alloc.h"
 #include "prototypes.h"
 #include "defines.h"
 #include "commonio.h"
@@ -311,7 +312,7 @@ static /*@null@*/struct commonio_entry *merge_group_entries (
 
        /* Concatenate the 2 lines */
        new_line_len = strlen (gr1->line) + strlen (gr2->line) +1;
-       new_line = (char *)malloc (new_line_len + 1);
+       new_line = MALLOCARRAY (new_line_len + 1, char);
        if (NULL == new_line) {
                return NULL;
        }
@@ -332,7 +333,7 @@ static /*@null@*/struct commonio_entry *merge_group_entries (
                        members++;
                }
        }
-       new_members = (char **)calloc ( (members+1), sizeof(char*) );
+       new_members = CALLOC (members + 1, char *);
        if (NULL == new_members) {
                free (new_line);
                return NULL;
@@ -393,7 +394,7 @@ static int split_groups (unsigned int max_members)
                        continue;
                }
 
-               new = (struct commonio_entry *) malloc (sizeof *new);
+               new = MALLOC (struct commonio_entry);
                if (NULL == new) {
                        return 0;
                }
index e5f5e9cc8822afb5a106a50e05e7970dd77daff5..e0088fe3625eecb4c6d5c4b5bd7e84c1188ffd52 100644 (file)
@@ -12,6 +12,7 @@
 
 #ident "$Id$"
 
+#include "alloc.h"
 #include "prototypes.h"
 #include "defines.h"
 #include "groupio.h"
@@ -21,7 +22,7 @@
        struct group *gr;
        int i;
 
-       gr = (struct group *) malloc (sizeof *gr);
+       gr = MALLOC (struct group);
        if (NULL == gr) {
                return NULL;
        }
@@ -46,7 +47,7 @@
        for (i = 0; grent->gr_mem[i]; i++);
 
        /*@-mustfreeonly@*/
-       gr->gr_mem = (char **) mallocarray (i + 1, sizeof (char *));
+       gr->gr_mem = MALLOCARRAY (i + 1, char *);
        /*@=mustfreeonly@*/
        if (NULL == gr->gr_mem) {
                gr_free(gr);
index 2d0db28ba2f4df61b44a732a5997b16167511cfb..c17af67fdf4d5a8c635597a5f4dc56230e1892e4 100644 (file)
 
 #include <stdio.h>
 #include <string.h>
+
+#include "alloc.h"
 #include "prototypes.h"
 #include "defines.h"
+
 static /*@null@*/FILE *shadow;
 static /*@null@*//*@only@*/char **members = NULL;
 static size_t nmembers = 0;
@@ -63,7 +66,7 @@ static /*@null@*/char **build_list (char *s, char **list[], size_t * nlist)
 
        while (s != NULL && *s != '\0') {
                size = (nelem + 1) * sizeof (ptr);
-               ptr = realloc (*list, size);
+               ptr = REALLOCARRAY (*list, size, char *);
                if (NULL != ptr) {
                        ptr[nelem] = s;
                        nelem++;
@@ -77,7 +80,7 @@ static /*@null@*/char **build_list (char *s, char **list[], size_t * nlist)
                }
        }
        size = (nelem + 1) * sizeof (ptr);
-       ptr = realloc (*list, size);
+       ptr = REALLOCARRAY (*list, size, char *);
        if (NULL != ptr) {
                ptr[nelem] = NULL;
                *list = ptr;
@@ -117,7 +120,7 @@ void endsgent (void)
        size_t len = strlen (string) + 1;
 
        if (len > sgrbuflen) {
-               char *buf = (char *) reallocarray (sgrbuf, len, sizeof (char));
+               char *buf = REALLOCARRAY (sgrbuf, len, char);
                if (NULL == buf) {
                        return NULL;
                }
@@ -195,7 +198,7 @@ void endsgent (void)
        char *cp;
 
        if (0 == buflen) {
-               buf = (char *) malloc (BUFSIZ);
+               buf = MALLOCARRAY (BUFSIZ, char);
                if (NULL == buf) {
                        return NULL;
                }
@@ -216,7 +219,7 @@ void endsgent (void)
                       && (feof (fp) == 0)) {
                        size_t len;
 
-                       cp = (char *) realloc (buf, buflen*2);
+                       cp = REALLOCARRAY (buf, buflen * 2, char);
                        if (NULL == cp) {
                                return NULL;
                        }
@@ -437,7 +440,7 @@ int putsgent (const struct sgrp *sgrp, FILE * fp)
                size += strlen (sgrp->sg_mem[i]) + 1;
        }
 
-       buf = malloc (size);
+       buf = MALLOCARRAY (size, char);
        if (NULL == buf) {
                return -1;
        }
index 89de2ac9def7526f8a4fa300a9711a324a404390..3864a87cff9291a73a6223e11a59ac72ce563130 100644 (file)
--- a/lib/nss.c
+++ b/lib/nss.c
@@ -6,6 +6,8 @@
 #include <strings.h>
 #include <ctype.h>
 #include <stdatomic.h>
+
+#include "alloc.h"
 #include "prototypes.h"
 #include "../libsubid/subid.h"
 #include "shadowlog_internal.h"
@@ -100,7 +102,7 @@ void nss_init(const char *nsswitch_path) {
                                subid_nss = NULL;
                                goto done;
                        }
-                       subid_nss = malloc(sizeof(*subid_nss));
+                       subid_nss = MALLOC(struct subid_nss_ops);
                        if (!subid_nss) {
                                dlclose(h);
                                goto done;
index a25e19286dcf4ac6e998b1606d4b1de7fd7d6ed4..4e966a4249bf76d63cd6a4a3e42fc803a25ed93d 100644 (file)
@@ -13,6 +13,8 @@
 #ident "$Id$"
 
 #include <stdio.h>
+
+#include "alloc.h"
 #include "defines.h"
 #include "prototypes.h"
 #include "pwio.h"
@@ -21,7 +23,7 @@
 {
        struct passwd *pw;
 
-       pw = (struct passwd *) calloc (1, sizeof *pw);
+       pw = CALLOC (1, struct passwd);
        if (NULL == pw) {
                return NULL;
        }
index bce11d37a45a58cc3ffc7e65c8124c495b040419..aa7a67e2174847589e89597decfbabea48c22850 100644 (file)
@@ -8,6 +8,8 @@
 #include <sys/wait.h>
 #include <unistd.h>
 #include <lib/prototypes.h>
+
+#include "alloc.h"
 #include "run_part.h"
 #include "shadowlog_internal.h"
 
@@ -57,7 +59,7 @@ int run_parts (const char *directory, const char *name, const char *action)
                struct stat sb;
 
                path_length=strlen(directory) + strlen(namelist[n]->d_name) + 2;
-               char *s = (char*)malloc(path_length);
+               char *s = MALLOCARRAY(path_length, char);
                if (!s) {
                        printf ("could not allocate memory\n");
                        for (; n<scanlist; n++) {
index dee9cb601b78f3c74530a828510c95f1e9000c43..55ec7d580e966f9847da0d2b95c9e3531e0f8627 100644 (file)
@@ -14,6 +14,8 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <grp.h>
+
+#include "alloc.h"
 #include "defines.h"
 #include "prototypes.h"
 
@@ -44,7 +46,7 @@ static char **list (char *s)
                   member name, or terminating NULL).  */
                if (i >= size) {
                        size = i + 100; /* at least: i + 1 */
-                       members = reallocarrayf (members, size, sizeof(char *));
+                       members = REALLOCARRAYF(members, size, char *);
                        if (!members)
                                return NULL;
                }
@@ -77,7 +79,7 @@ struct group *sgetgrent (const char *buf)
                   allocate a larger block */
                free (grpbuf);
                size = strlen (buf) + 1000;     /* at least: strlen(buf) + 1 */
-               grpbuf = malloc (size);
+               grpbuf = MALLOCARRAY (size, char);
                if (grpbuf == NULL) {
                        size = 0;
                        return NULL;
index 4b4c020dad4f3206cc1346949ae1bde43413e9bc..1255c53e367bb9e8885ac94981dece7bcfaeee69 100644 (file)
@@ -14,6 +14,7 @@
 
 #ident "$Id$"
 
+#include "alloc.h"
 #include "prototypes.h"
 #include "defines.h"
 #include "commonio.h"
@@ -25,7 +26,7 @@
        struct sgrp *sg;
        int i;
 
-       sg = (struct sgrp *) calloc (1, sizeof *sg);
+       sg = CALLOC (1, struct sgrp);
        if (NULL == sg) {
                return NULL;
        }
@@ -49,7 +50,7 @@
 
        for (i = 0; NULL != sgent->sg_adm[i]; i++);
        /*@-mustfreeonly@*/
-       sg->sg_adm = (char **) mallocarray (i + 1, sizeof (char *));
+       sg->sg_adm = MALLOCARRAY (i + 1, char *);
        /*@=mustfreeonly@*/
        if (NULL == sg->sg_adm) {
                free (sg->sg_passwd);
@@ -74,7 +75,7 @@
 
        for (i = 0; NULL != sgent->sg_mem[i]; i++);
        /*@-mustfreeonly@*/
-       sg->sg_mem = (char **) mallocarray (i + 1, sizeof (char *));
+       sg->sg_mem = MALLOCARRAY (i + 1, char *);
        /*@=mustfreeonly@*/
        if (NULL == sg->sg_mem) {
                for (i = 0; NULL != sg->sg_adm[i]; i++) {
index 9e46d0ceb4123dafb1c995aa286c99637656c640..93bcb12dde4ab00a175998fc6f4f5de9b6529eea 100644 (file)
 #include "defines.h"
 #include <shadow.h>
 #include <stdio.h>
+
+#include "alloc.h"
 #include "shadowio.h"
 
 /*@null@*/ /*@only@*/struct spwd *__spw_dup (const struct spwd *spent)
 {
        struct spwd *sp;
 
-       sp = (struct spwd *) calloc (1, sizeof *sp);
+       sp = CALLOC (1, struct spwd);
        if (NULL == sp) {
                return NULL;
        }
index 3727c8fdc5c1e5e71d5951dc7d814422fe784fcc..2da920a867bc4f9cb12bdefb3ad2dc50ee17f335 100644 (file)
@@ -6,6 +6,8 @@
 #include <stdio.h>
 #include <sys/wait.h>
 #include <sys/types.h>
+
+#include "alloc.h"
 #include "exitcodes.h"
 #include "defines.h"
 #include "prototypes.h"
@@ -24,7 +26,7 @@ int sssd_flush_cache (int dbflags)
        const char *spawnedEnv[] = {NULL};
        int i = 0;
 
-       sss_cache_args = malloc(4);
+       sss_cache_args = MALLOCARRAY(4, char);
        if (sss_cache_args == NULL) {
            return -1;
        }
index 7bb7c3b2c436a237d2caf509fd4e5298ab87000a..7fddf62f24266abd996253202489d91b62b3e8b1 100644 (file)
@@ -17,6 +17,8 @@
 #include <ctype.h>
 #include <fcntl.h>
 
+#include "alloc.h"
+
 #define ID_SIZE 31
 
 /*
@@ -32,7 +34,7 @@ static /*@null@*/ /*@only@*/void *subordinate_dup (const void *ent)
        const struct subordinate_range *rangeent = ent;
        struct subordinate_range *range;
 
-       range = (struct subordinate_range *) malloc (sizeof *range);
+       range = MALLOC (struct subordinate_range);
        if (NULL == range) {
                return NULL;
        }
@@ -314,12 +316,12 @@ static bool have_range(struct commonio_db *db,
 static bool append_range(struct subid_range **ranges, const struct subordinate_range *new, int n)
 {
        if (!*ranges) {
-               *ranges = malloc(sizeof(struct subid_range));
+               *ranges = MALLOC(struct subid_range);
                if (!*ranges)
                        return false;
        } else {
                struct subid_range *alloced;
-               alloced = reallocarray(*ranges, n + 1, sizeof(struct subid_range));
+               alloced = REALLOCARRAY(*ranges, n + 1, struct subid_range);
                if (!alloced)
                        return false;
                *ranges = alloced;
@@ -911,7 +913,7 @@ static int append_uids(uid_t **uids, const char *owner, int n)
                        return n;
        }
 
-       ret = reallocarray(*uids, n + 1, sizeof(uid_t));
+       ret = REALLOCARRAY(*uids, n + 1, uid_t);
        if (!ret) {
                free(*uids);
                return -1;
index 688e3e51f5322d31d30ee8c54ade41c61a59bfee..5440819be2a8d45c39ad35ff569314840bc4b1bb 100644 (file)
@@ -17,6 +17,8 @@
 #include <stdio.h>
 #include <grp.h>
 #include <errno.h>
+
+#include "alloc.h"
 #include "shadowlog.h"
 
 #ident "$Id$"
@@ -46,7 +48,7 @@ int add_groups (const char *list)
 
        i = 16;
        for (;;) {
-               grouplist = (gid_t *) mallocarray (i, sizeof (GETGROUPS_T));
+               grouplist = MALLOCARRAY (i, GETGROUPS_T);
                if (NULL == grouplist) {
                        return -1;
                }
@@ -88,7 +90,7 @@ int add_groups (const char *list)
                        fputs (_("Warning: too many groups\n"), shadow_logfd);
                        break;
                }
-               grouplist = (gid_t *) reallocarrayf (grouplist, (size_t)ngroups + 1, sizeof (GETGROUPS_T));
+               grouplist = REALLOCARRAYF(grouplist, (size_t) ngroups + 1, GETGROUPS_T);
                if (grouplist == NULL) {
                        return -1;
                }
index 8b45b15dba69200491f31f27eb87880131d3111b..7895b3c52315f3916284c39ec8cfcf7a47b6999c 100644 (file)
@@ -15,6 +15,7 @@
 
 #ident "$Id$"
 
+#include "alloc.h"
 #include "prototypes.h"
 
 
@@ -46,7 +47,7 @@
  *       through malloc(3).  This makes the function thread-safe, and
  *       also reduces the visibility of the buffer.
  *
- *     - agetpass() doesn't call realloc(3) internally.  Some
+ *     - agetpass() doesn't reallocate internally.  Some
  *       implementations of getpass(3), such as glibc, do that, as a
  *       consequence of calling getline(3).  That's a bug in glibc,
  *       which allows leaking prefixes of passwords in freed memory.
@@ -101,7 +102,7 @@ agetpass(const char *prompt)
         * Let's add one more byte, and if the password uses it, it
         * means the introduced password was longer than PASS_MAX.
         */
-       pass = malloc(PASS_MAX + 2);
+       pass = MALLOCARRAY(PASS_MAX + 2, char);
        if (pass == NULL)
                return NULL;
 
index 23fbd711e6b68749772585314476babc37c39e9d..b6b9ee237294225e33bcb046126550dbf810b05a 100644 (file)
@@ -228,7 +228,7 @@ static /*@exposed@*/ /*@null@*/struct link_name *check_link (const char *name, c
                return NULL;
        }
 
-       lp = (struct link_name *) xmalloc (sizeof *lp);
+       lp = XMALLOC (struct link_name);
        src_len = strlen (src_orig);
        dst_len = strlen (dst_orig);
        name_len = strlen (name);
@@ -236,7 +236,7 @@ static /*@exposed@*/ /*@null@*/struct link_name *check_link (const char *name, c
        lp->ln_ino = sb->st_ino;
        lp->ln_count = sb->st_nlink;
        len = name_len - src_len + dst_len + 1;
-       lp->ln_name = (char *) xmalloc (len);
+       lp->ln_name = XMALLOCARRAY (len, char);
        (void) snprintf (lp->ln_name, len, "%s%s", dst_orig, name + src_len);
        lp->ln_next = links;
        links = lp;
@@ -326,8 +326,8 @@ static int copy_tree_impl (const struct path_info *src, const struct path_info *
                        src_len += strlen (src->full_path);
                        dst_len += strlen (dst->full_path);
 
-                       src_name = (char *) malloc (src_len);
-                       dst_name = (char *) malloc (dst_len);
+                       src_name = MALLOCARRAY (src_len, char);
+                       dst_name = MALLOCARRAY (dst_len, char);
 
                        if ((NULL == src_name) || (NULL == dst_name)) {
                                err = -1;
@@ -561,7 +561,7 @@ static /*@null@*/char *readlink_malloc (const char *filename)
 
        while (true) {
                ssize_t nchars;
-               char *buffer = (char *) malloc (size);
+               char *buffer = MALLOCARRAY (size, char);
                if (NULL == buffer) {
                        return NULL;
                }
@@ -626,7 +626,7 @@ static int copy_symlink (const struct path_info *src, const struct path_info *ds
         */
        if (strncmp (oldlink, src_orig, strlen (src_orig)) == 0) {
                size_t len = strlen (dst_orig) + strlen (oldlink) - strlen (src_orig) + 1;
-               char *dummy = (char *) xmalloc (len);
+               char *dummy = XMALLOCARRAY (len, char);
                (void) snprintf (dummy, len, "%s%s",
                                 dst_orig,
                                 oldlink + strlen (src_orig));
index 5b4eec13e1b7aa8a3713e3626648edba3ca25c95..75c7c8c6a781e504947e7078522e5ce43ca8676f 100644 (file)
@@ -60,7 +60,7 @@ static const char *const noslash[] = {
  */
 void initenv (void)
 {
-       newenvp = (char **) xmallocarray (NEWENVP_STEP, sizeof (char *));
+       newenvp = XMALLOCARRAY (NEWENVP_STEP, char *);
        *newenvp = NULL;
 }
 
@@ -74,7 +74,7 @@ void addenv (const char *string, /*@null@*/const char *value)
        if (NULL != value) {
                size_t len = strlen (string) + strlen (value) + 2;
                int wlen;
-               newstring = xmalloc (len);
+               newstring = XMALLOCARRAY (len, char);
                wlen = snprintf (newstring, len, "%s=%s", string, value);
                assert (wlen == (int) len -1);
        } else {
@@ -135,8 +135,7 @@ void addenv (const char *string, /*@null@*/const char *value)
                 * happily go on, else print a message.
                 */
 
-               __newenvp = (char **) reallocarray (newenvp, newenvc + NEWENVP_STEP,
-                                                   sizeof (char *));
+               __newenvp = REALLOCARRAY(newenvp, newenvc + NEWENVP_STEP, char *);
 
                if (NULL != __newenvp) {
                        /*
index 808cd7ce5f81bea45ef68a22f009c10ca9842456..cfd560982f6d528a0a80c3edfac0dbaa3c920ba6 100644 (file)
@@ -12,6 +12,7 @@
 #include <stdio.h>
 #include <errno.h>
 
+#include "alloc.h"
 #include "prototypes.h"
 #include "groupio.h"
 #include "getdef.h"
@@ -231,7 +232,7 @@ int find_new_gid (bool sys_group,
         */
 
        /* Create an array to hold all of the discovered GIDs */
-       used_gids = calloc (gid_max + 1, sizeof (bool));
+       used_gids = CALLOC (gid_max + 1, bool);
        if (NULL == used_gids) {
                fprintf (log_get_logfd(),
                         _("%s: failed to allocate memory: %s\n"),
index 1be646f2b3512b0cb5194add0bd1a71c6fe07782..e107a6fb8a3f39d7ec1f365f5598d518393f6591 100644 (file)
@@ -12,6 +12,7 @@
 #include <stdio.h>
 #include <errno.h>
 
+#include "alloc.h"
 #include "prototypes.h"
 #include "pwio.h"
 #include "getdef.h"
@@ -231,7 +232,7 @@ int find_new_uid(bool sys_user,
         */
 
        /* Create an array to hold all of the discovered UIDs */
-       used_uids = mallocarray (uid_max + 1, sizeof (bool));
+       used_uids = MALLOCARRAY (uid_max + 1, bool);
        if (NULL == used_uids) {
                fprintf (log_get_logfd(),
                         _("%s: failed to allocate memory: %s\n"),
index 1c6e719f0f71b736618bc7ad71c82463f4cbc850..f6b4a8e74a1960207127c2336798ac882de70a4d 100644 (file)
@@ -11,6 +11,8 @@
 #include <limits.h>
 #include <stdlib.h>
 #include <stdio.h>
+
+#include "alloc.h"
 #include "prototypes.h"
 #include "stpeprintf.h"
 #include "idmapping.h"
@@ -44,7 +46,7 @@ struct map_range *get_map_ranges(int ranges, int argc, char **argv)
                return NULL;
        }
 
-       mappings = calloc(ranges, sizeof(*mappings));
+       mappings = CALLOC(ranges, struct map_range);
        if (!mappings) {
                fprintf(log_get_logfd(), _( "%s: Memory allocation failure\n"),
                        log_get_progname());
@@ -189,7 +191,7 @@ void write_mapping(int proc_dir_fd, int ranges, const struct map_range *mappings
 #endif
 
        bufsize = ranges * ((ULONG_DIGITS + 1) * 3);
-       pos = buf = xmalloc(bufsize);
+       pos = buf = XMALLOCARRAY(bufsize, char);
        end = buf + bufsize;
 
        /* Build the mapping command */
index 2b8f155aa6681811069a093154f363c03a8b3dd4..ed76e8ff524bf53ebab1a5285a784a19a8f01c40 100644 (file)
@@ -11,6 +11,8 @@
 #ident "$Id$"
 
 #include <assert.h>
+
+#include "alloc.h"
 #include "prototypes.h"
 #include "defines.h"
 /*
@@ -44,7 +46,7 @@
         * old entries, and the new entries as well.
         */
 
-       tmp = (char **) xmallocarray (i + 2, sizeof member);
+       tmp = XMALLOCARRAY (i + 2, char *);
 
        /*
         * Copy the original list to the new list, then append the
         * old entries.
         */
 
-       tmp = (char **) xmallocarray (j + 1, sizeof member);
+       tmp = XMALLOCARRAY (j + 1, char *);
 
        /*
         * Copy the original list except the deleted members to the
 
        for (i = 0; NULL != list[i]; i++);
 
-       tmp = (char **) xmallocarray (i + 1, sizeof (char *));
+       tmp = XMALLOCARRAY (i + 1, char *);
 
        i = 0;
        while (NULL != *list) {
@@ -210,7 +212,7 @@ bool is_on_list (char *const *list, const char *member)
         * Allocate the array we're going to store the pointers into.
         */
 
-       array = (char **) xmallocarray (i, sizeof (char *));
+       array = XMALLOCARRAY (i, char *);
 
        /*
         * Empty list is special - 0 members, not 1 empty member.  --marekm
index f39faf00e1376b1b412bab3f2254b62f8991ec8c..db1b7313ea34da336cd55390d7fd40436f9ac552 100644 (file)
@@ -15,6 +15,8 @@
 #include <stdio.h>
 #include <signal.h>
 #include <ctype.h>
+
+#include "alloc.h"
 #include "prototypes.h"
 #include "defines.h"
 #include "getdef.h"
@@ -130,7 +132,7 @@ void login_prompt (const char *prompt, char *name, int namesize)
                                envp[envc] = nvar;
                        } else {
                                size_t len = strlen (nvar) + 32;
-                               envp[envc] = xmalloc (len);
+                               envp[envc] = XMALLOCARRAY (len, char);
                                (void) snprintf (envp[envc], len,
                                                 "L%d=%s", count++, nvar);
                        }
index 647f879c035c5b72b9ba5cb3116cce874a1bfcd9..c8af2ee3f3cb5760aef119ddfe8efde8fa476665 100644 (file)
@@ -15,6 +15,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "alloc.h"
 #include "getdef.h"
 
 #ident "$Id$"
@@ -38,7 +39,7 @@ void mailcheck (void)
                size_t len = strlen (mailbox) + 5;
                int wlen;
 
-               newmail = xmalloc (len);
+               newmail = XMALLOCARRAY (len, char);
                wlen = snprintf (newmail, len, "%s/new", mailbox);
                assert (wlen == (int) len - 1);
 
index e7b185ec7a7c1544be8904fbadd1473584ee49c1..90bfeb9b65ffc64d5b3dd2e5e1a537d59c106cb6 100644 (file)
@@ -21,6 +21,8 @@
  */
 #include <ctype.h>
 #include <stdio.h>
+
+#include "alloc.h"
 #include "prototypes.h"
 #include "defines.h"
 #include "getdef.h"
@@ -158,7 +160,7 @@ static /*@observer@*//*@null@*/const char *password_check (
 
        newmono = str_lower (xstrdup (new));
        oldmono = str_lower (xstrdup (old));
-       wrapped = xmalloc (strlen (oldmono) * 2 + 1);
+       wrapped = XMALLOCARRAY (strlen (oldmono) * 2 + 1, char);
        strcpy (wrapped, oldmono);
        strcat (wrapped, oldmono);
 
index 46c41ef338dc2a0d8a763859d0a0b3f1cce329c8..cd4d78e00286945bfb428a271b963494a05cd26a 100644 (file)
@@ -14,6 +14,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <security/pam_appl.h>
+
+#include "alloc.h"
 #include "prototypes.h"
 #include "shadowlog.h"
 
@@ -43,7 +45,7 @@ static int ni_conv (int num_msg,
                return PAM_CONV_ERR;
        }
 
-       responses = (struct pam_response *) calloc (num_msg, sizeof (*responses));
+       responses = CALLOC (num_msg, struct pam_response);
        if (NULL == responses) {
                return PAM_CONV_ERR;
        }
index 243d00d45ca08443e0d21878fb673a0e3e933ef7..56243f2e1d6162626df8f727b0e46a36b8495f47 100644 (file)
@@ -11,7 +11,9 @@
 
 #include <stdio.h>
 #include <assert.h>
+
 #include "defines.h"
+#include "alloc.h"
 #include "prototypes.h"
 /*@-exitarg@*/
 #include "exitcodes.h"
@@ -95,18 +97,18 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
                }
                size_t len;
                len = strlen(prefix) + strlen(PASSWD_FILE) + 2;
-               passwd_db_file = xmalloc(len);
+               passwd_db_file = XMALLOCARRAY(len, char);
                snprintf(passwd_db_file, len, "%s/%s", prefix, PASSWD_FILE);
                pw_setdbname(passwd_db_file);
 
                len = strlen(prefix) + strlen(GROUP_FILE) + 2;
-               group_db_file = xmalloc(len);
+               group_db_file = XMALLOCARRAY(len, char);
                snprintf(group_db_file, len, "%s/%s", prefix, GROUP_FILE);
                gr_setdbname(group_db_file);
 
 #ifdef  SHADOWGRP
                len = strlen(prefix) + strlen(SGROUP_FILE) + 2;
-               sgroup_db_file = xmalloc(len);
+               sgroup_db_file = XMALLOCARRAY(len, char);
                snprintf(sgroup_db_file, len, "%s/%s", prefix, SGROUP_FILE);
                sgr_setdbname(sgroup_db_file);
 #endif
@@ -115,18 +117,18 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
 #endif
 
                len = strlen(prefix) + strlen(SHADOW_FILE) + 2;
-               spw_db_file = xmalloc(len);
+               spw_db_file = XMALLOCARRAY(len, char);
                snprintf(spw_db_file, len, "%s/%s", prefix, SHADOW_FILE);
                spw_setdbname(spw_db_file);
 
 #ifdef ENABLE_SUBIDS
                len = strlen(prefix) + strlen("/etc/subuid") + 2;
-               suid_db_file = xmalloc(len);
+               suid_db_file = XMALLOCARRAY(len, char);
                snprintf(suid_db_file, len, "%s/%s", prefix, "/etc/subuid");
                sub_uid_setdbname(suid_db_file);
 
                len = strlen(prefix) + strlen("/etc/subgid") + 2;
-               sgid_db_file = xmalloc(len);
+               sgid_db_file = XMALLOCARRAY(len, char);
                snprintf(sgid_db_file, len, "%s/%s", prefix, "/etc/subgid");
                sub_gid_setdbname(sgid_db_file);
 #endif
@@ -135,7 +137,7 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
                setdef_config_file(prefix);
 #else
                len = strlen(prefix) + strlen("/etc/login.defs") + 2;
-               def_conf_file = xmalloc(len);
+               def_conf_file = XMALLOCARRAY(len, char);
                snprintf(def_conf_file, len, "%s/%s", prefix, "/etc/login.defs");
                setdef_config_file(def_conf_file);
 #endif
index 5d7aefa25c910a6801a7ad90709fcc231a8ef0d5..f644dad3778c54ebe1ef3e3bae31ec3b7607d833 100644 (file)
@@ -20,6 +20,8 @@
 #include <sys/stat.h>
 #include <stdio.h>
 #include <ctype.h>
+
+#include "alloc.h"
 #include "prototypes.h"
 #include "defines.h"
 #include <pwd.h>
@@ -34,7 +36,7 @@ addenv_path (const char *varname, const char *dirname, const char *filename)
        size_t len = strlen (dirname) + strlen (filename) + 2;
        int wlen;
 
-       buf = xmalloc (len);
+       buf = XMALLOCARRAY (len, char);
        wlen = snprintf (buf, len, "%s/%s", dirname, filename);
        assert (wlen == (int) len - 1);
 
index 1ae9fd54ad376374cb4744146041967679dbf614..ff6acee0b2657714c0da462e2dbdadc2bac37f50 100644 (file)
@@ -20,6 +20,8 @@
 #include <netdb.h>
 #include <stdio.h>
 
+#include "alloc.h"
+
 #ident "$Id$"
 
 
@@ -93,7 +95,7 @@ static bool is_my_tty (const char *tty)
        }
 
        if (NULL != ut) {
-               ret = (struct utmp *) xmalloc (sizeof (*ret));
+               ret = XMALLOC (struct utmp);
                memcpy (ret, ut, sizeof (*ret));
        }
 
@@ -158,12 +160,12 @@ static void updwtmp (const char *filename, const struct utmp *ut)
 
        if (   (NULL != host)
            && ('\0' != host[0])) {
-               hostname = (char *) xmalloc (strlen (host) + 1);
+               hostname = XMALLOCARRAY (strlen (host) + 1, char);
                strcpy (hostname, host);
 #ifdef HAVE_STRUCT_UTMP_UT_HOST
        } else if (   (NULL != ut)
                   && ('\0' != ut->ut_host[0])) {
-               hostname = (char *) xmalloc (sizeof (ut->ut_host) + 1);
+               hostname = XMALLOCARRAY (sizeof (ut->ut_host) + 1, char);
                strncpy (hostname, ut->ut_host, sizeof (ut->ut_host));
                hostname[sizeof (ut->ut_host)] = '\0';
 #endif                         /* HAVE_STRUCT_UTMP_UT_HOST */
@@ -174,7 +176,7 @@ static void updwtmp (const char *filename, const struct utmp *ut)
        }
 
 
-       utent = (struct utmp *) xcalloc (1, sizeof (*utent));
+       utent = XCALLOC (1, struct utmp);
 
 
 #ifdef HAVE_STRUCT_UTMP_UT_TYPE
index 132e2cbc0e87c4d6d976e9bb8cddca1ebcb1c4b7..e1bf70f67c6bdeaa7135efe48de227ccaf71bf6e 100644 (file)
@@ -30,6 +30,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>
+
+#include "alloc.h"
 #include "prototypes.h"
 #include "shadowlog.h"
 
@@ -50,7 +52,7 @@
        /* we have to start with something */
        size_t length = 0x100;
 
-       result = malloc(sizeof(LOOKUP_TYPE));
+       result = MALLOC(LOOKUP_TYPE);
        if (NULL == result) {
                fprintf (log_get_logfd(), _("%s: out of memory\n"),
                         "x" STRINGIZE(FUNCTION_NAME));
@@ -60,7 +62,7 @@
        while (true) {
                int status;
                LOOKUP_TYPE *resbuf = NULL;
-               buffer = (char *)xreallocarray (buffer, length, sizeof(char));
+               buffer = XREALLOCARRAY (buffer, length, char);
                status = REENTRANT_NAME(ARG_NAME, result, buffer,
                                        length, &resbuf);
                if ((0 == status) && (resbuf == result)) {
index cb78c2aa0f68d49329501612c40d6cdce32826df..9da92d1417e4fb37f2c478a3bc30b66eac5159bb 100644 (file)
@@ -19,6 +19,8 @@
 #include <signal.h>
 #include <stdio.h>
 #include <sys/types.h>
+
+#include "alloc.h"
 #include "defines.h"
 #include "groupio.h"
 #include "nscd.h"
@@ -834,7 +836,7 @@ static void get_group (struct group *gr)
 
                        sg->sg_mem = dup_list (gr->gr_mem);
 
-                       sg->sg_adm = (char **) xmallocarray (2, sizeof (char *));
+                       sg->sg_adm = XMALLOCARRAY (2, char *);
 #ifdef FIRST_MEMBER_IS_ADMIN
                        if (sg->sg_mem[0]) {
                                sg->sg_adm[0] = xstrdup (sg->sg_mem[0]);
index 50c5fc868b01633bc8b034f4d99b19eeb62502a8..f241a5cc10db21afdfa9d647e6f343d5d1aa6d30 100644 (file)
@@ -18,6 +18,8 @@
 #include "pam_defs.h"
 #endif                         /* USE_PAM */
 #include <pwd.h>
+
+#include "alloc.h"
 #include "defines.h"
 #include "prototypes.h"
 #include "groupio.h"
@@ -125,7 +127,7 @@ static void add_user (const char *user,
                        static struct sgrp sgrent;
                        sgrent.sg_name = xstrdup (newgrp->gr_name);
                        sgrent.sg_mem = dup_list (newgrp->gr_mem);
-                       sgrent.sg_adm = (char **) xmalloc (sizeof (char *));
+                       sgrent.sg_adm = XMALLOC (char *);
 #ifdef FIRST_MEMBER_IS_ADMIN
                        if (sgrent.sg_mem[0]) {
                                sgrent.sg_adm[0] = xstrdup (sgrent.sg_mem[0]);
@@ -208,7 +210,7 @@ static void remove_user (const char *user,
                        static struct sgrp sgrent;
                        sgrent.sg_name = xstrdup (newgrp->gr_name);
                        sgrent.sg_mem = dup_list (newgrp->gr_mem);
-                       sgrent.sg_adm = (char **) xmalloc (sizeof (char *));
+                       sgrent.sg_adm = XMALLOC (char *);
 #ifdef FIRST_MEMBER_IS_ADMIN
                        if (sgrent.sg_mem[0]) {
                                sgrent.sg_adm[0] = xstrdup (sgrent.sg_mem[0]);
@@ -281,9 +283,9 @@ static void purge_members (const struct group *grp)
                        /* Create a shadow group based on this group */
                        static struct sgrp sgrent;
                        sgrent.sg_name = xstrdup (newgrp->gr_name);
-                       sgrent.sg_mem = (char **) xmalloc (sizeof (char *));
+                       sgrent.sg_mem = XMALLOC (char *);
                        sgrent.sg_mem[0] = NULL;
-                       sgrent.sg_adm = (char **) xmalloc (sizeof (char *));
+                       sgrent.sg_adm = XMALLOC (char *);
                        sgrent.sg_adm[0] = NULL;
 
                        /* Move any password to gshadow */
index a2facfb80d8109d763882fbca84cdb46ff8f84fa..a689d3f8d6827963928d349f23a00532dbdd11a2 100644 (file)
@@ -24,6 +24,8 @@
 #include <pwd.h>
 #endif                         /* USE_PAM */
 #endif                         /* ACCT_TOOLS_SETUID */
+
+#include "alloc.h"
 #include "chkname.h"
 #include "defines.h"
 #include "groupio.h"
@@ -249,7 +251,7 @@ static void grp_update (void)
                        // requested to replace the existing groups
                        if (NULL != grp.gr_mem[0])
                                gr_free_members(&grp);
-                       grp.gr_mem = (char **)xmalloc(sizeof(char *));
+                       grp.gr_mem = XMALLOC(char *);
                        grp.gr_mem[0] = NULL;
                } else {
                        // append to existing groups
@@ -557,15 +559,15 @@ static void prepare_failure_reports (void)
 #endif
        info_passwd.name  = group_name;
 
-       gr                     = xmalloc (512);
+       gr                     = XMALLOCARRAY(512, char);
        info_group.audit_msg   = gr;
        gr_end                 = gr + 512;
 #ifdef SHADOWGRP
-       sgr                    = xmalloc (512);
+       sgr                    = XMALLOCARRAY(512, char);
        info_gshadow.audit_msg = sgr;
        sgr_end                = sgr + 512;
 #endif
-       pw                     = xmalloc (512);
+       pw                     = XMALLOCARRAY(512, char);
        info_passwd.audit_msg  = pw;
        pw_end                 = pw + 512;
 
index bfa548fde05392e842a9ef0d2b8f9bb3f73e600b..1d1c646e857d051e2b91872d968e04228d20c222 100644 (file)
 #include <grp.h>
 #include <pwd.h>
 #include <stdio.h>
+
+#include "alloc.h"
 #include "defines.h"
 #include "prototypes.h"
 #include "shadowlog.h"
+
 /*
  * Global variables
  */
@@ -88,7 +91,7 @@ int main (int argc, char **argv)
        GETGROUPS_T *groups;
 
        sys_ngroups = sysconf (_SC_NGROUPS_MAX);
-       groups = (GETGROUPS_T *) mallocarray (sys_ngroups, sizeof (GETGROUPS_T));
+       groups = MALLOCARRAY (sys_ngroups, GETGROUPS_T);
 
        (void) setlocale (LC_ALL, "");
        (void) bindtextdomain (PACKAGE, LOCALEDIR);
index e5a75f4a2c385018868f64ef02aa0a078a1f84dd..3bc61444f6c0625c9040426b116e49531d3f5bbd 100644 (file)
--- a/src/id.c
+++ b/src/id.c
 #include <pwd.h>
 #include <stdio.h>
 #include <sys/types.h>
+
+#include "alloc.h"
 #include "defines.h"
+
 /* local function prototypes */
 static void usage (void);
 
@@ -63,7 +66,7 @@ static void usage (void)
         * work if the system library is recompiled.
         */
        sys_ngroups = sysconf (_SC_NGROUPS_MAX);
-       groups = (GETGROUPS_T *) mallocarray (sys_ngroups, sizeof (GETGROUPS_T));
+       groups = MALLOCARRAY (sys_ngroups, GETGROUPS_T);
 
        /*
         * See if the -a flag has been given to print out the concurrent
index 5f31a41743bc0d3a479b82655dc13d9f952c0c81..e5c6beff66c93c7fc4d98375c5232766a017550b 100644 (file)
@@ -22,6 +22,8 @@
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <assert.h>
+
+#include "alloc.h"
 #include "defines.h"
 #include "faillog.h"
 #include "failure.h"
@@ -589,7 +591,7 @@ int main (int argc, char **argv)
 #ifdef RLOGIN
        if (rflg) {
                assert (NULL == username);
-               username = xmalloc (USER_NAME_MAX_LENGTH + 1);
+               username = XMALLOCARRAY (USER_NAME_MAX_LENGTH + 1, char);
                username[USER_NAME_MAX_LENGTH] = '\0';
                if (do_rlogin (hostname, username, USER_NAME_MAX_LENGTH, term, sizeof term)) {
                        preauth_flag = true;
@@ -906,7 +908,7 @@ int main (int argc, char **argv)
                                exit (1);
                        }
                        preauth_flag = false;
-                       username = xmalloc (USER_NAME_MAX_LENGTH + 1);
+                       username = XMALLOCARRAY (USER_NAME_MAX_LENGTH + 1, char);
                        username[USER_NAME_MAX_LENGTH] = '\0';
                        login_prompt (_("\n%s login: "), username, USER_NAME_MAX_LENGTH);
 
index 87fd779629e88b117eca73451145a39cedd76624..f8387f11cbe4bc145ccc04ba9f73b113b9312a89 100644 (file)
@@ -16,6 +16,8 @@
 #include <pwd.h>
 #include <stdio.h>
 #include <assert.h>
+
+#include "alloc.h"
 #include "defines.h"
 #include "getdef.h"
 #include "prototypes.h"
@@ -531,7 +533,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 *) xmallocarray (i, sizeof (GETGROUPS_T));
+               grouplist = XMALLOCARRAY (i, GETGROUPS_T);
                ngroups = getgroups (i, grouplist);
                if (i > ngroups && !(ngroups == -1 && errno == EINVAL)) {
                        break;
index 315ec3c6ee0d81ba476f095280f6d9b10ab9a0c2..dd6b442120a7a633b90c2e5458bd97f0de6afb62 100644 (file)
@@ -29,6 +29,8 @@
 #include <ctype.h>
 #include <errno.h>
 #include <string.h>
+
+#include "alloc.h"
 #ifdef ACCT_TOOLS_SETUID
 #ifdef USE_PAM
 #include "pam_defs.h"
@@ -1200,9 +1202,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,     nusers, sizeof (lines[0]));
-               usernames = reallocf (usernames, nusers, sizeof (usernames[0]));
-               passwords = reallocf (passwords, nusers, sizeof (passwords[0]));
+               lines     = REALLOCARRAYF(lines, nusers, int);
+               usernames = REALLOCARRAYF(usernames, nusers, char *);
+               passwords = REALLOCARRAYF(passwords, nusers, char *);
                if (lines == NULL || usernames == NULL || passwords == NULL) {
                        fprintf (stderr,
                                 _("%s: line %d: %s\n"),
index bf07667392fc47b7453c456609f5bf6ac318fd16..ed95bff3f61f2d3a837b0f92f94c93420558feca 100644 (file)
@@ -19,6 +19,8 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <time.h>
+
+#include "alloc.h"
 #include "defines.h"
 #include "getdef.h"
 #include "nscd.h"
@@ -524,7 +526,7 @@ static char *update_crypt_pw (char *cp)
        }
 
        if (lflg && *cp != '!') {
-               char *newpw = xmalloc (strlen (cp) + 2);
+               char *newpw = XMALLOCARRAY (strlen (cp) + 2, char);
 
                strcpy (newpw, "!");
                strcat (newpw, cp);
index f7777964692c3e0abbc6b642834f40315dceb79a..9c134a9be296601e5da1713b2af0180ad9dc7e5c 100644 (file)
--- a/src/su.c
+++ b/src/su.c
@@ -45,6 +45,8 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #endif                         /* !USE_PAM */
+
+#include "alloc.h"
 #include "prototypes.h"
 #include "defines.h"
 #include "pwauth.h"
@@ -238,7 +240,7 @@ static void execve_shell (const char *shellname,
                while (NULL != args[n_args]) {
                        n_args++;
                }
-               targs = (char **) xmallocarray (n_args + 3, sizeof (args[0]));
+               targs = XMALLOCARRAY (n_args + 3, char *);
                targs[0] = "sh";
                targs[1] = "-";
                targs[2] = xstrdup (shellname);
@@ -1176,7 +1178,7 @@ int main (int argc, char **argv)
                        cp = Basename (shellstr);
                }
 
-               arg0 = xmalloc (strlen (cp) + 2);
+               arg0 = XMALLOCARRAY (strlen (cp) + 2, char);
                arg0[0] = '-';
                strcpy (arg0 + 1, cp);
                cp = arg0;
index b3698aa5aca24df65998143411f8afae6ddbabd0..c0d8eda9bc3f35950fe0f7d500b3e65d8e47de43 100644 (file)
@@ -32,6 +32,8 @@
 #include <sys/wait.h>
 #include <time.h>
 #include <unistd.h>
+
+#include "alloc.h"
 #include "chkname.h"
 #include "defines.h"
 #include "faillog.h"
@@ -355,7 +357,7 @@ static void get_defaults (void)
                int wlen;
 
                len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2;
-               default_file = malloc(len);
+               default_file = MALLOCARRAY(len, char);
                 if (default_file == NULL)
                        return;
                wlen = snprintf(default_file, len, "%s/%s", prefix, USER_DEFAULTS_FILE);
@@ -468,7 +470,7 @@ static void get_defaults (void)
                                char* _def_template; /* avoid const warning */
 
                                len = strlen(prefix) + strlen(cp) + 2;
-                               _def_template = xmalloc(len);
+                               _def_template = XMALLOCARRAY(len, char);
                                wlen = snprintf(_def_template, len, "%s/%s", prefix, cp);
                                assert (wlen == (int) len -1);
                                def_template = _def_template;
@@ -492,7 +494,7 @@ static void get_defaults (void)
                                char* _def_usrtemplate; /* avoid const warning */
 
                                len = strlen(prefix) + strlen(cp) + 2;
-                               _def_usrtemplate = xmalloc(len);
+                               _def_usrtemplate = XMALLOCARRAY(len, char);
                                wlen = snprintf(_def_usrtemplate, len, "%s/%s", prefix, cp);
                                assert (wlen == (int) len -1);
                                def_usrtemplate = _def_usrtemplate;
@@ -582,7 +584,7 @@ static int set_defaults (void)
 
 
        len = strlen(prefix) + strlen(NEW_USER_FILE) + 2;
-       new_file = malloc(len);
+       new_file = MALLOCARRAY(len, char);
         if (new_file == NULL) {
                fprintf (stderr,
                         _("%s: cannot create new defaults file: %s\n"),
@@ -594,7 +596,7 @@ static int set_defaults (void)
 
        if (prefix[0]) {
                len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2;
-               default_file = malloc(len);
+               default_file = MALLOCARRAY(len, char);
                if (default_file == NULL) {
                        fprintf (stderr,
                                 _("%s: cannot create new defaults file: %s\n"),
@@ -1610,7 +1612,7 @@ static void process_flags (int argc, char **argv)
                        size_t len = strlen (def_home) + strlen (user_name) + 2;
                        int wlen;
 
-                       uh = xmalloc (len);
+                       uh = XMALLOCARRAY (len, char);
                        wlen = snprintf (uh, len, "%s/%s", def_home, user_name);
                        assert (wlen == (int) len -1);
 
@@ -1620,7 +1622,7 @@ static void process_flags (int argc, char **argv)
                        size_t len = strlen(prefix) + strlen(user_home) + 2;
                        int wlen;
                        char* _prefix_user_home; /* to avoid const warning */
-                       _prefix_user_home = xmalloc(len);
+                       _prefix_user_home = XMALLOCARRAY(len, char);
                        wlen = snprintf(_prefix_user_home, len, "%s/%s", prefix, user_home);
                        assert (wlen == (int) len -1);
                        prefix_user_home = _prefix_user_home;
@@ -2429,7 +2431,7 @@ static void create_mail (void)
                if (NULL == spool) {
                        return;
                }
-               file = alloca (strlen (prefix) + strlen (spool) + strlen (user_name) + 3);
+               file = ALLOCARRAY (strlen (prefix) + strlen (spool) + strlen (user_name) + 3, char);
                if (prefix[0])
                        sprintf (file, "%s/%s/%s", prefix, spool, user_name);
                else
@@ -2539,7 +2541,7 @@ int main (int argc, char **argv)
 #endif
 
        sys_ngroups = sysconf (_SC_NGROUPS_MAX);
-       user_groups = (char **) xmallocarray (1 + sys_ngroups, sizeof (char *));
+       user_groups = XMALLOCARRAY (1 + sys_ngroups, char *);
        /*
         * Initialize the list to be empty
         */
index 2fa44934527b1b00256be7dfc33ddee016f381df..5296924968a78f1bbeb7d1f857984f156300536a 100644 (file)
@@ -19,6 +19,8 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+
+#include "alloc.h"
 #ifdef ACCT_TOOLS_SETUID
 #ifdef USE_PAM
 #include "pam_defs.h"
@@ -803,7 +805,7 @@ static int remove_mailbox (void)
        }
 
        len = strlen (prefix) + strlen (maildir) + strlen (user_name) + 2;
-       mailfile = xmalloc (len);
+       mailfile = XMALLOCARRAY (len, char);
 
        if (prefix[0]) {
                (void) snprintf (mailfile, len, "%s/%s/%s",
@@ -917,7 +919,7 @@ static int remove_tcbdir (const char *user_name, uid_t user_id)
                return 0;
        }
 
-       buf = malloc (buflen);
+       buf = MALLOCARRAY (buflen, char);
        if (NULL == buf) {
                fprintf (stderr, _("%s: Can't allocate memory, "
                                   "tcb entry for %s not removed.\n"),
@@ -1129,7 +1131,7 @@ int main (int argc, char **argv)
 
                        size_t len = strlen(prefix) + strlen(pwd->pw_dir) + 2;
                        int wlen;
-                       user_home = xmalloc(len);
+                       user_home = XMALLOCARRAY(len, char);
                        wlen = snprintf(user_home, len, "%s/%s", prefix, pwd->pw_dir);
                        assert (wlen == (int) len -1);
                }
index aa2d684fa9e4b0e585f45ef65ae67e1c667f0aa3..17cfac91b6f72754c864da0c4dc653fa99ae98f1 100644 (file)
@@ -28,6 +28,8 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <time.h>
+
+#include "alloc.h"
 #include "chkname.h"
 #include "defines.h"
 #include "faillog.h"
@@ -342,7 +344,7 @@ static int prepend_range(const char *str, struct ulong_range_list_entry **head)
        if (range.first > range.last)
                return 0;
 
-       entry = malloc(sizeof(*entry));
+       entry = MALLOC(struct ulong_range_list_entry);
        if (!entry) {
                fprintf (stderr,
                        _("%s: failed to allocate memory: %s\n"),
@@ -415,7 +417,7 @@ usage (int status)
 static char *new_pw_passwd (char *pw_pass)
 {
        if (Lflg && ('!' != pw_pass[0])) {
-               char *buf = xmalloc (strlen (pw_pass) + 2);
+               char *buf = XMALLOCARRAY (strlen (pw_pass) + 2, char);
 
 #ifdef WITH_AUDIT
                audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
@@ -1258,12 +1260,12 @@ static void process_flags (int argc, char **argv)
        if (prefix[0]) {
                size_t len = strlen(prefix) + strlen(user_home) + 2;
                int wlen;
-               prefix_user_home = xmalloc(len);
+               prefix_user_home = XMALLOCARRAY(len, char);
                wlen = snprintf(prefix_user_home, len, "%s/%s", prefix, user_home);
                assert (wlen == (int) len -1);
                if (user_newhome) {
                        len = strlen(prefix) + strlen(user_newhome) + 2;
-                       prefix_user_newhome = xmalloc(len);
+                       prefix_user_newhome = XMALLOCARRAY(len, char);
                        wlen = snprintf(prefix_user_newhome, len, "%s/%s", prefix, user_newhome);
                        assert (wlen == (int) len -1);
                }
@@ -2038,7 +2040,7 @@ static void move_mailbox (void)
                return;
        }
        len = strlen (prefix) + strlen (maildir) + strlen (user_name) + 2;
-       mailfile = alloca (len);
+       mailfile = ALLOCARRAY (len, char);
 
        /*
         * O_NONBLOCK is to make sure open won't hang on mandatory locks.
@@ -2093,7 +2095,7 @@ static void move_mailbox (void)
 
        if (lflg) {
                len = strlen (prefix) + strlen (maildir) + strlen (user_newname) + 2;
-               newmailfile = alloca(len);
+               newmailfile = ALLOCARRAY(len, char);
                if (prefix[0]) {
                        (void) snprintf (newmailfile, len, "%s/%s/%s",
                                         prefix, maildir, user_newname);
@@ -2150,7 +2152,7 @@ int main (int argc, char **argv)
 #endif
 
        sys_ngroups = sysconf (_SC_NGROUPS_MAX);
-       user_groups = (char **) mallocarray (sys_ngroups + 1, sizeof (char *));
+       user_groups = MALLOCARRAY (sys_ngroups + 1, char *);
        user_groups[0] = NULL;
 
        is_shadow_pwd = spw_file_present ();
index 488a97d9829cb3d3a8b25a1c9ede558af9890a9f..1c6357f95f824f8e753a7e4e9348aff243d178e3 100644 (file)
@@ -26,6 +26,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <utime.h>
+
+#include "alloc.h"
 #include "defines.h"
 #include "groupio.h"
 #include "nscd.h"
@@ -302,7 +304,7 @@ vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (void))
                                continue;
                }
 
-               buf = (char *) malloc (strlen (editor) + strlen (fileedit) + 2);
+               buf = MALLOCARRAY(strlen(editor) + strlen(fileedit) + 2, char);
                snprintf (buf, strlen (editor) + strlen (fileedit) + 2,
                          "%s %s", editor, fileedit);
                status = system (buf);
@@ -418,7 +420,7 @@ vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (void))
                if (stat (file, &st1) != 0) {
                        vipwexit (_("failed to stat edited file"), errno, 1);
                }
-               to_rename = malloc (strlen (file) + 2);
+               to_rename = MALLOCARRAY (strlen (file) + 2, char);
                if (NULL == to_rename) {
                        vipwexit (_("failed to allocate memory"), errno, 1);
                }
index 957e25e85220a40b56d0c4513d062a7307fa37d4..892008799b9aac479dfacc5062fd2c8f4ddb2e9d 100644 (file)
@@ -76,7 +76,7 @@ static uid_t getnamuid(const char *name) {
 }
 
 static int alloc_uid(uid_t **uids, uid_t id) {
-       *uids = malloc(sizeof(uid_t));
+       *uids = MALLOC(uid_t);
        if (!*uids)
                return -1;
        *uids[0] = id;
@@ -121,7 +121,7 @@ enum subid_status shadow_subid_list_owner_ranges(const char *owner, enum subid_t
                return SUBID_STATUS_SUCCESS;
        if (id_type == ID_TYPE_UID && strcmp(owner, "group1") == 0)
                return SUBID_STATUS_SUCCESS;
-       ranges = (struct subid_range *)malloc(sizeof(struct subid_range));
+       ranges = MALLOC(struct subid_range);
        if (!ranges)
                return SUBID_STATUS_ERROR;
        if (strcmp(owner, "user1") == 0 || strcmp(owner, "group1") == 0) {