Author: benno
Date: Wed Apr 25 01:48:15 2018
New Revision: 332981
URL: https://svnweb.freebsd.org/changeset/base/332981

Log:
  MFC r316579
  
   makefs: use emalloc and friends
  
   The emalloc set of error-checking memory allocation routines were added
   to libnetbsd in r316572. Use them in makefs to reduce differences with
   NetBSD.
  
   NetBSD revs:
   cd9660.c                     1.39
   ffs.c                                1.56
   makefs.c                     1.42
   walk.c                               1.27
   cd9660/cd9660_archimedes.c   1.2
   cd9660/cd9660_eltorito.c     1.20
   cd9660/cd9660_write.c                1.16
   cd9660/iso9660_rrip.c                1.12
   ffs/buf.c                    1.17
   ffs/mkfs.c                   1.26
  
  Sponsored by: iXsystems, Inc.

Modified:
  stable/11/usr.sbin/makefs/cd9660.c
  stable/11/usr.sbin/makefs/cd9660/cd9660_archimedes.c
  stable/11/usr.sbin/makefs/cd9660/cd9660_eltorito.c
  stable/11/usr.sbin/makefs/cd9660/cd9660_write.c
  stable/11/usr.sbin/makefs/cd9660/iso9660_rrip.c
  stable/11/usr.sbin/makefs/ffs.c
  stable/11/usr.sbin/makefs/ffs/buf.c
  stable/11/usr.sbin/makefs/ffs/mkfs.c
  stable/11/usr.sbin/makefs/makefs.c
  stable/11/usr.sbin/makefs/mtree.c
  stable/11/usr.sbin/makefs/walk.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/usr.sbin/makefs/cd9660.c
==============================================================================
--- stable/11/usr.sbin/makefs/cd9660.c  Wed Apr 25 01:35:06 2018        
(r332980)
+++ stable/11/usr.sbin/makefs/cd9660.c  Wed Apr 25 01:48:15 2018        
(r332981)
@@ -105,6 +105,7 @@ __FBSDID("$FreeBSD$");
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
+#include <util.h>
 
 #include "makefs.h"
 #include "cd9660.h"
@@ -173,10 +174,8 @@ static cd9660node *cd9660_create_special_directory(iso
 static cd9660node *
 cd9660_allocate_cd9660node(void)
 {
-       cd9660node *temp;
+       cd9660node *temp = ecalloc(1, sizeof(*temp));
 
-       if ((temp = calloc(1, sizeof(cd9660node))) == NULL)
-               err(EXIT_FAILURE, "%s: calloc", __func__);
        TAILQ_INIT(&temp->cn_children);
        temp->parent = temp->dot_record = temp->dot_dot_record = NULL;
        temp->ptnext = temp->ptprev = temp->ptlast = NULL;
@@ -253,11 +252,8 @@ cd9660_set_defaults(iso9660_disk *diskStructure)
 void
 cd9660_prep_opts(fsinfo_t *fsopts)
 {
-       iso9660_disk *diskStructure;
+       iso9660_disk *diskStructure = ecalloc(1, sizeof(*diskStructure));
 
-       if ((diskStructure = calloc(1, sizeof(*diskStructure))) == NULL)
-               err(EXIT_FAILURE, "%s: calloc", __func__);
-
 #define OPT_STR(letter, name, desc) \
        { letter, name, NULL, OPT_STRBUF, 0, 0, desc }
 
@@ -440,9 +436,7 @@ cd9660_parse_opts(const char *option, fsinfo_t *fsopts
                                rv = 0;
                        } else {
                                diskStructure->boot_image_directory =
-                                   malloc(strlen(buf) + 1);
-                               if (diskStructure->boot_image_directory == NULL)
-                                       err(1, "malloc");
+                                   emalloc(strlen(buf) + 1);
                                /* BIG TODO: Add the max length function here */
                                rv = cd9660_arguments_set_string(buf, desc, 12,
                                    'd', diskStructure->boot_image_directory);
@@ -522,12 +516,7 @@ cd9660_makefs(const char *image, const char *dir, fsno
        /* Actually, we now need to add the REAL root node, at level 0 */
 
        real_root = cd9660_allocate_cd9660node();
-       if ((real_root->isoDirRecord =
-               malloc( sizeof(iso_directory_record_cd9660) )) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("cd9660_makefs");
-               exit(1);
-       }
-
+       real_root->isoDirRecord = emalloc(sizeof(*real_root->isoDirRecord));
        /* Leave filename blank for root */
        memset(real_root->isoDirRecord->name, 0,
            ISO_FILENAME_MAXLENGTH_WITH_PADDING);
@@ -769,11 +758,7 @@ cd9660_setup_volume_descriptors(iso9660_disk *diskStru
        volume_descriptor *temp, *t;
 
        /* Set up the PVD */
-       if ((temp = malloc(sizeof(volume_descriptor))) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
-               exit(1);
-       }
-
+       temp = emalloc(sizeof(*temp));
        temp->volumeDescriptorData =
           (unsigned char *)&diskStructure->primaryDescriptor;
        temp->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_PVD;
@@ -786,19 +771,10 @@ cd9660_setup_volume_descriptors(iso9660_disk *diskStru
        sector++;
        /* Set up boot support if enabled. BVD must reside in sector 17 */
        if (diskStructure->is_bootable) {
-               if ((t = malloc(sizeof(volume_descriptor))) == NULL) {
-                       CD9660_MEM_ALLOC_ERROR(
-                           "cd9660_setup_volume_descriptors");
-                       exit(1);
-               }
-               if ((t->volumeDescriptorData = malloc(2048)) == NULL) {
-                       CD9660_MEM_ALLOC_ERROR(
-                           "cd9660_setup_volume_descriptors");
-                       exit(1);
-               }
+               t = emalloc(sizeof(*t));
+               t->volumeDescriptorData = ecalloc(1, 2048);
                temp->next = t;
                temp = t;
-               memset(t->volumeDescriptorData, 0, 2048);
                t->sector = 17;
                if (diskStructure->verbose_level > 0)
                        printf("Setting up boot volume descriptor\n");
@@ -807,17 +783,9 @@ cd9660_setup_volume_descriptors(iso9660_disk *diskStru
        }
 
        /* Set up the terminator */
-       if ((t = malloc(sizeof(volume_descriptor))) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
-               exit(1);
-       }
-       if ((t->volumeDescriptorData = malloc(2048)) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
-               exit(1);
-       }
-
+       t = emalloc(sizeof(*t));
+       t->volumeDescriptorData = ecalloc(1, 2048);
        temp->next = t;
-       memset(t->volumeDescriptorData, 0, 2048);
        t->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_TERMINATOR;
        t->next = NULL;
        t->volumeDescriptorData[6] = 1;
@@ -837,12 +805,7 @@ cd9660_setup_volume_descriptors(iso9660_disk *diskStru
 static int
 cd9660_fill_extended_attribute_record(cd9660node *node)
 {
-       if ((node->isoExtAttributes =
-           malloc(sizeof(struct iso_extended_attributes))) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("cd9660_fill_extended_attribute_record");
-               exit(1);
-       };
-
+       node->isoExtAttributes = emalloc(sizeof(*node->isoExtAttributes));
        return 1;
 }
 #endif
@@ -901,12 +864,7 @@ cd9660_translate_node(iso9660_disk *diskStructure, fsn
                               "returning\n");
                return 0;
        }
-       if ((newnode->isoDirRecord =
-               malloc(sizeof(iso_directory_record_cd9660))) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("cd9660_translate_node");
-               return 0;
-       }
-
+       newnode->isoDirRecord = emalloc(sizeof(*newnode->isoDirRecord));
        /* Set the node pointer */
        newnode->node = node;
 
@@ -1113,7 +1071,7 @@ cd9660_rename_filename(iso9660_disk *diskStructure, cd
        else
                maxlength = ISO_FILENAME_MAXLENGTH_BEFORE_VERSION;
 
-       tmp = malloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING);
+       tmp = emalloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING);
 
        while (i < num && iter) {
                powers = 1;
@@ -1552,9 +1510,7 @@ struct ptq_entry
 } *n;
 
 #define PTQUEUE_NEW(n,s,r,t){\
-       n = malloc(sizeof(struct s));   \
-       if (n == NULL)  \
-               return r; \
+       n = emalloc(sizeof(struct s));  \
        n->node = t;\
 }
 
@@ -2006,25 +1962,10 @@ cd9660_create_virtual_entry(iso9660_disk *diskStructur
        if (temp == NULL)
                return NULL;
 
-       if ((tfsnode = malloc(sizeof(fsnode))) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
-               return NULL;
-       }
+       tfsnode = emalloc(sizeof(*tfsnode));
+       tfsnode->name = estrdup(name);
+       temp->isoDirRecord = emalloc(sizeof(*temp->isoDirRecord));
 
-       /* Assume for now name is a valid length */
-       if ((tfsnode->name = malloc(strlen(name) + 1)) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
-               return NULL;
-       }
-
-       if ((temp->isoDirRecord =
-           malloc(sizeof(iso_directory_record_cd9660))) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
-               return NULL;
-       }
-
-       strcpy(tfsnode->name, name);
-
        cd9660_convert_filename(diskStructure, tfsnode->name,
            temp->isoDirRecord->name, file);
 
@@ -2075,8 +2016,7 @@ cd9660_create_file(iso9660_disk *diskStructure, const 
 
        temp->type = CD9660_TYPE_FILE | CD9660_TYPE_VIRTUAL;
 
-       if ((temp->node->inode = calloc(1, sizeof(fsinode))) == NULL)
-               return NULL;
+       temp->node->inode = ecalloc(1, sizeof(*temp->node->inode));
        *temp->node->inode = *me->node->inode;
 
        if (cd9660_translate_node_common(diskStructure, temp) == 0)
@@ -2103,8 +2043,7 @@ cd9660_create_directory(iso9660_disk *diskStructure, c
 
        temp->type = CD9660_TYPE_DIR | CD9660_TYPE_VIRTUAL;
 
-       if ((temp->node->inode = calloc(1, sizeof(fsinode))) == NULL)
-               return NULL;
+       temp->node->inode = ecalloc(1, sizeof(*temp->node->inode));
        *temp->node->inode = *me->node->inode;
 
        if (cd9660_translate_node_common(diskStructure, temp) == 0)
@@ -2172,10 +2111,7 @@ cd9660_add_generic_bootimage(iso9660_disk *diskStructu
                return 0;
        }
 
-       if ((diskStructure->generic_bootimage = strdup(bootimage)) == NULL) {
-               warn("%s: strdup", __func__);
-               return 0;
-       }
+       diskStructure->generic_bootimage = estrdup(bootimage);
 
        /* Get information about the file */
        if (lstat(diskStructure->generic_bootimage, &stbuf) == -1)

Modified: stable/11/usr.sbin/makefs/cd9660/cd9660_archimedes.c
==============================================================================
--- stable/11/usr.sbin/makefs/cd9660/cd9660_archimedes.c        Wed Apr 25 
01:35:06 2018        (r332980)
+++ stable/11/usr.sbin/makefs/cd9660/cd9660_archimedes.c        Wed Apr 25 
01:48:15 2018        (r332981)
@@ -42,12 +42,11 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include <sys/types.h>
 #include <assert.h>
 #include <stdint.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
+#include <util.h>
 
 #include "makefs.h"
 #include "cd9660.h"
@@ -95,11 +94,8 @@ archimedes_convert_node(cd9660node *node)
                return;
        if (type == -1) type = 0;
 
-       assert(sizeof(struct ISO_ARCHIMEDES) == 32);
-       if ((arc = calloc(1, sizeof(struct ISO_ARCHIMEDES))) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("archimedes_convert_node");
-               exit(1);
-       }
+       assert(sizeof(*arc) == 32);
+       arc = ecalloc(1, sizeof(*arc));
 
        stamp = riscos_date(node->node->inode->st.st_mtime);
 

Modified: stable/11/usr.sbin/makefs/cd9660/cd9660_eltorito.c
==============================================================================
--- stable/11/usr.sbin/makefs/cd9660/cd9660_eltorito.c  Wed Apr 25 01:35:06 
2018        (r332980)
+++ stable/11/usr.sbin/makefs/cd9660/cd9660_eltorito.c  Wed Apr 25 01:48:15 
2018        (r332981)
@@ -36,6 +36,7 @@
 
 #include "cd9660.h"
 #include "cd9660_eltorito.h"
+#include <util.h>
 
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
@@ -75,10 +76,7 @@ cd9660_add_boot_disk(iso9660_disk *diskStructure, cons
        }
 
        /* First decode the boot information */
-       if ((temp = strdup(boot_info)) == NULL) {
-               warn("%s: strdup", __func__);
-               return 0;
-       }
+       temp = estrdup(boot_info);
 
        sysname = temp;
        filename = strchr(sysname, ';');
@@ -95,12 +93,7 @@ cd9660_add_boot_disk(iso9660_disk *diskStructure, cons
                printf("Found bootdisk with system %s, and filename %s\n",
                    sysname, filename);
        }
-       if ((new_image = malloc(sizeof(*new_image))) == NULL) {
-               warn("%s: malloc", __func__);
-               free(temp);
-               return 0;
-       }
-       (void)memset(new_image, 0, sizeof(*new_image));
+       new_image = ecalloc(1, sizeof(*new_image));
        new_image->loadSegment = 0;     /* default for now */
 
        /* Decode System */
@@ -120,12 +113,7 @@ cd9660_add_boot_disk(iso9660_disk *diskStructure, cons
        }
 
 
-       if ((new_image->filename = strdup(filename)) == NULL) {
-               warn("%s: strdup", __func__);
-               free(temp);
-               free(new_image);
-               return 0;
-       }
+       new_image->filename = estrdup(filename);
 
        free(temp);
 
@@ -228,12 +216,7 @@ cd9660_eltorito_add_boot_option(iso9660_disk *diskStru
 static struct boot_catalog_entry *
 cd9660_init_boot_catalog_entry(void)
 {
-       struct boot_catalog_entry *temp;
-
-       if ((temp = malloc(sizeof(*temp))) == NULL)
-               return NULL;
-
-       return memset(temp, 0, sizeof(*temp));
+       return ecalloc(1, sizeof(struct boot_catalog_entry));
 }
 
 static struct boot_catalog_entry *
@@ -246,11 +229,6 @@ cd9660_boot_setup_validation_entry(char sys)
        int i;
        entry = cd9660_init_boot_catalog_entry();
 
-       if (entry == NULL) {
-               warnx("Error: memory allocation failed in "
-                     "cd9660_boot_setup_validation_entry");
-               return 0;
-       }
        ve = &entry->entry_data.VE;
 
        ve->header_id[0] = 1;

Modified: stable/11/usr.sbin/makefs/cd9660/cd9660_write.c
==============================================================================
--- stable/11/usr.sbin/makefs/cd9660/cd9660_write.c     Wed Apr 25 01:35:06 
2018        (r332980)
+++ stable/11/usr.sbin/makefs/cd9660/cd9660_write.c     Wed Apr 25 01:48:15 
2018        (r332981)
@@ -40,6 +40,8 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#include <util.h>
+
 static int cd9660_write_volume_descriptors(iso9660_disk *, FILE *);
 static int cd9660_write_path_table(iso9660_disk *, FILE *, off_t, int);
 static int cd9660_write_path_tables(iso9660_disk *, FILE *);
@@ -174,14 +176,8 @@ cd9660_write_path_table(iso9660_disk *diskStructure, F
        path_table_entry temp_entry;
        cd9660node *ptcur;
 
-       buffer = malloc(diskStructure->sectorSize * path_table_sectors);
-       if (buffer == NULL) {
-               warnx("%s: Memory allocation error allocating buffer",
-                   __func__);
-               return 0;
-       }
+       buffer = ecalloc(path_table_sectors, diskStructure->sectorSize);
        buffer_head = buffer;
-       memset(buffer, 0, diskStructure->sectorSize * path_table_sectors);
 
        ptcur = diskStructure->rootNode;
 
@@ -280,16 +276,8 @@ cd9660_write_file(iso9660_disk *diskStructure, FILE *f
 
        /* Todo : clean up variables */
 
-       temp_file_name = malloc(CD9660MAXPATH + 1);
-       if (temp_file_name == NULL)
-               err(EXIT_FAILURE, "%s: malloc", __func__);
-
-       memset(temp_file_name, 0, CD9660MAXPATH + 1);
-
-       buf = malloc(diskStructure->sectorSize);
-       if (buf == NULL)
-               err(EXIT_FAILURE, "%s: malloc", __func__);
-
+       temp_file_name = ecalloc(CD9660MAXPATH + 1, 1);
+       buf = emalloc(diskStructure->sectorSize);
        if ((writenode->level != 0) &&
            !(writenode->node->type & S_IFDIR)) {
                fsinode *inode = writenode->node->inode;
@@ -448,10 +436,7 @@ cd9660_copy_file(iso9660_disk *diskStructure, FILE *fd
        int buf_size = diskStructure->sectorSize;
        char *buf;
 
-       buf = malloc(buf_size);
-       if (buf == NULL)
-               err(EXIT_FAILURE, "%s: malloc", __func__);
-
+       buf = emalloc(buf_size);
        if ((rf = fopen(filename, "rb")) == NULL) {
                warn("%s: cannot open %s", __func__, filename);
                free(buf);

Modified: stable/11/usr.sbin/makefs/cd9660/iso9660_rrip.c
==============================================================================
--- stable/11/usr.sbin/makefs/cd9660/iso9660_rrip.c     Wed Apr 25 01:35:06 
2018        (r332980)
+++ stable/11/usr.sbin/makefs/cd9660/iso9660_rrip.c     Wed Apr 25 01:48:15 
2018        (r332981)
@@ -47,6 +47,7 @@ __FBSDID("$FreeBSD$");
 #include "makefs.h"
 #include "cd9660.h"
 #include "iso9660_rrip.h"
+#include <util.h>
 
 static void cd9660_rrip_initialize_inode(cd9660node *);
 static int cd9660_susp_handle_continuation(iso9660_disk *, cd9660node *);
@@ -460,11 +461,7 @@ cd9660node_susp_create_node(int susp_type, int entry_t
 {
        struct ISO_SUSP_ATTRIBUTES* temp;
 
-       if ((temp = malloc(sizeof(struct ISO_SUSP_ATTRIBUTES))) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("cd9660node_susp_create_node");
-               exit(1);
-       }
-
+       temp = emalloc(sizeof(*temp));
        temp->susp_type = susp_type;
        temp->entry_type = entry_type;
        temp->last_in_suf = 0;

Modified: stable/11/usr.sbin/makefs/ffs.c
==============================================================================
--- stable/11/usr.sbin/makefs/ffs.c     Wed Apr 25 01:35:06 2018        
(r332980)
+++ stable/11/usr.sbin/makefs/ffs.c     Wed Apr 25 01:48:15 2018        
(r332981)
@@ -82,6 +82,7 @@ __FBSDID("$FreeBSD$");
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <util.h>
 
 #include "makefs.h"
 #include "ffs.h"
@@ -149,11 +150,8 @@ int        sectorsize;             /* XXX: for 
buf.c::getblk() */
 void
 ffs_prep_opts(fsinfo_t *fsopts)
 {
-       ffs_opt_t *ffs_opts;
+       ffs_opt_t *ffs_opts = ecalloc(1, sizeof(*ffs_opts));
 
-       if ((ffs_opts = calloc(1, sizeof(ffs_opt_t))) == NULL)
-               err(1, "Allocating memory for ffs_options");
-
        const option_t ffs_options[] = {
            { 'b', "bsize", &ffs_opts->bsize, OPT_INT32,
              1, INT_MAX, "block size" },
@@ -518,10 +516,7 @@ ffs_create_image(const char *image, fsinfo_t *fsopts)
                        printf("zero-ing image `%s', %lld sectors, "
                            "using %d byte chunks\n", image, (long long)bufrem,
                            bufsize);
-               if ((buf = calloc(1, bufsize)) == NULL) {
-                       warn("Can't create buffer for sector");
-                       return (-1);
-               }
+               buf = ecalloc(1, bufsize);
        }
        while (bufrem > 0) {
                i = write(fsopts->fd, buf, MIN(bufsize, bufrem));
@@ -923,8 +918,7 @@ ffs_write_file(union dinode *din, uint32_t ino, void *
                goto write_inode_and_leave;             /* mmm, cheating */
 
        if (isfile) {
-               if ((fbuf = malloc(ffs_opts->bsize)) == NULL)
-                       err(1, "Allocating memory for write buffer");
+               fbuf = emalloc(ffs_opts->bsize);
                if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) {
                        warn("Can't open `%s' for reading", (char *)buf);
                        goto leave_ffs_write_file;
@@ -1051,8 +1045,7 @@ ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsno
                if (debug & DEBUG_FS_MAKE_DIRBUF)
                        printf("ffs_make_dirbuf: growing buf to %d\n",
                            dbuf->size + DIRBLKSIZ);
-               if ((newbuf = realloc(dbuf->buf, dbuf->size + DIRBLKSIZ)) == 
NULL)
-                       err(1, "Allocating memory for directory buffer");
+               newbuf = erealloc(dbuf->buf, dbuf->size + DIRBLKSIZ);
                dbuf->buf = newbuf;
                dbuf->size += DIRBLKSIZ;
                memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ);
@@ -1103,10 +1096,7 @@ ffs_write_inode(union dinode *dp, uint32_t ino, const 
 
        assert (isclr(cg_inosused_swap(cgp, fsopts->needswap), cgino));
 
-       buf = malloc(fs->fs_bsize);
-       if (buf == NULL)
-               errx(1, "ffs_write_inode: cg %d: can't alloc inode block", cg);
-
+       buf = emalloc(fs->fs_bsize);
        dp1 = (struct ufs1_dinode *)buf;
        dp2 = (struct ufs2_dinode *)buf;
 

Modified: stable/11/usr.sbin/makefs/ffs/buf.c
==============================================================================
--- stable/11/usr.sbin/makefs/ffs/buf.c Wed Apr 25 01:35:06 2018        
(r332980)
+++ stable/11/usr.sbin/makefs/ffs/buf.c Wed Apr 25 01:48:15 2018        
(r332981)
@@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$");
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <util.h>
 
 #include "makefs.h"
 
@@ -207,9 +208,7 @@ getblk(struct vnode *vp, daddr_t blkno, int size, int 
                }
        }
        if (bp == NULL) {
-               if ((bp = calloc(1, sizeof(struct buf))) == NULL)
-                       err(1, "getblk: calloc");
-
+               bp = ecalloc(1, sizeof(*bp));
                bp->b_bufsize = 0;
                bp->b_blkno = bp->b_lblkno = blkno;
                bp->b_fd = fd;
@@ -219,9 +218,7 @@ getblk(struct vnode *vp, daddr_t blkno, int size, int 
        }
        bp->b_bcount = size;
        if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) {
-               n = realloc(bp->b_data, size);
-               if (n == NULL)
-                       err(1, "getblk: realloc b_data %ld", bp->b_bcount);
+               n = erealloc(bp->b_data, size);
                bp->b_data = n;
                bp->b_bufsize = size;
        }

Modified: stable/11/usr.sbin/makefs/ffs/mkfs.c
==============================================================================
--- stable/11/usr.sbin/makefs/ffs/mkfs.c        Wed Apr 25 01:35:06 2018        
(r332980)
+++ stable/11/usr.sbin/makefs/ffs/mkfs.c        Wed Apr 25 01:48:15 2018        
(r332981)
@@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$");
 #include <string.h>
 #include <unistd.h>
 #include <errno.h>
+#include <util.h>
 
 #include "makefs.h"
 #include "ffs.h"
@@ -403,8 +404,7 @@ ffs_mkfs(const char *fsys, const fsinfo_t *fsopts, tim
        blks = howmany(size, sblock.fs_fsize);
        if (sblock.fs_contigsumsize > 0)
                size += sblock.fs_ncg * sizeof(int32_t);
-       if ((space = (char *)calloc(1, size)) == NULL)
-               err(1, "memory allocation error for cg summaries");
+       space = ecalloc(1, size);
        sblock.fs_csp = space;
        space = (char *)space + sblock.fs_cssize;
        if (sblock.fs_contigsumsize > 0) {
@@ -492,11 +492,7 @@ ffs_mkfs(const char *fsys, const fsinfo_t *fsopts, tim
                iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
        else
                iobufsize = 4 * sblock.fs_bsize;
-       if ((iobuf = malloc(iobufsize)) == NULL) {
-               printf("Cannot allocate I/O buffer\n");
-               exit(38);
-       }
-       memset(iobuf, 0, iobufsize);
+       iobuf = ecalloc(1, iobufsize);
        /*
         * Make a copy of the superblock into the buffer that we will be
         * writing out in each cylinder group.
@@ -562,8 +558,7 @@ ffs_write_superblock(struct fs *fs, const fsinfo_t *fs
        size = fs->fs_cssize;
        blks = howmany(size, fs->fs_fsize);
        space = (void *)fs->fs_csp;
-       if ((wrbuf = malloc(size)) == NULL)
-               err(1, "ffs_write_superblock: malloc %d", size);
+       wrbuf = emalloc(size);
        for (i = 0; i < blks; i+= fs->fs_frag) {
                size = fs->fs_bsize;
                if (i + fs->fs_frag > blks)

Modified: stable/11/usr.sbin/makefs/makefs.c
==============================================================================
--- stable/11/usr.sbin/makefs/makefs.c  Wed Apr 25 01:35:06 2018        
(r332980)
+++ stable/11/usr.sbin/makefs/makefs.c  Wed Apr 25 01:48:15 2018        
(r332981)
@@ -52,6 +52,7 @@ __FBSDID("$FreeBSD$");
 #include <time.h>
 #include <unistd.h>
 #include <stdbool.h>
+#include <util.h>
 
 #include "makefs.h"
 #include "mtree.h"
@@ -352,10 +353,7 @@ set_option(const option_t *options, const char *option
 
        assert(option != NULL);
 
-       if ((var = strdup(option)) == NULL) {
-               err(EXIT_FAILURE, "Allocating memory for copy of option 
string");
-       }
-
+       var = estrdup(option);
        for (val = var; *val; val++)
                if (*val == '=') {
                        *val++ = '\0';
@@ -396,8 +394,7 @@ set_option_var(const option_t *options, const char *va
                            options[i].maximum);
                        break;
                case OPT_STRPTR:
-                       if ((s = strdup(val)) == NULL)
-                               err(1, NULL);
+                       s = estrdup(val);
                        *(char **)options[i].value = s;
                        break;
                case OPT_STRBUF:
@@ -440,14 +437,11 @@ option_t *
 copy_opts(const option_t *o)
 {
        size_t i;
-       void *rv;
 
        for (i = 0; o[i].name; i++)
                continue;
        i++;
-       if ((rv = calloc(i, sizeof(*o))) == NULL)
-               err(1, "calloc");
-       return memcpy(rv, o, i * sizeof(*o));
+       return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o));
 }
 
 static int

Modified: stable/11/usr.sbin/makefs/mtree.c
==============================================================================
--- stable/11/usr.sbin/makefs/mtree.c   Wed Apr 25 01:35:06 2018        
(r332980)
+++ stable/11/usr.sbin/makefs/mtree.c   Wed Apr 25 01:48:15 2018        
(r332981)
@@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$");
 #include <strings.h>
 #include <time.h>
 #include <unistd.h>
+#include <util.h>
 #include <vis.h>
 
 #include "makefs.h"
@@ -83,14 +84,11 @@ mtree_file_push(const char *name, FILE *fp)
 {
        struct mtree_fileinfo *fi;
 
-       fi = malloc(sizeof(*fi));
-       if (fi == NULL)
-               return (ENOMEM);
-
+       fi = emalloc(sizeof(*fi));
        if (strcmp(name, "-") == 0)
-               fi->name = strdup("(stdin)");
+               fi->name = estrdup("(stdin)");
        else
-               fi->name = strdup(name);
+               fi->name = estrdup(name);
        if (fi->name == NULL) {
                free(fi);
                return (ENOMEM);
@@ -176,7 +174,7 @@ mtree_file_path(fsnode *node)
        }
        sbuf_cat(sb, rp[depth]);
        sbuf_finish(sb);
-       res = strdup(sbuf_data(sb));
+       res = estrdup(sbuf_data(sb));
        sbuf_delete(sb);
        if (res == NULL)
                errno = ENOMEM;
@@ -206,8 +204,8 @@ mtree_resolve(const char *spec, int *istemp)
        quoted = (subst || c == '\'') ? 1 : 0;
 
        if (!subst) {
-               res = strdup(spec + quoted);
-               if (res != NULL && quoted)
+               res = estrdup(spec + quoted);
+               if (quoted)
                        res[len - 2] = '\0';
                return (res);
        }
@@ -263,25 +261,18 @@ mtree_resolve(const char *spec, int *istemp)
                }
 
                error = ENOMEM;
-               var = calloc(p - v, 1);
-               if (var == NULL)
-                       break;
-
+               var = ecalloc(p - v, 1);
                memcpy(var, v + 1, p - v - 1);
                if (strcmp(var, ".CURDIR") == 0) {
                        res = getcwd(NULL, 0);
                        if (res == NULL)
                                break;
                } else if (strcmp(var, ".PROG") == 0) {
-                       res = strdup(getprogname());
-                       if (res == NULL)
-                               break;
+                       res = estrdup(getprogname());
                } else {
                        v = getenv(var);
                        if (v != NULL) {
-                               res = strdup(v);
-                               if (res == NULL)
-                                       break;
+                               res = estrdup(v);
                        } else
                                res = NULL;
                }
@@ -433,25 +424,12 @@ create_node(const char *name, u_int type, fsnode *pare
 {
        fsnode *n;
 
-       n = calloc(1, sizeof(*n));
-       if (n == NULL)
-               return (NULL);
-
-       n->name = strdup(name);
-       if (n->name == NULL) {
-               free(n);
-               return (NULL);
-       }
-
+       n = ecalloc(1, sizeof(*n));
+       n->name = estrdup(name);
        n->type = (type == 0) ? global->type : type;
        n->parent = parent;
 
-       n->inode = calloc(1, sizeof(*n->inode));
-       if (n->inode == NULL) {
-               free(n->name);
-               free(n);
-               return (NULL);
-       }
+       n->inode = ecalloc(1, sizeof(*n->inode));
 
        /* Assign global options/defaults. */
        bcopy(global->inode, n->inode, sizeof(*n->inode));
@@ -542,7 +520,7 @@ read_mtree_keywords(FILE *fp, fsnode *node)
                                        error = ENOATTR;
                                        break;
                                }
-                               node->contents = strdup(value);
+                               node->contents = estrdup(value);
                        } else
                                error = ENOSYS;
                        break;
@@ -589,7 +567,7 @@ read_mtree_keywords(FILE *fp, fsnode *node)
                                        error = ENOATTR;
                                        break;
                                }
-                               node->symlink = malloc(strlen(value) + 1);
+                               node->symlink = emalloc(strlen(value) + 1);
                                if (node->symlink == NULL) {
                                        error = errno;
                                        break;

Modified: stable/11/usr.sbin/makefs/walk.c
==============================================================================
--- stable/11/usr.sbin/makefs/walk.c    Wed Apr 25 01:35:06 2018        
(r332980)
+++ stable/11/usr.sbin/makefs/walk.c    Wed Apr 25 01:48:15 2018        
(r332981)
@@ -204,8 +204,7 @@ walk_dir(const char *root, const char *dir, fsnode *pa
                        if (llen == -1)
                                err(1, "Readlink `%s'", path);
                        slink[llen] = '\0';
-                       if ((cur->symlink = strdup(slink)) == NULL)
-                               err(1, "Memory allocation error");
+                       cur->symlink = estrdup(slink);
                }
        }
        assert(first != NULL);
@@ -223,11 +222,10 @@ create_fsnode(const char *root, const char *path, cons
 {
        fsnode *cur;
 
-       if ((cur = calloc(1, sizeof(fsnode))) == NULL ||
-           (cur->path = strdup(path)) == NULL ||
-           (cur->name = strdup(name)) == NULL ||
-           (cur->inode = calloc(1, sizeof(fsinode))) == NULL)
-               err(1, "Memory allocation error");
+       cur = ecalloc(1, sizeof(*cur));
+       cur->path = estrdup(path);
+       cur->name = estrdup(name);
+       cur->inode = ecalloc(1, sizeof(*cur->inode));
        cur->root = root;
        cur->type = stbuf->st_mode & S_IFMT;
        cur->inode->nlink = 1;
@@ -458,9 +456,7 @@ apply_specdir(const char *dir, NODE *specnode, fsnode 
                        if (curfsnode->type == S_IFLNK) {
                                assert(curnode->slink != NULL);
                                        /* for symlinks, copy the target */
-                               if ((curfsnode->symlink =
-                                   strdup(curnode->slink)) == NULL)
-                                       err(1, "Memory allocation error");
+                               curfsnode->symlink = estrdup(curnode->slink);
                        }
                }
                apply_specentry(dir, curnode, curfsnode);
@@ -516,8 +512,7 @@ apply_specentry(const char *dir, NODE *specnode, fsnod
                assert(specnode->slink != NULL);
                ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
                free(dirnode->symlink);
-               if ((dirnode->symlink = strdup(specnode->slink)) == NULL)
-                       err(1, "Memory allocation error");
+               dirnode->symlink = estrdup(specnode->slink);
        }
        if (specnode->flags & F_TIME) {
                ASEPRINT("time", "%ld",
@@ -666,10 +661,7 @@ link_check(fsinode *entry)
                htused = 0;
 
                ohtable = htable;
-               htable = calloc(htmask+1, sizeof(*htable));
-               if (!htable)
-                       err(1, "Memory allocation error");
-
+               htable = ecalloc(htmask+1, sizeof(*htable));
                /* populate newly allocated hashtable */
                if (ohtable) {
                        int i;
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to