From 322ae93a5e0b78b65831f9fd87fd456eb84d21a1 Mon Sep 17 00:00:00 2001 From: Matt Kraai Date: Wed, 13 Sep 2000 02:46:14 +0000 Subject: [PATCH] Fix calls to {m,c,re}alloc so that they use x{m,c,re}alloc instead of segfaulting or handling errors the same way themselves. --- ar.c | 14 +++++++------- archival/ar.c | 14 +++++++------- cmdedit.c | 14 +++++++------- console-tools/loadfont.c | 6 +----- console-tools/loadkmap.c | 6 +----- coreutils/ln.c | 2 +- coreutils/sort.c | 12 +++--------- coreutils/tail.c | 12 ++++++------ coreutils/test.c | 4 +--- editors/sed.c | 5 ++--- findutils/which.c | 2 +- ln.c | 2 +- loadfont.c | 6 +----- loadkmap.c | 6 +----- mount.c | 4 ++-- procps/ps.c | 2 +- ps.c | 2 +- sed.c | 5 ++--- shell/cmdedit.c | 14 +++++++------- sort.c | 12 +++--------- tail.c | 12 ++++++------ test.c | 4 +--- util-linux/mount.c | 4 ++-- utility.c | 12 ++++-------- which.c | 2 +- 25 files changed, 70 insertions(+), 108 deletions(-) diff --git a/ar.c b/ar.c index d82763df1..241c1fac2 100644 --- a/ar.c +++ b/ar.c @@ -246,7 +246,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct) headerL_t *list; off_t initialOffset; - list = (headerL_t *) malloc(sizeof(headerL_t)); + list = (headerL_t *) xmalloc(sizeof(headerL_t)); initialOffset=lseek(srcFd, 0, SEEK_CUR); if (checkArMagic(srcFd)==TRUE) ar=TRUE; @@ -258,7 +258,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct) if (tar==TRUE) { while(readTarHeader(srcFd, list)==TRUE) { off_t tarOffset; - list->next = (headerL_t *) malloc(sizeof(headerL_t)); + list->next = (headerL_t *) xmalloc(sizeof(headerL_t)); *list->next = *head; *head = *list; @@ -282,7 +282,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct) if (readArEntry(srcFd, list) == FALSE) return(head); } - list->next = (headerL_t *) malloc(sizeof(headerL_t)); + list->next = (headerL_t *) xmalloc(sizeof(headerL_t)); *list->next = *head; *head = *list; /* recursive check for sub-archives */ @@ -349,9 +349,9 @@ extern int ar_main(int argc, char **argv) return (FALSE); } optind++; - entry = (headerL_t *) malloc(sizeof(headerL_t)); - header = (headerL_t *) malloc(sizeof(headerL_t)); - extractList = (headerL_t *) malloc(sizeof(headerL_t)); + entry = (headerL_t *) xmalloc(sizeof(headerL_t)); + header = (headerL_t *) xmalloc(sizeof(headerL_t)); + extractList = (headerL_t *) xmalloc(sizeof(headerL_t)); header = getHeaders(srcFd, header, funct); /* find files to extract or display */ @@ -359,7 +359,7 @@ extern int ar_main(int argc, char **argv) /* only handle specified files */ while(optind < argc) { if ( (entry = findEntry(header, argv[optind])) != NULL) { - entry->next = (headerL_t *) malloc(sizeof(headerL_t)); + entry->next = (headerL_t *) xmalloc(sizeof(headerL_t)); *entry->next = *extractList; *extractList = *entry; } diff --git a/archival/ar.c b/archival/ar.c index d82763df1..241c1fac2 100644 --- a/archival/ar.c +++ b/archival/ar.c @@ -246,7 +246,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct) headerL_t *list; off_t initialOffset; - list = (headerL_t *) malloc(sizeof(headerL_t)); + list = (headerL_t *) xmalloc(sizeof(headerL_t)); initialOffset=lseek(srcFd, 0, SEEK_CUR); if (checkArMagic(srcFd)==TRUE) ar=TRUE; @@ -258,7 +258,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct) if (tar==TRUE) { while(readTarHeader(srcFd, list)==TRUE) { off_t tarOffset; - list->next = (headerL_t *) malloc(sizeof(headerL_t)); + list->next = (headerL_t *) xmalloc(sizeof(headerL_t)); *list->next = *head; *head = *list; @@ -282,7 +282,7 @@ static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct) if (readArEntry(srcFd, list) == FALSE) return(head); } - list->next = (headerL_t *) malloc(sizeof(headerL_t)); + list->next = (headerL_t *) xmalloc(sizeof(headerL_t)); *list->next = *head; *head = *list; /* recursive check for sub-archives */ @@ -349,9 +349,9 @@ extern int ar_main(int argc, char **argv) return (FALSE); } optind++; - entry = (headerL_t *) malloc(sizeof(headerL_t)); - header = (headerL_t *) malloc(sizeof(headerL_t)); - extractList = (headerL_t *) malloc(sizeof(headerL_t)); + entry = (headerL_t *) xmalloc(sizeof(headerL_t)); + header = (headerL_t *) xmalloc(sizeof(headerL_t)); + extractList = (headerL_t *) xmalloc(sizeof(headerL_t)); header = getHeaders(srcFd, header, funct); /* find files to extract or display */ @@ -359,7 +359,7 @@ extern int ar_main(int argc, char **argv) /* only handle specified files */ while(optind < argc) { if ( (entry = findEntry(header, argv[optind])) != NULL) { - entry->next = (headerL_t *) malloc(sizeof(headerL_t)); + entry->next = (headerL_t *) xmalloc(sizeof(headerL_t)); *entry->next = *extractList; *extractList = *entry; } diff --git a/cmdedit.c b/cmdedit.c index 042064f1e..04abc938c 100644 --- a/cmdedit.c +++ b/cmdedit.c @@ -246,11 +246,11 @@ char** username_tab_completion(char* command, int *num_matches) char** exe_n_cwd_tab_completion(char* command, int *num_matches) { char *dirName; - char **matches = (char **) NULL; + char **matches; DIR *dir; struct dirent *next; - matches = malloc( sizeof(char*)*50); + matches = xmalloc( sizeof(char*)*50); /* Stick a wildcard onto the command, for later use */ strcat( command, "*"); @@ -273,7 +273,7 @@ char** exe_n_cwd_tab_completion(char* command, int *num_matches) /* See if this matches */ if (check_wildcard_match(next->d_name, command) == TRUE) { /* Cool, found a match. Add it to the list */ - matches[*num_matches] = malloc(strlen(next->d_name)+1); + matches[*num_matches] = xmalloc(strlen(next->d_name)+1); strcpy( matches[*num_matches], next->d_name); ++*num_matches; //matches = realloc( matches, sizeof(char*)*(*num_matches)); @@ -302,7 +302,7 @@ void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len) /* Make a local copy of the string -- up * to the position of the cursor */ - matchBuf = (char *) calloc(BUFSIZ, sizeof(char)); + matchBuf = (char *) xcalloc(BUFSIZ, sizeof(char)); strncpy(matchBuf, command, cursor); tmp=matchBuf; @@ -670,8 +670,8 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ]) if (!h) { /* No previous history -- this memory is never freed */ - h = his_front = malloc(sizeof(struct history)); - h->n = malloc(sizeof(struct history)); + h = his_front = xmalloc(sizeof(struct history)); + h->n = xmalloc(sizeof(struct history)); h->p = NULL; h->s = strdup(command); @@ -682,7 +682,7 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ]) history_counter++; } else { /* Add a new history command -- this memory is never freed */ - h->n = malloc(sizeof(struct history)); + h->n = xmalloc(sizeof(struct history)); h->n->p = h; h->n->n = NULL; diff --git a/console-tools/loadfont.c b/console-tools/loadfont.c index 927c2bad4..e93ca3186 100644 --- a/console-tools/loadfont.c +++ b/console-tools/loadfont.c @@ -102,12 +102,8 @@ do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize) u_short unicode; maxct = tailsz; /* more than enough */ - up = (struct unipair *) malloc(maxct * sizeof(struct unipair)); + up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair)); - if (!up) { - errorMsg("Out of memory?\n"); - exit(1); - } for (glyph = 0; glyph < fontsize; glyph++) { while (tailsz >= 2) { unicode = (((u_short) inbuf[1]) << 8) + inbuf[0]; diff --git a/console-tools/loadkmap.c b/console-tools/loadkmap.c index 488585f9c..2321a1ede 100644 --- a/console-tools/loadkmap.c +++ b/console-tools/loadkmap.c @@ -67,11 +67,7 @@ int loadkmap_main(int argc, char **argv) exit(FALSE); } - ibuff = (u_short *) malloc(ibuffsz); - if (!ibuff) { - errorMsg("Out of memory.\n"); - exit(FALSE); - } + ibuff = (u_short *) xmalloc(ibuffsz); for (i = 0; i < MAX_NR_KEYMAPS; i++) { if (flags[i] == 1) { diff --git a/coreutils/ln.c b/coreutils/ln.c index 8b8fa1c58..d5f44ea4c 100644 --- a/coreutils/ln.c +++ b/coreutils/ln.c @@ -90,7 +90,7 @@ extern int ln_main(int argc, char **argv) if (linkIntoDirFlag == TRUE) { char *baseName = get_last_path_component(*argv); - linkName = (char *)malloc(strlen(dirName)+strlen(baseName)+2); + linkName = (char *)xmalloc(strlen(dirName)+strlen(baseName)+2); strcpy(linkName, dirName); if(dirName[strlen(dirName)-1] != '/') strcat(linkName, "/"); diff --git a/coreutils/sort.c b/coreutils/sort.c index 6af5c4df3..a74f96ad0 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c @@ -64,7 +64,7 @@ static const int max = 1024; static Line *line_alloc() { Line *self; - self = malloc(1 * sizeof(Line)); + self = xmalloc(1 * sizeof(Line)); return self; } @@ -76,9 +76,6 @@ static Line *line_newFromFile(FILE * src) if ((cstring = get_line_from_file(src))) { self = line_alloc(); - if (self == NULL) { - return NULL; - } self->data = cstring; self->next = NULL; return self; @@ -173,10 +170,7 @@ static List *list_sort(List * self, Compare * compare) Line *line; /* mallocate array of Line*s */ - self->sorted = (Line **) malloc(self->len * sizeof(Line *)); - if (self->sorted == NULL) { - return NULL; - } + self->sorted = (Line **) xmalloc(self->len * sizeof(Line *)); /* fill array w/ List's contents */ i = 0; @@ -294,4 +288,4 @@ int sort_main(int argc, char **argv) return(0); } -/* $Id: sort.c,v 1.20 2000/07/16 20:57:15 kraai Exp $ */ +/* $Id: sort.c,v 1.21 2000/09/13 02:46:13 kraai Exp $ */ diff --git a/coreutils/tail.c b/coreutils/tail.c index c940bfef7..dcb4f6742 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c @@ -87,12 +87,12 @@ int tail_stream(int fd) ssize_t f_size=0; bs=BUFSIZ; - line=malloc(bs); + line=xmalloc(bs); while(1) { bytes_read=read(fd,line,bs); if(bytes_read<=0) break; - buffer=realloc(buffer,f_size+bytes_read); + buffer=xrealloc(buffer,f_size+bytes_read); memcpy(&buffer[f_size],line,bytes_read); filelocation=f_size+=bytes_read; } @@ -150,8 +150,8 @@ int tail_stream(int fd) void add_file(char *name) { ++n_files; - files = realloc(files, n_files); - files[n_files - 1] = (char *) malloc(strlen(name) + 1); + files = xrealloc(files, n_files); + files[n_files - 1] = (char *) xmalloc(strlen(name) + 1); strcpy(files[n_files - 1], name); } @@ -268,13 +268,13 @@ int tail_main(int argc, char **argv) units=-11; if(units>0) units--; - fd=malloc(sizeof(int)*n_files); + fd=xmalloc(sizeof(int)*n_files); if (n_files == 1) #ifndef BB_FEATURE_SIMPLE_TAIL if (!verbose) #endif show_headers = 0; - buffer=malloc(BUFSIZ); + buffer=xmalloc(BUFSIZ); for (test = 0; test < n_files; test++) { if (show_headers) printf("==> %s <==\n", files[test]); diff --git a/coreutils/test.c b/coreutils/test.c index 6dde718c7..a2bec4492 100644 --- a/coreutils/test.c +++ b/coreutils/test.c @@ -555,9 +555,7 @@ static void initialize_group_array () { ngroups = getgroups(0, NULL); - if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL) - fatalError("Out of space\n"); - + group_array = xrealloc(group_array, ngroups * sizeof(gid_t)); getgroups(ngroups, group_array); } diff --git a/editors/sed.c b/editors/sed.c index f3c3262e4..0e0d7f58c 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -44,7 +44,6 @@ */ #include -#include /* for realloc() */ #include /* for getopt() */ #include #include /* for strdup() */ @@ -457,7 +456,7 @@ static void add_cmd_str(const char *cmdstr) continue; } /* grow the array */ - sed_cmds = realloc(sed_cmds, sizeof(struct sed_cmd) * (++ncmds)); + sed_cmds = xrealloc(sed_cmds, sizeof(struct sed_cmd) * (++ncmds)); /* zero new element */ memset(&sed_cmds[ncmds-1], 0, sizeof(struct sed_cmd)); /* load command string into new array element, get remainder */ @@ -481,7 +480,7 @@ static void load_cmd_file(char *filename) /* if a line ends with '\' it needs the next line appended to it */ while (line[strlen(line)-2] == '\\' && (nextline = get_line_from_file(cmdfile)) != NULL) { - line = realloc(line, strlen(line) + strlen(nextline) + 1); + line = xrealloc(line, strlen(line) + strlen(nextline) + 1); strcat(line, nextline); free(nextline); } diff --git a/findutils/which.c b/findutils/which.c index b3fd934d7..07c0e0d85 100644 --- a/findutils/which.c +++ b/findutils/which.c @@ -40,7 +40,7 @@ extern int which_main(int argc, char **argv) if (!path_list) path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"; - path_parsed = malloc (strlen(path_list) + 1); + path_parsed = xmalloc (strlen(path_list) + 1); strcpy (path_parsed, path_list); /* Replace colons with zeros in path_parsed and count them */ diff --git a/ln.c b/ln.c index 8b8fa1c58..d5f44ea4c 100644 --- a/ln.c +++ b/ln.c @@ -90,7 +90,7 @@ extern int ln_main(int argc, char **argv) if (linkIntoDirFlag == TRUE) { char *baseName = get_last_path_component(*argv); - linkName = (char *)malloc(strlen(dirName)+strlen(baseName)+2); + linkName = (char *)xmalloc(strlen(dirName)+strlen(baseName)+2); strcpy(linkName, dirName); if(dirName[strlen(dirName)-1] != '/') strcat(linkName, "/"); diff --git a/loadfont.c b/loadfont.c index 927c2bad4..e93ca3186 100644 --- a/loadfont.c +++ b/loadfont.c @@ -102,12 +102,8 @@ do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize) u_short unicode; maxct = tailsz; /* more than enough */ - up = (struct unipair *) malloc(maxct * sizeof(struct unipair)); + up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair)); - if (!up) { - errorMsg("Out of memory?\n"); - exit(1); - } for (glyph = 0; glyph < fontsize; glyph++) { while (tailsz >= 2) { unicode = (((u_short) inbuf[1]) << 8) + inbuf[0]; diff --git a/loadkmap.c b/loadkmap.c index 488585f9c..2321a1ede 100644 --- a/loadkmap.c +++ b/loadkmap.c @@ -67,11 +67,7 @@ int loadkmap_main(int argc, char **argv) exit(FALSE); } - ibuff = (u_short *) malloc(ibuffsz); - if (!ibuff) { - errorMsg("Out of memory.\n"); - exit(FALSE); - } + ibuff = (u_short *) xmalloc(ibuffsz); for (i = 0; i < MAX_NR_KEYMAPS; i++) { if (flags[i] == 1) { diff --git a/mount.c b/mount.c index b4f5746bc..15ab5c997 100644 --- a/mount.c +++ b/mount.c @@ -273,7 +273,7 @@ mount_one(char *blockDevice, char *directory, char *filesystemType, numfilesystems = ioctl (fd, DEVMTAB_COUNT_FILESYSTEMS); if (numfilesystems<0) fatalError("\nDEVMTAB_COUNT_FILESYSTEMS: %s\n", strerror (errno)); - fslist = (struct k_fstype *) calloc ( numfilesystems, sizeof(struct k_fstype)); + fslist = (struct k_fstype *) xcalloc ( numfilesystems, sizeof(struct k_fstype)); /* Grab the list of available filesystems */ status = ioctl (fd, DEVMTAB_GET_FILESYSTEMS, fslist); @@ -343,7 +343,7 @@ extern int mount_main(int argc, char **argv) numfilesystems = ioctl (fd, DEVMTAB_COUNT_MOUNTS); if (numfilesystems<0) fatalError( "\nDEVMTAB_COUNT_MOUNTS: %s\n", strerror (errno)); - mntentlist = (struct k_mntent *) calloc ( numfilesystems, sizeof(struct k_mntent)); + mntentlist = (struct k_mntent *) xcalloc ( numfilesystems, sizeof(struct k_mntent)); /* Grab the list of mounted filesystems */ if (ioctl (fd, DEVMTAB_GET_MOUNTS, mntentlist)<0) diff --git a/procps/ps.c b/procps/ps.c index ae33e3262..a326bc546 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -229,7 +229,7 @@ extern int ps_main(int argc, char **argv) * some new processes start up while we wait. The kernel will * just ignore any extras if we give it too many, and will trunc. * the list if we give it too few. */ - pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t)); + pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t)); pid_array[0] = num_pids+10; /* Now grab the pid list */ diff --git a/ps.c b/ps.c index ae33e3262..a326bc546 100644 --- a/ps.c +++ b/ps.c @@ -229,7 +229,7 @@ extern int ps_main(int argc, char **argv) * some new processes start up while we wait. The kernel will * just ignore any extras if we give it too many, and will trunc. * the list if we give it too few. */ - pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t)); + pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t)); pid_array[0] = num_pids+10; /* Now grab the pid list */ diff --git a/sed.c b/sed.c index f3c3262e4..0e0d7f58c 100644 --- a/sed.c +++ b/sed.c @@ -44,7 +44,6 @@ */ #include -#include /* for realloc() */ #include /* for getopt() */ #include #include /* for strdup() */ @@ -457,7 +456,7 @@ static void add_cmd_str(const char *cmdstr) continue; } /* grow the array */ - sed_cmds = realloc(sed_cmds, sizeof(struct sed_cmd) * (++ncmds)); + sed_cmds = xrealloc(sed_cmds, sizeof(struct sed_cmd) * (++ncmds)); /* zero new element */ memset(&sed_cmds[ncmds-1], 0, sizeof(struct sed_cmd)); /* load command string into new array element, get remainder */ @@ -481,7 +480,7 @@ static void load_cmd_file(char *filename) /* if a line ends with '\' it needs the next line appended to it */ while (line[strlen(line)-2] == '\\' && (nextline = get_line_from_file(cmdfile)) != NULL) { - line = realloc(line, strlen(line) + strlen(nextline) + 1); + line = xrealloc(line, strlen(line) + strlen(nextline) + 1); strcat(line, nextline); free(nextline); } diff --git a/shell/cmdedit.c b/shell/cmdedit.c index 042064f1e..04abc938c 100644 --- a/shell/cmdedit.c +++ b/shell/cmdedit.c @@ -246,11 +246,11 @@ char** username_tab_completion(char* command, int *num_matches) char** exe_n_cwd_tab_completion(char* command, int *num_matches) { char *dirName; - char **matches = (char **) NULL; + char **matches; DIR *dir; struct dirent *next; - matches = malloc( sizeof(char*)*50); + matches = xmalloc( sizeof(char*)*50); /* Stick a wildcard onto the command, for later use */ strcat( command, "*"); @@ -273,7 +273,7 @@ char** exe_n_cwd_tab_completion(char* command, int *num_matches) /* See if this matches */ if (check_wildcard_match(next->d_name, command) == TRUE) { /* Cool, found a match. Add it to the list */ - matches[*num_matches] = malloc(strlen(next->d_name)+1); + matches[*num_matches] = xmalloc(strlen(next->d_name)+1); strcpy( matches[*num_matches], next->d_name); ++*num_matches; //matches = realloc( matches, sizeof(char*)*(*num_matches)); @@ -302,7 +302,7 @@ void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len) /* Make a local copy of the string -- up * to the position of the cursor */ - matchBuf = (char *) calloc(BUFSIZ, sizeof(char)); + matchBuf = (char *) xcalloc(BUFSIZ, sizeof(char)); strncpy(matchBuf, command, cursor); tmp=matchBuf; @@ -670,8 +670,8 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ]) if (!h) { /* No previous history -- this memory is never freed */ - h = his_front = malloc(sizeof(struct history)); - h->n = malloc(sizeof(struct history)); + h = his_front = xmalloc(sizeof(struct history)); + h->n = xmalloc(sizeof(struct history)); h->p = NULL; h->s = strdup(command); @@ -682,7 +682,7 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ]) history_counter++; } else { /* Add a new history command -- this memory is never freed */ - h->n = malloc(sizeof(struct history)); + h->n = xmalloc(sizeof(struct history)); h->n->p = h; h->n->n = NULL; diff --git a/sort.c b/sort.c index 6af5c4df3..a74f96ad0 100644 --- a/sort.c +++ b/sort.c @@ -64,7 +64,7 @@ static const int max = 1024; static Line *line_alloc() { Line *self; - self = malloc(1 * sizeof(Line)); + self = xmalloc(1 * sizeof(Line)); return self; } @@ -76,9 +76,6 @@ static Line *line_newFromFile(FILE * src) if ((cstring = get_line_from_file(src))) { self = line_alloc(); - if (self == NULL) { - return NULL; - } self->data = cstring; self->next = NULL; return self; @@ -173,10 +170,7 @@ static List *list_sort(List * self, Compare * compare) Line *line; /* mallocate array of Line*s */ - self->sorted = (Line **) malloc(self->len * sizeof(Line *)); - if (self->sorted == NULL) { - return NULL; - } + self->sorted = (Line **) xmalloc(self->len * sizeof(Line *)); /* fill array w/ List's contents */ i = 0; @@ -294,4 +288,4 @@ int sort_main(int argc, char **argv) return(0); } -/* $Id: sort.c,v 1.20 2000/07/16 20:57:15 kraai Exp $ */ +/* $Id: sort.c,v 1.21 2000/09/13 02:46:13 kraai Exp $ */ diff --git a/tail.c b/tail.c index c940bfef7..dcb4f6742 100644 --- a/tail.c +++ b/tail.c @@ -87,12 +87,12 @@ int tail_stream(int fd) ssize_t f_size=0; bs=BUFSIZ; - line=malloc(bs); + line=xmalloc(bs); while(1) { bytes_read=read(fd,line,bs); if(bytes_read<=0) break; - buffer=realloc(buffer,f_size+bytes_read); + buffer=xrealloc(buffer,f_size+bytes_read); memcpy(&buffer[f_size],line,bytes_read); filelocation=f_size+=bytes_read; } @@ -150,8 +150,8 @@ int tail_stream(int fd) void add_file(char *name) { ++n_files; - files = realloc(files, n_files); - files[n_files - 1] = (char *) malloc(strlen(name) + 1); + files = xrealloc(files, n_files); + files[n_files - 1] = (char *) xmalloc(strlen(name) + 1); strcpy(files[n_files - 1], name); } @@ -268,13 +268,13 @@ int tail_main(int argc, char **argv) units=-11; if(units>0) units--; - fd=malloc(sizeof(int)*n_files); + fd=xmalloc(sizeof(int)*n_files); if (n_files == 1) #ifndef BB_FEATURE_SIMPLE_TAIL if (!verbose) #endif show_headers = 0; - buffer=malloc(BUFSIZ); + buffer=xmalloc(BUFSIZ); for (test = 0; test < n_files; test++) { if (show_headers) printf("==> %s <==\n", files[test]); diff --git a/test.c b/test.c index 6dde718c7..a2bec4492 100644 --- a/test.c +++ b/test.c @@ -555,9 +555,7 @@ static void initialize_group_array () { ngroups = getgroups(0, NULL); - if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL) - fatalError("Out of space\n"); - + group_array = xrealloc(group_array, ngroups * sizeof(gid_t)); getgroups(ngroups, group_array); } diff --git a/util-linux/mount.c b/util-linux/mount.c index b4f5746bc..15ab5c997 100644 --- a/util-linux/mount.c +++ b/util-linux/mount.c @@ -273,7 +273,7 @@ mount_one(char *blockDevice, char *directory, char *filesystemType, numfilesystems = ioctl (fd, DEVMTAB_COUNT_FILESYSTEMS); if (numfilesystems<0) fatalError("\nDEVMTAB_COUNT_FILESYSTEMS: %s\n", strerror (errno)); - fslist = (struct k_fstype *) calloc ( numfilesystems, sizeof(struct k_fstype)); + fslist = (struct k_fstype *) xcalloc ( numfilesystems, sizeof(struct k_fstype)); /* Grab the list of available filesystems */ status = ioctl (fd, DEVMTAB_GET_FILESYSTEMS, fslist); @@ -343,7 +343,7 @@ extern int mount_main(int argc, char **argv) numfilesystems = ioctl (fd, DEVMTAB_COUNT_MOUNTS); if (numfilesystems<0) fatalError( "\nDEVMTAB_COUNT_MOUNTS: %s\n", strerror (errno)); - mntentlist = (struct k_mntent *) calloc ( numfilesystems, sizeof(struct k_mntent)); + mntentlist = (struct k_mntent *) xcalloc ( numfilesystems, sizeof(struct k_mntent)); /* Grab the list of mounted filesystems */ if (ioctl (fd, DEVMTAB_GET_MOUNTS, mntentlist)<0) diff --git a/utility.c b/utility.c index 8827b4a65..4defbfc81 100644 --- a/utility.c +++ b/utility.c @@ -1290,7 +1290,7 @@ extern pid_t* findPidByName( char* pidName) * some new processes start up while we wait. The kernel will * just ignore any extras if we give it too many, and will trunc. * the list if we give it too few. */ - pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t)); + pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t)); pid_array[0] = num_pids+10; /* Now grab the pid list */ @@ -1316,9 +1316,7 @@ extern pid_t* findPidByName( char* pidName) if ((strstr(info.command_line, pidName) != NULL) && (strlen(pidName) == strlen(info.command_line))) { - pidList=realloc( pidList, sizeof(pid_t) * (j+2)); - if (pidList==NULL) - fatalError(memory_exhausted); + pidList=xrealloc( pidList, sizeof(pid_t) * (j+2)); pidList[j++]=info.pid; } } @@ -1389,9 +1387,7 @@ extern pid_t* findPidByName( char* pidName) if ((strstr(p, pidName) != NULL) && (strlen(pidName) == strlen(p))) { - pidList=realloc( pidList, sizeof(pid_t) * (i+2)); - if (pidList==NULL) - fatalError(memory_exhausted); + pidList=xrealloc( pidList, sizeof(pid_t) * (i+2)); pidList[i++]=strtol(next->d_name, NULL, 0); } } @@ -1624,7 +1620,7 @@ extern char *get_line_from_file(FILE *file) break; /* grow the line buffer as necessary */ if (idx > linebufsz-2) - linebuf = realloc(linebuf, linebufsz += GROWBY); + linebuf = xrealloc(linebuf, linebufsz += GROWBY); linebuf[idx++] = (char)ch; if ((char)ch == '\n') break; diff --git a/which.c b/which.c index b3fd934d7..07c0e0d85 100644 --- a/which.c +++ b/which.c @@ -40,7 +40,7 @@ extern int which_main(int argc, char **argv) if (!path_list) path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"; - path_parsed = malloc (strlen(path_list) + 1); + path_parsed = xmalloc (strlen(path_list) + 1); strcpy (path_parsed, path_list); /* Replace colons with zeros in path_parsed and count them */