* all mallocs now use xmalloc (and so are OOM error safe), and
authorErik Andersen <andersen@codepoet.org>
Tue, 21 Mar 2000 22:32:57 +0000 (22:32 -0000)
committerErik Andersen <andersen@codepoet.org>
Tue, 21 Mar 2000 22:32:57 +0000 (22:32 -0000)
the common error handling saves a few bytes.  Thanks to
Bob Tinsley <bob@earthrise.demon.co.uk> for the patch.
 -Erik

19 files changed:
Changelog
coreutils/date.c
coreutils/dd.c
coreutils/tr.c
date.c
dd.c
dmesg.c
fsck_minix.c
mkfs_minix.c
mkswap.c
sfdisk.c
tr.c
umount.c
util-linux/dmesg.c
util-linux/fsck_minix.c
util-linux/mkfs_minix.c
util-linux/mkswap.c
util-linux/umount.c
utility.c

index d81207c9d231df0f93c817627ab27e4769aeecc1..c0ffd23cd43e457ae2ad2f5e91183711184af09e 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -52,6 +52,9 @@
            to match the terminal (defaults to width=79 when this is off).
        * Fixed mount'ing loop devices when the filesystem type was not 
            specified.  It used to revert to non-loop after the first try.
+       * all mallocs now use xmalloc (and so are OOM error safe), and
+           the common error handling saves a few bytes.  Thanks to 
+           Bob Tinsley <bob@earthrise.demon.co.uk> for the patch.
 
 
        -Erik Andersen
index b4c3e71535518997677834ec17326b9522c58d29..652db8d74c2cd93d211a665767b592fe08e06ccb 100644 (file)
@@ -276,7 +276,7 @@ int date_main(int argc, char **argv)
        }
 
        /* Print OUTPUT (after ALL that!) */
-       t_buff = malloc(201);
+       t_buff = xmalloc(201);
        strftime(t_buff, 200, date_fmt, &tm_time);
        printf("%s\n", t_buff);
 
index 0d5b3e8ab9867bc7918208da17b763275e4b72de..f40dec712d0d7c6801150f9589833684c2fd2a09 100644 (file)
@@ -114,11 +114,7 @@ extern int dd_main(int argc, char **argv)
                argv++;
        }
 
-       buf = malloc(blockSize);
-       if (buf == NULL) {
-               fprintf(stderr, "Cannot allocate buffer\n");
-               exit(FALSE);
-       }
+       buf = xmalloc(blockSize);
 
        intotal = 0;
        outTotal = 0;
index 8ac09e64175f6f8d49b81132aa69ce7690bb34ea..3bfa4808056d4dc0c694ee30bc886f4ce5814d66 100644 (file)
@@ -44,7 +44,7 @@ static char sccsid[] = "@(#)tr.c      8.2 (Berkeley) 5/4/95";
 #endif
 static const char rcsid[] =
 
-       "$Id: tr.c,v 1.1 2000/03/05 08:07:00 erik Exp $";
+       "$Id: tr.c,v 1.2 2000/03/21 22:32:57 erik Exp $";
 #endif                                                 /* not lint */
 #endif                                                 /* #if 0 */
 
@@ -433,8 +433,7 @@ STR *s;
                                                                                                                "unknown class %s",
                                                                                                                s->str);
 
-       if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL)
-               errx(1, "malloc");
+       cp->set = p = xmalloc((NCHARS + 1) * sizeof(int));
        bzero(p, (NCHARS + 1) * sizeof(int));
 
        for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt)
diff --git a/date.c b/date.c
index b4c3e71535518997677834ec17326b9522c58d29..652db8d74c2cd93d211a665767b592fe08e06ccb 100644 (file)
--- a/date.c
+++ b/date.c
@@ -276,7 +276,7 @@ int date_main(int argc, char **argv)
        }
 
        /* Print OUTPUT (after ALL that!) */
-       t_buff = malloc(201);
+       t_buff = xmalloc(201);
        strftime(t_buff, 200, date_fmt, &tm_time);
        printf("%s\n", t_buff);
 
diff --git a/dd.c b/dd.c
index 0d5b3e8ab9867bc7918208da17b763275e4b72de..f40dec712d0d7c6801150f9589833684c2fd2a09 100644 (file)
--- a/dd.c
+++ b/dd.c
@@ -114,11 +114,7 @@ extern int dd_main(int argc, char **argv)
                argv++;
        }
 
-       buf = malloc(blockSize);
-       if (buf == NULL) {
-               fprintf(stderr, "Cannot allocate buffer\n");
-               exit(FALSE);
-       }
+       buf = xmalloc(blockSize);
 
        intotal = 0;
        outTotal = 0;
diff --git a/dmesg.c b/dmesg.c
index bbed8221aec0d48010d38e9514860f5c424f7456..2bbf43a12b86dd98e4164c337a49a274808472d6 100644 (file)
--- a/dmesg.c
+++ b/dmesg.c
@@ -95,7 +95,7 @@ int dmesg_main(int argc, char **argv)
 
        if (bufsize < 4096)
                bufsize = 4096;
-       buf = (char *) malloc(bufsize);
+       buf = (char *) xmalloc(bufsize);
        n = klogctl(cmd, buf, bufsize);
        if (n < 0) {
                goto klogctl_error;
index cfa973ecf321cbc3401f45c344d8e55db794f176..47e81ce421a4417d73ffa3d03d3ecbe313ac8e22 100644 (file)
@@ -603,23 +603,13 @@ static void read_superblock(void)
 
 static void read_tables(void)
 {
-       inode_map = malloc(IMAPS * BLOCK_SIZE);
-       if (!inode_map)
-               die("Unable to allocate buffer for inode map");
-       zone_map = malloc(ZMAPS * BLOCK_SIZE);
-       if (!inode_map)
-               die("Unable to allocate buffer for zone map");
+       inode_map = xmalloc(IMAPS * BLOCK_SIZE);
+       zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
        memset(inode_map, 0, sizeof(inode_map));
        memset(zone_map, 0, sizeof(zone_map));
-       inode_buffer = malloc(INODE_BUFFER_SIZE);
-       if (!inode_buffer)
-               die("Unable to allocate buffer for inodes");
-       inode_count = malloc(INODES + 1);
-       if (!inode_count)
-               die("Unable to allocate buffer for inode count");
-       zone_count = malloc(ZONES);
-       if (!zone_count)
-               die("Unable to allocate buffer for zone count");
+       inode_buffer = xmalloc(INODE_BUFFER_SIZE);
+       inode_count = xmalloc(INODES + 1);
+       zone_count = xmalloc(ZONES);
        if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
                die("Unable to read inode map");
        if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
@@ -1247,18 +1237,9 @@ static void alloc_name_list(void)
 {
        int i;
 
-       name_list = malloc(sizeof(char *) * MAX_DEPTH);
-       if (!name_list) { 
-               fprintf(stderr,"fsck_minix: name_list: %s\n", strerror(errno));
-               exit(1); 
-       }
-       for (i = 0; i < MAX_DEPTH; i++) {
-               name_list[i] = malloc(sizeof(char) * PATH_MAX + 1);
-               if (!name_list[i]) {
-                       fprintf(stderr,"fsck_minix: name_list: %s\n", strerror(errno));
-                       exit(1); 
-               }
-       }
+       name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
+       for (i = 0; i < MAX_DEPTH; i++)
+               name_list[i] = xmalloc(sizeof(char) * PATH_MAX + 1);
 }
 
 /* execute this atexit() to deallocate name_list[] */
index 4435cb64a82f9a960947ef44bb628923b322f6da..1ee3d4cfa5c6253380326a5b9652246fe82fae4a 100644 (file)
@@ -530,19 +530,15 @@ static void setup_tables(void)
                die("unable to allocate buffers for maps");
        }
        FIRSTZONE = NORM_FIRSTZONE;
-       inode_map = malloc(IMAPS * BLOCK_SIZE);
-       zone_map = malloc(ZMAPS * BLOCK_SIZE);
-       if (!inode_map || !zone_map)
-               die("unable to allocate buffers for maps");
+       inode_map = xmalloc(IMAPS * BLOCK_SIZE);
+       zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
        memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
        memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
        for (i = FIRSTZONE; i < ZONES; i++)
                unmark_zone(i);
        for (i = MINIX_ROOT_INO; i <= INODES; i++)
                unmark_inode(i);
-       inode_buffer = malloc(INODE_BUFFER_SIZE);
-       if (!inode_buffer)
-               die("unable to allocate buffer for inodes");
+       inode_buffer = xmalloc(INODE_BUFFER_SIZE);
        memset(inode_buffer, 0, INODE_BUFFER_SIZE);
        printf("%ld inodes\n", INODES);
        printf("%ld blocks\n", ZONES);
index 130d24162cc003b4b753c99df07792433b03066f..17866a735324522ab7d439906d8fa2b858860809 100644 (file)
--- a/mkswap.c
+++ b/mkswap.c
@@ -116,7 +116,7 @@ static void init_signature_page()
        if (pagesize != PAGE_SIZE)
                fprintf(stderr, "Assuming pages of size %d\n", pagesize);
 #endif
-       signature_page = (int *) malloc(pagesize);
+       signature_page = (int *) xmalloc(pagesize);
        memset(signature_page, 0, pagesize);
        p = (struct swap_header_v1 *) signature_page;
 }
@@ -230,9 +230,7 @@ void check_blocks(void)
        int do_seek = 1;
        char *buffer;
 
-       buffer = malloc(pagesize);
-       if (!buffer)
-               die("Out of memory");
+       buffer = xmalloc(pagesize);
        current_page = 0;
        while (current_page < PAGES) {
                if (!check) {
index f23eb5611bc451c1f2c214ed15912bfaca215a0b..0a740ab178ab40c9aff528c7af1027cf67552385 100644 (file)
--- a/sfdisk.c
+++ b/sfdisk.c
@@ -300,8 +300,7 @@ static struct sector *get_sector(char *dev, int fd, unsigned long sno)
        if (!sseek(dev, fd, sno))
                return 0;
 
-       if (!(s = (struct sector *) malloc(sizeof(struct sector))))
-               fatalError("out of memory - giving up\n");
+       s = (struct sector *) xmalloc(sizeof(struct sector));
 
        if (read(fd, s->data, sizeof(s->data)) != sizeof(s->data)) {
                perror("read");
diff --git a/tr.c b/tr.c
index 8ac09e64175f6f8d49b81132aa69ce7690bb34ea..3bfa4808056d4dc0c694ee30bc886f4ce5814d66 100644 (file)
--- a/tr.c
+++ b/tr.c
@@ -44,7 +44,7 @@ static char sccsid[] = "@(#)tr.c      8.2 (Berkeley) 5/4/95";
 #endif
 static const char rcsid[] =
 
-       "$Id: tr.c,v 1.1 2000/03/05 08:07:00 erik Exp $";
+       "$Id: tr.c,v 1.2 2000/03/21 22:32:57 erik Exp $";
 #endif                                                 /* not lint */
 #endif                                                 /* #if 0 */
 
@@ -433,8 +433,7 @@ STR *s;
                                                                                                                "unknown class %s",
                                                                                                                s->str);
 
-       if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL)
-               errx(1, "malloc");
+       cp->set = p = xmalloc((NCHARS + 1) * sizeof(int));
        bzero(p, (NCHARS + 1) * sizeof(int));
 
        for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt)
index b0f393cca509b0adc95e51aa57186193c4f761df..6661db8789e9d1caa25496e4cfcf02ce8b13c421 100644 (file)
--- a/umount.c
+++ b/umount.c
@@ -89,8 +89,7 @@ void mtab_read(void)
                return;
        }
        while ((e = getmntent(fp))) {
-               entry = malloc(sizeof(struct _mtab_entry_t));
-
+               entry = xmalloc(sizeof(struct _mtab_entry_t));
                entry->device = strdup(e->mnt_fsname);
                entry->mountpt = strdup(e->mnt_dir);
                entry->next = mtab_cache;
index bbed8221aec0d48010d38e9514860f5c424f7456..2bbf43a12b86dd98e4164c337a49a274808472d6 100644 (file)
@@ -95,7 +95,7 @@ int dmesg_main(int argc, char **argv)
 
        if (bufsize < 4096)
                bufsize = 4096;
-       buf = (char *) malloc(bufsize);
+       buf = (char *) xmalloc(bufsize);
        n = klogctl(cmd, buf, bufsize);
        if (n < 0) {
                goto klogctl_error;
index cfa973ecf321cbc3401f45c344d8e55db794f176..47e81ce421a4417d73ffa3d03d3ecbe313ac8e22 100644 (file)
@@ -603,23 +603,13 @@ static void read_superblock(void)
 
 static void read_tables(void)
 {
-       inode_map = malloc(IMAPS * BLOCK_SIZE);
-       if (!inode_map)
-               die("Unable to allocate buffer for inode map");
-       zone_map = malloc(ZMAPS * BLOCK_SIZE);
-       if (!inode_map)
-               die("Unable to allocate buffer for zone map");
+       inode_map = xmalloc(IMAPS * BLOCK_SIZE);
+       zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
        memset(inode_map, 0, sizeof(inode_map));
        memset(zone_map, 0, sizeof(zone_map));
-       inode_buffer = malloc(INODE_BUFFER_SIZE);
-       if (!inode_buffer)
-               die("Unable to allocate buffer for inodes");
-       inode_count = malloc(INODES + 1);
-       if (!inode_count)
-               die("Unable to allocate buffer for inode count");
-       zone_count = malloc(ZONES);
-       if (!zone_count)
-               die("Unable to allocate buffer for zone count");
+       inode_buffer = xmalloc(INODE_BUFFER_SIZE);
+       inode_count = xmalloc(INODES + 1);
+       zone_count = xmalloc(ZONES);
        if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
                die("Unable to read inode map");
        if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
@@ -1247,18 +1237,9 @@ static void alloc_name_list(void)
 {
        int i;
 
-       name_list = malloc(sizeof(char *) * MAX_DEPTH);
-       if (!name_list) { 
-               fprintf(stderr,"fsck_minix: name_list: %s\n", strerror(errno));
-               exit(1); 
-       }
-       for (i = 0; i < MAX_DEPTH; i++) {
-               name_list[i] = malloc(sizeof(char) * PATH_MAX + 1);
-               if (!name_list[i]) {
-                       fprintf(stderr,"fsck_minix: name_list: %s\n", strerror(errno));
-                       exit(1); 
-               }
-       }
+       name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
+       for (i = 0; i < MAX_DEPTH; i++)
+               name_list[i] = xmalloc(sizeof(char) * PATH_MAX + 1);
 }
 
 /* execute this atexit() to deallocate name_list[] */
index 4435cb64a82f9a960947ef44bb628923b322f6da..1ee3d4cfa5c6253380326a5b9652246fe82fae4a 100644 (file)
@@ -530,19 +530,15 @@ static void setup_tables(void)
                die("unable to allocate buffers for maps");
        }
        FIRSTZONE = NORM_FIRSTZONE;
-       inode_map = malloc(IMAPS * BLOCK_SIZE);
-       zone_map = malloc(ZMAPS * BLOCK_SIZE);
-       if (!inode_map || !zone_map)
-               die("unable to allocate buffers for maps");
+       inode_map = xmalloc(IMAPS * BLOCK_SIZE);
+       zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
        memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
        memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
        for (i = FIRSTZONE; i < ZONES; i++)
                unmark_zone(i);
        for (i = MINIX_ROOT_INO; i <= INODES; i++)
                unmark_inode(i);
-       inode_buffer = malloc(INODE_BUFFER_SIZE);
-       if (!inode_buffer)
-               die("unable to allocate buffer for inodes");
+       inode_buffer = xmalloc(INODE_BUFFER_SIZE);
        memset(inode_buffer, 0, INODE_BUFFER_SIZE);
        printf("%ld inodes\n", INODES);
        printf("%ld blocks\n", ZONES);
index 130d24162cc003b4b753c99df07792433b03066f..17866a735324522ab7d439906d8fa2b858860809 100644 (file)
@@ -116,7 +116,7 @@ static void init_signature_page()
        if (pagesize != PAGE_SIZE)
                fprintf(stderr, "Assuming pages of size %d\n", pagesize);
 #endif
-       signature_page = (int *) malloc(pagesize);
+       signature_page = (int *) xmalloc(pagesize);
        memset(signature_page, 0, pagesize);
        p = (struct swap_header_v1 *) signature_page;
 }
@@ -230,9 +230,7 @@ void check_blocks(void)
        int do_seek = 1;
        char *buffer;
 
-       buffer = malloc(pagesize);
-       if (!buffer)
-               die("Out of memory");
+       buffer = xmalloc(pagesize);
        current_page = 0;
        while (current_page < PAGES) {
                if (!check) {
index b0f393cca509b0adc95e51aa57186193c4f761df..6661db8789e9d1caa25496e4cfcf02ce8b13c421 100644 (file)
@@ -89,8 +89,7 @@ void mtab_read(void)
                return;
        }
        while ((e = getmntent(fp))) {
-               entry = malloc(sizeof(struct _mtab_entry_t));
-
+               entry = xmalloc(sizeof(struct _mtab_entry_t));
                entry->device = strdup(e->mnt_fsname);
                entry->mountpt = strdup(e->mnt_dir);
                entry->next = mtab_cache;
index c274bfa1536e4f72318172b50c8666dadb21c7bd..29edbf1c4899e898fe2940ba8ced040dadc7febc 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -172,9 +172,7 @@ void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
     
        i = hash_inode(statbuf->st_ino);
        s = name ? strlen(name) : 0;
-       bucket = malloc(sizeof(ino_dev_hashtable_bucket_t) + s);
-       if (bucket == NULL)
-               fatalError("Not enough memory.");
+       bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
        bucket->ino = statbuf->st_ino;
        bucket->dev = statbuf->st_dev;
        if (name)
@@ -1003,7 +1001,7 @@ extern int replace_match(char *haystack, char *needle, char *newNeedle,
        if (strcmp(needle, newNeedle) == 0)
                return FALSE;
 
-       oldhayStack = (char *) malloc((unsigned) (strlen(haystack)));
+       oldhayStack = (char *) xmalloc((unsigned) (strlen(haystack)));
        while (where != NULL) {
                foundOne++;
                strcpy(oldhayStack, haystack);
@@ -1364,22 +1362,16 @@ extern pid_t findPidByName( char* pidName)
 #endif                                                 /* BB_FEATURE_USE_DEVPS_PATCH */
 #endif                                                 /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
 
-#if defined BB_GUNZIP \
- || defined BB_GZIP   \
- || defined BB_PRINTF \
- || defined BB_TAIL
+/* this should really be farmed out to libbusybox.a */
 extern void *xmalloc(size_t size)
 {
        void *cp = malloc(size);
 
-       if (cp == NULL) {
-               errorMsg("out of memory");
-       }
+       if (cp == NULL)
+               fatalError("out of memory");
        return cp;
 }
 
-#endif                                                 /* BB_GUNZIP || BB_GZIP || BB_PRINTF || BB_TAIL */
-
 #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
 extern int vdprintf(int d, const char *format, va_list ap)
 {