Author: emaste
Date: Thu Apr  6 16:18:42 2017
New Revision: 316579
URL: https://svnweb.freebsd.org/changeset/base/316579

Log:
  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
  
  Obtained from:        NetBSD

Modified:
  head/usr.sbin/makefs/cd9660.c
  head/usr.sbin/makefs/cd9660/cd9660_archimedes.c
  head/usr.sbin/makefs/cd9660/cd9660_eltorito.c
  head/usr.sbin/makefs/cd9660/cd9660_write.c
  head/usr.sbin/makefs/cd9660/iso9660_rrip.c
  head/usr.sbin/makefs/ffs.c
  head/usr.sbin/makefs/ffs/buf.c
  head/usr.sbin/makefs/ffs/mkfs.c
  head/usr.sbin/makefs/makefs.c
  head/usr.sbin/makefs/mtree.c
  head/usr.sbin/makefs/walk.c

Modified: head/usr.sbin/makefs/cd9660.c
==============================================================================
--- head/usr.sbin/makefs/cd9660.c       Thu Apr  6 15:57:53 2017        
(r316578)
+++ head/usr.sbin/makefs/cd9660.c       Thu Apr  6 16:18:42 2017        
(r316579)
@@ -103,6 +103,7 @@ __FBSDID("$FreeBSD$");
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
+#include <util.h>
 
 #include "makefs.h"
 #include "cd9660.h"
@@ -172,10 +173,8 @@ static int  cd9660_add_generic_bootimage
 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;
@@ -252,10 +251,7 @@ cd9660_set_defaults(iso9660_disk *diskSt
 void
 cd9660_prep_opts(fsinfo_t *fsopts)
 {
-       iso9660_disk *diskStructure;
-
-       if ((diskStructure = calloc(1, sizeof(*diskStructure))) == NULL)
-               err(EXIT_FAILURE, "%s: calloc", __func__);
+       iso9660_disk *diskStructure = ecalloc(1, sizeof(*diskStructure));
 
 #define OPT_STR(letter, name, desc) \
        { letter, name, NULL, OPT_STRBUF, 0, 0, desc }
@@ -439,9 +435,7 @@ cd9660_parse_opts(const char *option, fs
                                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);
@@ -521,12 +515,7 @@ cd9660_makefs(const char *image, const c
        /* 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);
@@ -770,11 +759,7 @@ cd9660_setup_volume_descriptors(iso9660_
        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;
@@ -787,19 +772,10 @@ cd9660_setup_volume_descriptors(iso9660_
        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");
@@ -808,17 +784,9 @@ cd9660_setup_volume_descriptors(iso9660_
        }
 
        /* 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;
@@ -838,12 +806,7 @@ cd9660_setup_volume_descriptors(iso9660_
 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 *disk
                        printf("%s: NULL node passed, returning\n", __func__);
                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 *dis
        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,24 +1962,9 @@ cd9660_create_virtual_entry(iso9660_disk
        if (temp == NULL)
                return NULL;
 
-       if ((tfsnode = malloc(sizeof(fsnode))) == NULL) {
-               CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
-               return NULL;
-       }
-
-       /* 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);
+       tfsnode = emalloc(sizeof(*tfsnode));
+       tfsnode->name = estrdup(name);
+       temp->isoDirRecord = emalloc(sizeof(*temp->isoDirRecord));
 
        cd9660_convert_filename(diskStructure, tfsnode->name,
            temp->isoDirRecord->name, file);
@@ -2075,8 +2016,7 @@ cd9660_create_file(iso9660_disk *diskStr
 
        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 *di
 
        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_dis
                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: head/usr.sbin/makefs/cd9660/cd9660_archimedes.c
==============================================================================
--- head/usr.sbin/makefs/cd9660/cd9660_archimedes.c     Thu Apr  6 15:57:53 
2017        (r316578)
+++ head/usr.sbin/makefs/cd9660/cd9660_archimedes.c     Thu Apr  6 16:18:42 
2017        (r316579)
@@ -40,12 +40,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"
@@ -93,11 +92,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: head/usr.sbin/makefs/cd9660/cd9660_eltorito.c
==============================================================================
--- head/usr.sbin/makefs/cd9660/cd9660_eltorito.c       Thu Apr  6 15:57:53 
2017        (r316578)
+++ head/usr.sbin/makefs/cd9660/cd9660_eltorito.c       Thu Apr  6 16:18:42 
2017        (r316579)
@@ -34,6 +34,7 @@
 
 #include "cd9660.h"
 #include "cd9660_eltorito.h"
+#include <util.h>
 
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
@@ -73,10 +74,7 @@ cd9660_add_boot_disk(iso9660_disk *diskS
        }
 
        /* 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, ';');
@@ -93,12 +91,7 @@ cd9660_add_boot_disk(iso9660_disk *diskS
                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 */
@@ -118,12 +111,7 @@ cd9660_add_boot_disk(iso9660_disk *diskS
        }
 
 
-       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);
 
@@ -226,12 +214,7 @@ cd9660_eltorito_add_boot_option(iso9660_
 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 *
@@ -244,11 +227,6 @@ cd9660_boot_setup_validation_entry(char 
        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: head/usr.sbin/makefs/cd9660/cd9660_write.c
==============================================================================
--- head/usr.sbin/makefs/cd9660/cd9660_write.c  Thu Apr  6 15:57:53 2017        
(r316578)
+++ head/usr.sbin/makefs/cd9660/cd9660_write.c  Thu Apr  6 16:18:42 2017        
(r316579)
@@ -38,6 +38,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 *);
@@ -172,14 +174,8 @@ cd9660_write_path_table(iso9660_disk *di
        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;
 
@@ -278,16 +274,8 @@ cd9660_write_file(iso9660_disk *diskStru
 
        /* 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;
@@ -446,10 +434,7 @@ cd9660_copy_file(iso9660_disk *diskStruc
        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: head/usr.sbin/makefs/cd9660/iso9660_rrip.c
==============================================================================
--- head/usr.sbin/makefs/cd9660/iso9660_rrip.c  Thu Apr  6 15:57:53 2017        
(r316578)
+++ head/usr.sbin/makefs/cd9660/iso9660_rrip.c  Thu Apr  6 16:18:42 2017        
(r316579)
@@ -45,6 +45,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 *);
@@ -458,11 +459,7 @@ cd9660node_susp_create_node(int susp_typ
 {
        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: head/usr.sbin/makefs/ffs.c
==============================================================================
--- head/usr.sbin/makefs/ffs.c  Thu Apr  6 15:57:53 2017        (r316578)
+++ head/usr.sbin/makefs/ffs.c  Thu Apr  6 16:18:42 2017        (r316579)
@@ -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,10 +150,7 @@ int        sectorsize;             /* XXX: for buf.c::getb
 void
 ffs_prep_opts(fsinfo_t *fsopts)
 {
-       ffs_opt_t *ffs_opts;
-
-       if ((ffs_opts = calloc(1, sizeof(ffs_opt_t))) == NULL)
-               err(1, "Allocating memory for ffs_options");
+       ffs_opt_t *ffs_opts = ecalloc(1, sizeof(*ffs_opts));
 
        const option_t ffs_options[] = {
            { 'b', "bsize", &ffs_opts->bsize, OPT_INT32,
@@ -518,10 +516,7 @@ ffs_create_image(const char *image, fsin
                        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));
@@ -919,8 +914,7 @@ ffs_write_file(union dinode *din, uint32
                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;
@@ -1047,8 +1041,7 @@ ffs_make_dirbuf(dirbuf_t *dbuf, const ch
                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);
@@ -1099,10 +1092,7 @@ ffs_write_inode(union dinode *dp, uint32
 
        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: head/usr.sbin/makefs/ffs/buf.c
==============================================================================
--- head/usr.sbin/makefs/ffs/buf.c      Thu Apr  6 15:57:53 2017        
(r316578)
+++ head/usr.sbin/makefs/ffs/buf.c      Thu Apr  6 16:18:42 2017        
(r316579)
@@ -47,6 +47,7 @@ __FBSDID("$FreeBSD$");
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <util.h>
 
 #include "makefs.h"
 
@@ -205,9 +206,7 @@ getblk(struct vnode *vp, daddr_t blkno, 
                }
        }
        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;
@@ -217,9 +216,7 @@ getblk(struct vnode *vp, daddr_t blkno, 
        }
        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);
                memset(n, 0, size);
                bp->b_data = n;
                bp->b_bufsize = size;

Modified: head/usr.sbin/makefs/ffs/mkfs.c
==============================================================================
--- head/usr.sbin/makefs/ffs/mkfs.c     Thu Apr  6 15:57:53 2017        
(r316578)
+++ head/usr.sbin/makefs/ffs/mkfs.c     Thu Apr  6 16:18:42 2017        
(r316579)
@@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$");
 #include <string.h>
 #include <unistd.h>
 #include <errno.h>
+#include <util.h>
 
 #include "makefs.h"
 #include "ffs.h"
@@ -402,8 +403,7 @@ ffs_mkfs(const char *fsys, const fsinfo_
        size = sblock.fs_cssize;
        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_
                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, cons
        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: head/usr.sbin/makefs/makefs.c
==============================================================================
--- head/usr.sbin/makefs/makefs.c       Thu Apr  6 15:57:53 2017        
(r316578)
+++ head/usr.sbin/makefs/makefs.c       Thu Apr  6 16:18:42 2017        
(r316579)
@@ -50,6 +50,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, cons
 
        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, 
                            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: head/usr.sbin/makefs/mtree.c
==============================================================================
--- head/usr.sbin/makefs/mtree.c        Thu Apr  6 15:57:53 2017        
(r316578)
+++ head/usr.sbin/makefs/mtree.c        Thu Apr  6 16:18:42 2017        
(r316579)
@@ -46,6 +46,7 @@ __FBSDID("$FreeBSD$");
 #include <strings.h>
 #include <time.h>
 #include <unistd.h>
+#include <util.h>
 
 #include "makefs.h"
 
@@ -80,14 +81,11 @@ mtree_file_push(const char *name, FILE *
 {
        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);
@@ -173,7 +171,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;
@@ -203,8 +201,8 @@ mtree_resolve(const char *spec, int *ist
        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);
        }
@@ -260,25 +258,18 @@ mtree_resolve(const char *spec, int *ist
                }
 
                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;
                }
@@ -456,25 +447,12 @@ create_node(const char *name, u_int type
 {
        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));
@@ -565,7 +543,7 @@ read_mtree_keywords(FILE *fp, fsnode *no
                                        error = ENOATTR;
                                        break;
                                }
-                               node->contents = strdup(value);
+                               node->contents = estrdup(value);
                        } else
                                error = ENOSYS;
                        break;
@@ -612,7 +590,7 @@ read_mtree_keywords(FILE *fp, fsnode *no
                                        error = ENOATTR;
                                        break;
                                }
-                               node->symlink = strdup(value);
+                               node->symlink = estrdup(value);
                        } else
                                error = ENOSYS;
                        break;

Modified: head/usr.sbin/makefs/walk.c
==============================================================================
--- head/usr.sbin/makefs/walk.c Thu Apr  6 15:57:53 2017        (r316578)
+++ head/usr.sbin/makefs/walk.c Thu Apr  6 16:18:42 2017        (r316579)
@@ -202,8 +202,7 @@ walk_dir(const char *root, const char *d
                        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);
@@ -221,11 +220,10 @@ create_fsnode(const char *root, const ch
 {
        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;
@@ -457,9 +455,7 @@ apply_specdir(const char *dir, NODE *spe
                        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);
@@ -515,8 +511,7 @@ apply_specentry(const char *dir, NODE *s
                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",
@@ -665,10 +660,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;
_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "[email protected]"

Reply via email to