diff --git a/Assemble.c b/Assemble.c index f06fb5c..c0ed917 100644 --- a/Assemble.c +++ b/Assemble.c @@ -727,8 +727,8 @@ int Assemble(struct supertype *st, char *mddev, bitmap_done = 0; #endif /* Ok, no bad inconsistancy, we can try updating etc */ - devices = malloc(num_devs * sizeof(*devices)); - devmap = calloc(num_devs * content->array.raid_disks, 1); + devices = xcalloc(num_devs, sizeof(*devices)); + devmap = xcalloc(num_devs, content->array.raid_disks); for (tmpdev = devlist; tmpdev; tmpdev=tmpdev->next) if (tmpdev->used == 1) { char *devname = tmpdev->devname; struct stat stb; @@ -866,7 +866,7 @@ int Assemble(struct supertype *st, char *mddev, if (i < 10000) { if (i >= bestcnt) { int newbestcnt = i+10; - int *newbest = malloc(sizeof(int)*newbestcnt); + int *newbest = xmalloc(sizeof(int)*newbestcnt); int c; for (c=0; c < newbestcnt; c++) if (c < bestcnt) @@ -931,8 +931,7 @@ int Assemble(struct supertype *st, char *mddev, /* now we have some devices that might be suitable. * I wonder how many */ - avail = malloc(content->array.raid_disks); - memset(avail, 0, content->array.raid_disks); + avail = xcalloc(content->array.raid_disks, 1); okcnt = 0; sparecnt=0; rebuilding_cnt=0; @@ -1199,7 +1198,7 @@ int Assemble(struct supertype *st, char *mddev, #ifndef MDASSEMBLE if (content->reshape_active) { int err = 0; - int *fdlist = malloc(sizeof(int)* bestcnt); + int *fdlist = xmalloc(sizeof(int)* bestcnt); if (verbose > 0) pr_err(":%s has an active reshape - checking " "if critical section needs to be restored\n", diff --git a/Create.c b/Create.c index f150954..af09495 100644 --- a/Create.c +++ b/Create.c @@ -803,11 +803,7 @@ int Create(struct supertype *st, char *mddev, } } - infos = malloc(sizeof(*infos) * total_slots); - if (!infos) { - pr_err("Unable to allocate memory\n"); - goto abort; - } + infos = xmalloc(sizeof(*infos) * total_slots); for (pass=1; pass <=2 ; pass++) { struct mddev_dev *moved_disk = NULL; /* the disk that was moved out of the insert point */ @@ -931,7 +927,7 @@ int Create(struct supertype *st, char *mddev, /* update parent container uuid */ if (me) { - char *path = strdup(me->path); + char *path = xstrdup(me->path); st->ss->getinfo_super(st, &info_new, NULL); map_update(&map, st->container_dev, diff --git a/Detail.c b/Detail.c index f021550..4133c3c 100644 --- a/Detail.c +++ b/Detail.c @@ -146,7 +146,7 @@ int Detail(char *dev, int brief, int export, int test, char *homehost, char *pre if (subarray) info = st->ss->container_content(st, subarray); else { - info = malloc(sizeof(*info)); + info = xmalloc(sizeof(*info)); st->ss->getinfo_super(st, info, NULL); } if (!info) @@ -226,7 +226,7 @@ int Detail(char *dev, int brief, int export, int test, char *homehost, char *pre goto out; } - disks = malloc(max_disks * sizeof(mdu_disk_info_t)); + disks = xmalloc(max_disks * sizeof(mdu_disk_info_t)); for (d=0; ddevs = dl_head(); ap->next = arrays; ap->spares = 0; diff --git a/Grow.c b/Grow.c index 50effc7..8aa0a13 100644 --- a/Grow.c +++ b/Grow.c @@ -49,11 +49,8 @@ int restore_backup(struct supertype *st, int disk_count = next_spare + working_disks; dprintf("Called restore_backup()\n"); - fdlist = malloc(sizeof(int) * disk_count); - if (fdlist == NULL) { - pr_err("cannot allocate memory for disk list\n"); - return 1; - } + fdlist = xmalloc(sizeof(int) * disk_count); + for (i = 0; i < next_spare; i++) fdlist[i] = -1; for (dev = content->devs; dev; dev = dev->next) { @@ -2401,12 +2398,8 @@ started: nrdisks = max(reshape.before.data_disks, reshape.after.data_disks) + reshape.parity + sra->array.spare_disks; - fdlist = malloc((1+nrdisks) * sizeof(int)); - offsets = malloc((1+nrdisks) * sizeof(offsets[0])); - if (!fdlist || !offsets) { - pr_err("malloc failed: grow aborted\n"); - goto release; - } + fdlist = xcalloc((1+nrdisks), sizeof(int)); + offsets = xcalloc((1+nrdisks), sizeof(offsets[0])); odisks = reshape.before.data_disks + reshape.parity; d = reshape_prepare_fdlist(devname, sra, odisks, @@ -3415,8 +3408,8 @@ static void validate(int afd, int bfd, unsigned long long offset) free(abuf); free(bbuf); abuflen = len; - abuf = malloc(abuflen); - bbuf = malloc(abuflen); + abuf = xmalloc(abuflen); + bbuf = xmalloc(abuflen); } lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0); @@ -3804,7 +3797,7 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt goto second_fail; /* Cannot find leading superblock */ /* Now need the data offsets for all devices. */ - offsets = malloc(sizeof(*offsets)*info->array.raid_disks); + offsets = xmalloc(sizeof(*offsets)*info->array.raid_disks); for(j=0; jarray.raid_disks; j++) { if (fdlist[j] < 0) continue; diff --git a/Incremental.c b/Incremental.c index 833eac7..44d5c7c 100644 --- a/Incremental.c +++ b/Incremental.c @@ -657,15 +657,11 @@ static int count_active(struct supertype *st, struct mdinfo *sra, st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum); if (!avail) { raid_disks = info.array.raid_disks; - avail = calloc(raid_disks, 1); - if (!avail) { - pr_err("out of memory.\n"); - exit(1); - } + avail = xcalloc(raid_disks, 1); *availp = avail; - best = calloc(raid_disks, sizeof(int)); - devmap = calloc(raid_disks * numdevs, 1); + best = xcalloc(raid_disks, sizeof(int)); + devmap = xcalloc(raid_disks, numdevs); st->ss->getinfo_super(st, &info, devmap); } diff --git a/Manage.c b/Manage.c index a596363..6ffa78c 100644 --- a/Manage.c +++ b/Manage.c @@ -152,7 +152,7 @@ static void remove_devices(int devnum, char *path) sprintf(base, "/dev/md_d%d", -1-devnum); be = base + strlen(base); - path2 = malloc(strlen(path)+20); + path2 = xmalloc(strlen(path)+20); strcpy(path2, path); pe = path2 + strlen(path2); @@ -865,7 +865,7 @@ int Manage_subdevs(char *devname, int fd, goto abort; } if (array.active_disks < array.raid_disks) { - char *avail = calloc(array.raid_disks, 1); + char *avail = xcalloc(array.raid_disks, 1); int d; int found = 0; @@ -948,8 +948,7 @@ int Manage_subdevs(char *devname, int fd, * As we are "--re-add"ing we must find a spare slot * to fill. */ - char *used = malloc(array.raid_disks); - memset(used, 0, array.raid_disks); + char *used = xcalloc(array.raid_disks, 1); for (j=0; j< tst->max_devs; j++) { mdu_disk_info_t disc2; disc2.number = j; diff --git a/Monitor.c b/Monitor.c index 35a984f..27d448b 100644 --- a/Monitor.c +++ b/Monitor.c @@ -166,13 +166,11 @@ int Monitor(struct mddev_dev *devlist, continue; if (strcasecmp(mdlist->devname, "") == 0) continue; - st = calloc(1, sizeof *st); - if (st == NULL) - continue; + st = xcalloc(1, sizeof *st); if (mdlist->devname[0] == '/') - st->devname = strdup(mdlist->devname); + st->devname = xstrdup(mdlist->devname); else { - st->devname = malloc(8+strlen(mdlist->devname)+1); + st->devname = xmalloc(8+strlen(mdlist->devname)+1); strcpy(strcpy(st->devname, "/dev/md/"), mdlist->devname); } @@ -181,17 +179,15 @@ int Monitor(struct mddev_dev *devlist, st->percent = RESYNC_UNKNOWN; st->expected_spares = mdlist->spare_disks; if (mdlist->spare_group) - st->spare_group = strdup(mdlist->spare_group); + st->spare_group = xstrdup(mdlist->spare_group); statelist = st; } } else { struct mddev_dev *dv; for (dv=devlist ; dv; dv=dv->next) { struct mddev_ident *mdlist = conf_get_ident(dv->devname); - struct state *st = calloc(1, sizeof *st); - if (st == NULL) - continue; - st->devname = strdup(dv->devname); + struct state *st = xcalloc(1, sizeof *st); + st->devname = xstrdup(dv->devname); st->next = statelist; st->devnum = INT_MAX; st->percent = RESYNC_UNKNOWN; @@ -199,7 +195,7 @@ int Monitor(struct mddev_dev *devlist, if (mdlist) { st->expected_spares = mdlist->spare_disks; if (mdlist->spare_group) - st->spare_group = strdup(mdlist->spare_group); + st->spare_group = xstrdup(mdlist->spare_group); } statelist = st; } @@ -670,12 +666,10 @@ static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist, (strcmp(mse->level, "raid0") != 0 && strcmp(mse->level, "linear") != 0)) ) { - struct state *st = calloc(1, sizeof *st); + struct state *st = xcalloc(1, sizeof *st); mdu_array_info_t array; int fd; - if (st == NULL) - continue; - st->devname = strdup(get_md_name(mse->devnum)); + st->devname = xstrdup(get_md_name(mse->devnum)); if ((fd = open(st->devname, O_RDONLY)) < 0 || ioctl(fd, GET_ARRAY_INFO, &array)< 0) { /* no such array */ diff --git a/bitmap.c b/bitmap.c index 351f08a..4144977 100644 --- a/bitmap.c +++ b/bitmap.c @@ -138,18 +138,7 @@ bitmap_info_t *bitmap_fd_read(int fd, int brief) } n = read(fd, buf, 8192); - info = malloc(sizeof(*info)); - if (info == NULL) { -#if __GNUC__ < 3 - pr_err("failed to allocate %d bytes\n", - (int)sizeof(*info)); -#else - pr_err("failed to allocate %zd bytes\n", - sizeof(*info)); -#endif - free(buf); - return NULL; - } + info = xmalloc(sizeof(*info)); if (n < sizeof(info->sb)) { pr_err("failed to read superblock of bitmap " diff --git a/config.c b/config.c index c79d382..9875d3e 100644 --- a/config.c +++ b/config.c @@ -178,8 +178,8 @@ struct mddev_dev *load_partitions(void) name = map_dev(major, minor, 1); if (!name) continue; - d = malloc(sizeof(*d)); - d->devname = strdup(name); + d = xmalloc(sizeof(*d)); + d->devname = xstrdup(name); d->next = rv; d->used = 0; rv = d; @@ -202,9 +202,7 @@ struct mddev_dev *load_containers(void) if (ent->metadata_version && strncmp(ent->metadata_version, "external:", 9) == 0 && !is_subarray(&ent->metadata_version[9])) { - d = malloc(sizeof(*d)); - if (!d) - continue; + d = xmalloc(sizeof(*d)); if (asprintf(&d->devname, "/dev/%s", ent->dev) < 0) { free(d); continue; @@ -351,8 +349,8 @@ void devline(char *line) for (w=dl_next(line); w != line; w=dl_next(w)) { if (w[0] == '/' || strcasecmp(w, "partitions") == 0 || strcasecmp(w, "containers") == 0) { - cd = malloc(sizeof(*cd)); - cd->name = strdup(w); + cd = xmalloc(sizeof(*cd)); + cd->name = xstrdup(w); cd->next = cdevlist; cdevlist = cd; } else { @@ -468,20 +466,20 @@ void arrayline(char *line) pr_err("only specify bitmap file once. %s ignored\n", w); else - mis.bitmap_file = strdup(w+7); + mis.bitmap_file = xstrdup(w+7); } else if (strncasecmp(w, "devices=", 8 ) == 0 ) { if (mis.devices) pr_err("only specify devices once (use a comma separated list). %s ignored\n", w); else - mis.devices = strdup(w+8); + mis.devices = xstrdup(w+8); } else if (strncasecmp(w, "spare-group=", 12) == 0 ) { if (mis.spare_group) pr_err("only specify one spare group per array. %s ignored.\n", w); else - mis.spare_group = strdup(w+12); + mis.spare_group = xstrdup(w+12); } else if (strncasecmp(w, "level=", 6) == 0 ) { /* this is mainly for compatability with --brief output */ mis.level = map_name(pers, w+6); @@ -508,11 +506,11 @@ void arrayline(char *line) mis.autof = parse_auto(w+5, "auto type", 0); } else if (strncasecmp(w, "member=", 7) == 0) { /* subarray within a container */ - mis.member = strdup(w+7); + mis.member = xstrdup(w+7); } else if (strncasecmp(w, "container=", 10) == 0) { /* the container holding this subarray. Either a device name * or a uuid */ - mis.container = strdup(w+10); + mis.container = xstrdup(w+10); } else { pr_err("unrecognised word on ARRAY line: %s\n", w); @@ -523,9 +521,9 @@ void arrayline(char *line) (mis.container == NULL || mis.member == NULL)) pr_err("ARRAY line %s has no identity information.\n", mis.devname); else { - mi = malloc(sizeof(*mi)); + mi = xmalloc(sizeof(*mi)); *mi = mis; - mi->devname = mis.devname ? strdup(mis.devname) : NULL; + mi->devname = mis.devname ? xstrdup(mis.devname) : NULL; mi->next = NULL; *mddevlp = mi; mddevlp = &mi->next; @@ -539,7 +537,7 @@ void mailline(char *line) for (w=dl_next(line); w != line ; w=dl_next(w)) { if (alert_email == NULL) - alert_email = strdup(w); + alert_email = xstrdup(w); else pr_err("excess address on MAIL line: %s - ignored\n", w); @@ -553,7 +551,7 @@ void mailfromline(char *line) for (w=dl_next(line); w != line ; w=dl_next(w)) { if (alert_mail_from == NULL) - alert_mail_from = strdup(w); + alert_mail_from = xstrdup(w); else { char *t = NULL; @@ -573,7 +571,7 @@ void programline(char *line) for (w=dl_next(line); w != line ; w=dl_next(w)) { if (alert_program == NULL) - alert_program = strdup(w); + alert_program = xstrdup(w); else pr_err("excess program on PROGRAM line: %s - ignored\n", w); @@ -591,9 +589,9 @@ void homehostline(char *line) require_homehost = 0; else if (home_host == NULL) { if (strcasecmp(w, "")==0) - home_host = strdup(""); + home_host = xstrdup(""); else - home_host = strdup(w); + home_host = xstrdup(w); }else pr_err("excess host name on HOMEHOST line: %s - ignored\n", w); @@ -647,7 +645,7 @@ void autoline(char *line) for (super_cnt = 0; superlist[super_cnt]; super_cnt++) ; - seen = calloc(super_cnt, 1); + seen = xcalloc(super_cnt, 1); for (w = dl_next(line); w != line ; w = dl_next(w)) { char *val; @@ -885,8 +883,8 @@ struct mddev_dev *conf_get_devs() } if (flags & GLOB_APPEND) { for (i=0; idevname = strdup(globbuf.gl_pathv[i]); + struct mddev_dev *t = xmalloc(sizeof(*t)); + t->devname = xstrdup(globbuf.gl_pathv[i]); t->next = dlist; t->used = 0; dlist = t; diff --git a/dlink.c b/dlink.c index d734281..3e31b43 100644 --- a/dlink.c +++ b/dlink.c @@ -8,6 +8,7 @@ #ifdef __dietlibc__ char *strncpy(char *dest, const char *src, size_t n) __THROW; #endif +void *xcalloc(size_t num, size_t size); #include "dlink.h" @@ -63,14 +64,9 @@ char *dl_strndup(char *s, int l) if (s == NULL) return NULL; n = dl_newv(char, l+1); - if (n == NULL) - return NULL; - else - { - strncpy(n, s, l); - n[l] = 0; - return n; - } + strncpy(n, s, l); + n[l] = 0; + return n; } char *dl_strdup(char *s) diff --git a/dlink.h b/dlink.h index 8e3f4cf..ab2a945 100644 --- a/dlink.h +++ b/dlink.h @@ -8,7 +8,7 @@ struct __dl_head void * dh_next; }; -#define dl_alloc(size) ((void*)(((char*)calloc(1,(size)+sizeof(struct __dl_head)))+sizeof(struct __dl_head))) +#define dl_alloc(size) ((void*)(((char*)xcalloc(1,(size)+sizeof(struct __dl_head)))+sizeof(struct __dl_head))) #define dl_new(t) ((t*)dl_alloc(sizeof(t))) #define dl_newv(t,n) ((t*)dl_alloc(sizeof(t)*n)) diff --git a/lib.c b/lib.c index c04f6a0..082cff5 100644 --- a/lib.c +++ b/lib.c @@ -70,7 +70,7 @@ char *devnum2devname(int num) { char name[100]; fmt_devname(name,num); - return strdup(name); + return xstrdup(name); } int devname2devnum(char *name) @@ -150,8 +150,8 @@ int add_dev(const char *name, const struct stat *stb, int flag, struct FTW *s) } if ((stb->st_mode&S_IFMT)== S_IFBLK) { - char *n = strdup(name); - struct devmap *dm = malloc(sizeof(*dm)); + char *n = xstrdup(name); + struct devmap *dm = xmalloc(sizeof(*dm)); if (strncmp(n, "/dev/./", 7)==0) strcpy(n+4, name+6); if (dm) { @@ -262,9 +262,7 @@ char *conf_word(FILE *file, int allow_key) int c; int quote; int wordfound = 0; - char *word = malloc(wsize); - - if (!word) abort(); + char *word = xmalloc(wsize); while (wordfound==0) { /* at the end of a word.. */ @@ -294,8 +292,7 @@ char *conf_word(FILE *file, int allow_key) else { if (len == wsize-1) { wsize += 100; - word = realloc(word, wsize); - if (!word) abort(); + word = xrealloc(word, wsize); } word[len++] = c; } diff --git a/managemon.c b/managemon.c index 6c21ecb..ef351b3 100644 --- a/managemon.c +++ b/managemon.c @@ -147,7 +147,7 @@ static void free_aa(struct active_array *aa) static struct active_array *duplicate_aa(struct active_array *aa) { - struct active_array *newa = malloc(sizeof(*newa)); + struct active_array *newa = xmalloc(sizeof(*newa)); struct mdinfo **dp1, **dp2; *newa = *aa; @@ -162,7 +162,7 @@ static struct active_array *duplicate_aa(struct active_array *aa) if ((*dp1)->state_fd < 0) continue; - d = malloc(sizeof(*d)); + d = xmalloc(sizeof(*d)); *d = **dp1; *dp2 = d; dp2 = & d->next; @@ -391,12 +391,8 @@ static void manage_container(struct mdstat_ent *mdstat, di->disk.minor == cd->disk.minor) break; if (!cd) { - struct mdinfo *newd = malloc(sizeof(*newd)); + struct mdinfo *newd = xmalloc(sizeof(*newd)); - if (!newd) { - container->devcnt = -1; - continue; - } *newd = *di; add_disk_to_container(container, newd); } @@ -525,9 +521,7 @@ static void manage_member(struct mdstat_ent *mdstat, for (d = newdev; d ; d = d->next) { struct mdinfo *newd; - newd = malloc(sizeof(*newd)); - if (!newd) - continue; + newd = xmalloc(sizeof(*newd)); if (sysfs_add_disk(&newa->info, d, 0) < 0) { free(newd); continue; @@ -577,9 +571,7 @@ static void manage_member(struct mdstat_ent *mdstat, if (!newa) break; } - newd = malloc(sizeof(*newd)); - if (!newd) - continue; + newd = xmalloc(sizeof(*newd)); disk_init_and_add(newd, d, newa); } if (sysfs_get_ll(info, NULL, "array_size", &array_size) == 0 @@ -643,16 +635,10 @@ static void manage_new(struct mdstat_ent *mdstat, GET_LEVEL|GET_CHUNK|GET_DISKS|GET_COMPONENT| GET_DEGRADED|GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE); - new = malloc(sizeof(*new)); - if (!new || !mdi) { - if (mdi) - sysfs_free(mdi); - if (new) - free(new); + if (!mdi) return; - } - memset(new, 0, sizeof(*new)); + new = xcalloc(1, sizeof(*new)); new->devnum = mdstat->devnum; strcpy(new->info.sys_name, devnum2devname(new->devnum)); @@ -668,7 +654,7 @@ static void manage_new(struct mdstat_ent *mdstat, new->info.component_size = mdi->component_size; for (i = 0; i < new->info.array.raid_disks; i++) { - struct mdinfo *newd = malloc(sizeof(*newd)); + struct mdinfo *newd = xmalloc(sizeof(*newd)); for (di = mdi->devs; di; di = di->next) if (i == di->disk.raid_disk) @@ -796,7 +782,7 @@ static void handle_message(struct supertype *container, struct metadata_update * manage(mdstat, container); free_mdstat(mdstat); } else if (!sigterm) { - mu = malloc(sizeof(*mu)); + mu = xmalloc(sizeof(*mu)); mu->len = msg->len; mu->buf = msg->buf; msg->buf = NULL; diff --git a/mapfile.c b/mapfile.c index 70ff355..fa23883 100644 --- a/mapfile.c +++ b/mapfile.c @@ -166,12 +166,12 @@ void map_fork(void) void map_add(struct map_ent **melp, int devnum, char *metadata, int uuid[4], char *path) { - struct map_ent *me = malloc(sizeof(*me)); + struct map_ent *me = xmalloc(sizeof(*me)); me->devnum = devnum; strcpy(me->metadata, metadata); memcpy(me->uuid, uuid, 16); - me->path = path ? strdup(path) : NULL; + me->path = path ? xstrdup(path) : NULL; me->next = *melp; me->bad = 0; *melp = me; @@ -237,7 +237,7 @@ int map_update(struct map_ent **mpp, int devnum, char *metadata, strcpy(mp->metadata, metadata); memcpy(mp->uuid, uuid, 16); free(mp->path); - mp->path = path ? strdup(path) : NULL; + mp->path = path ? xstrdup(path) : NULL; mp->bad = 0; break; } diff --git a/mdadm.c b/mdadm.c index 1ab8267..d346240 100644 --- a/mdadm.c +++ b/mdadm.c @@ -317,11 +317,7 @@ int main(int argc, char *argv[]) /* If first option is a device, don't force the mode yet */ if (opt == 1) { if (devs_found == 0) { - dv = malloc(sizeof(*dv)); - if (dv == NULL) { - pr_err("malloc failed\n"); - exit(3); - } + dv = xmalloc(sizeof(*dv)); dv->devname = optarg; dv->disposition = devmode; dv->writemostly = writemostly; @@ -372,11 +368,7 @@ int main(int argc, char *argv[]) " devices to add: %s\n", optarg); exit(2); } - dv = malloc(sizeof(*dv)); - if (dv == NULL) { - pr_err("malloc failed\n"); - exit(3); - } + dv = xmalloc(sizeof(*dv)); dv->devname = optarg; dv->disposition = devmode; dv->writemostly = writemostly; diff --git a/mdadm.h b/mdadm.h index ff44312..3071c45 100644 --- a/mdadm.h +++ b/mdadm.h @@ -1313,6 +1313,11 @@ static inline int xasprintf(char **strp, const char *fmt, ...) { #define pr_err(fmt ...) fprintf(stderr, Name ": " fmt) #define cont_err(fmt ...) fprintf(stderr, " " fmt) +void *xmalloc(size_t len); +void *xrealloc(void *ptr, size_t len); +void *xcalloc(size_t num, size_t size); +char *xstrdup(const char *str); + #define LEVEL_MULTIPATH (-4) #define LEVEL_LINEAR (-1) #define LEVEL_FAULTY (-5) diff --git a/mdmon.c b/mdmon.c index dee02a9..294cd72 100644 --- a/mdmon.c +++ b/mdmon.c @@ -434,7 +434,7 @@ static int mdmon(char *devname, int devnum, int must_fork, int takeover) } else pfd[0] = pfd[1] = -1; - container = calloc(1, sizeof(*container)); + container = xcalloc(1, sizeof(*container)); container->devnum = devnum; container->devname = devname; container->arrays = NULL; @@ -473,7 +473,7 @@ static int mdmon(char *devname, int devnum, int must_fork, int takeover) container->devs = NULL; for (di = mdi->devs; di; di = di->next) { - struct mdinfo *cd = malloc(sizeof(*cd)); + struct mdinfo *cd = xmalloc(sizeof(*cd)); *cd = *di; cd->next = container->devs; container->devs = cd; diff --git a/mdopen.c b/mdopen.c index 5a096aa..51001c3 100644 --- a/mdopen.c +++ b/mdopen.c @@ -67,7 +67,7 @@ void make_parts(char *dev, int cnt) minor_num = -1; } else return; - name = malloc(nlen); + name = xmalloc(nlen); for (i=1; i <= cnt ; i++) { struct stat stb2; snprintf(name, nlen, "%s%s%d", dev, dig?"p":"", i); diff --git a/mdstat.c b/mdstat.c index 0dece0a..5f690e5 100644 --- a/mdstat.c +++ b/mdstat.c @@ -103,7 +103,7 @@ static int add_member_devname(struct dev_member **m, char *name) /* not a device */ return 0; - new = malloc(sizeof(*new)); + new = xmalloc(sizeof(*new)); new->name = strndup(name, t - name); new->next = *m; *m = new; @@ -177,12 +177,7 @@ struct mdstat_ent *mdstat_read(int hold, int start) continue; } - ent = malloc(sizeof(*ent)); - if (!ent) { - pr_err("malloc failed reading /proc/mdstat.\n"); - free_line(line); - break; - } + ent = xmalloc(sizeof(*ent)); ent->dev = ent->level = ent->pattern= NULL; ent->next = NULL; ent->percent = RESYNC_NONE; @@ -193,7 +188,7 @@ struct mdstat_ent *mdstat_read(int hold, int start) ent->devcnt = 0; ent->members = NULL; - ent->dev = strdup(line); + ent->dev = xstrdup(line); ent->devnum = devnum; for (w=dl_next(line); w!= line ; w=dl_next(w)) { @@ -207,7 +202,7 @@ struct mdstat_ent *mdstat_read(int hold, int start) } else if (ent->active > 0 && ent->level == NULL && w[0] != '(' /*readonly*/) { - ent->level = strdup(w); + ent->level = xstrdup(w); in_devs = 1; } else if (in_devs && strcmp(w, "blocks")==0) in_devs = 0; @@ -231,13 +226,13 @@ struct mdstat_ent *mdstat_read(int hold, int start) } else if (strcmp(w, "super") == 0 && dl_next(w) != line) { w = dl_next(w); - ent->metadata_version = strdup(w); + ent->metadata_version = xstrdup(w); } else if (w[0] == '[' && isdigit(w[1])) { ent->raid_disks = atoi(w+1); } else if (!ent->pattern && w[0] == '[' && (w[1] == 'U' || w[1] == '_')) { - ent->pattern = strdup(w+1); + ent->pattern = xstrdup(w+1); if (ent->pattern[l-2]==']') ent->pattern[l-2] = '\0'; } else if (ent->percent == RESYNC_NONE && diff --git a/msg.c b/msg.c index e531ef7..70cefe8 100644 --- a/msg.c +++ b/msg.c @@ -106,9 +106,7 @@ int receive_message(int fd, struct metadata_update *msg, int tmo) if (rv < 0 || len > MSG_MAX_LEN) return -1; if (len > 0) { - msg->buf = malloc(len); - if (msg->buf == NULL) - return -1; + msg->buf = xmalloc(len); rv = recv_buf(fd, msg->buf, len, tmo); if (rv < 0) { free(msg->buf); diff --git a/platform-intel.c b/platform-intel.c index cb6c7dc..d6698c7 100644 --- a/platform-intel.c +++ b/platform-intel.c @@ -102,10 +102,10 @@ struct sys_dev *find_driver_devices(const char *bus, const char *driver) /* start / add list entry */ if (!head) { - head = malloc(sizeof(*head)); + head = xmalloc(sizeof(*head)); list = head; } else { - list->next = malloc(sizeof(*head)); + list->next = xmalloc(sizeof(*head)); list = list->next; } diff --git a/policy.c b/policy.c index 02f70fe..8ee6a6c 100644 --- a/policy.c +++ b/policy.c @@ -43,7 +43,7 @@ static void pol_new(struct dev_policy **pol, char *name, const char *val, const char *metadata) { - struct dev_policy *n = malloc(sizeof(*n)); + struct dev_policy *n = xmalloc(sizeof(*n)); const char *real_metadata = NULL; int i; @@ -217,7 +217,7 @@ static char *disk_path(struct mdinfo *disk) if (stb.st_rdev != makedev(disk->disk.major, disk->disk.minor)) continue; closedir(by_path); - return strdup(ent->d_name); + return xstrdup(ent->d_name); } closedir(by_path); /* A NULL path isn't really acceptable - use the devname.. */ @@ -228,9 +228,9 @@ static char *disk_path(struct mdinfo *disk) nm[rv] = 0; dname = strrchr(nm, '/'); if (dname) - return strdup(dname + 1); + return xstrdup(dname + 1); } - return strdup("unknown"); + return xstrdup("unknown"); } char type_part[] = "part"; @@ -451,10 +451,10 @@ static int try_rule(char *w, char *name, struct rule **rp) if (strncmp(w, name, len) != 0 || w[len] != '=') return 0; - r = malloc(sizeof(*r)); + r = xmalloc(sizeof(*r)); r->next = *rp; r->name = name; - r->value = strdup(w+len+1); + r->value = xstrdup(w+len+1); r->dups = NULL; *rp = r; return 1; @@ -468,7 +468,7 @@ void policyline(char *line, char *type) if (config_rules_end == NULL) config_rules_end = &config_rules; - pr = malloc(sizeof(*pr)); + pr = xmalloc(sizeof(*pr)); pr->type = type; pr->rule = NULL; for (w = dl_next(line); w != line ; w = dl_next(w)) { @@ -492,7 +492,7 @@ void policy_add(char *type, ...) struct pol_rule *pr; char *name, *val; - pr = malloc(sizeof(*pr)); + pr = xmalloc(sizeof(*pr)); pr->type = type; pr->rule = NULL; @@ -501,10 +501,10 @@ void policy_add(char *type, ...) struct rule *r; val = va_arg(ap, char*); - r = malloc(sizeof(*r)); + r = xmalloc(sizeof(*r)); r->next = pr->rule; r->name = name; - r->value = strdup(val); + r->value = xstrdup(val); r->dups = NULL; pr->rule = r; } @@ -618,7 +618,7 @@ static struct domainlist **domain_merge_one(struct domainlist **domp, dom = *domp; } if (dom == NULL || strcmp(dom->dom, domain) != 0) { - dom = malloc(sizeof(*dom)); + dom = xmalloc(sizeof(*dom)); dom->next = *domp; dom->dom = domain; *domp = dom; diff --git a/raid6check.c b/raid6check.c index 1d91c96..be7a449 100644 --- a/raid6check.c +++ b/raid6check.c @@ -106,12 +106,12 @@ int check_stripes(struct mdinfo *info, int *source, unsigned long long *offsets, unsigned long long start, unsigned long long length, char *name[]) { /* read the data and p and q blocks, and check we got them right */ - char *stripe_buf = malloc(raid_disks * chunk_size); - char **stripes = malloc(raid_disks * sizeof(char*)); - char **blocks = malloc(raid_disks * sizeof(char*)); - uint8_t *p = malloc(chunk_size); - uint8_t *q = malloc(chunk_size); - int *results = malloc(chunk_size * sizeof(int)); + char *stripe_buf = xmalloc(raid_disks * chunk_size); + char **stripes = xmalloc(raid_disks * sizeof(char*)); + char **blocks = xmalloc(raid_disks * sizeof(char*)); + uint8_t *p = xmalloc(chunk_size); + uint8_t *q = xmalloc(chunk_size); + int *results = xmalloc(chunk_size * sizeof(int)); int i; int diskP, diskQ; @@ -122,16 +122,6 @@ int check_stripes(struct mdinfo *info, int *source, unsigned long long *offsets, extern int tables_ready; - if((stripe_buf == NULL) || - (stripes == NULL) || - (blocks == NULL) || - (p == NULL) || - (q == NULL) || - (results == NULL)) { - err = 1; - goto exitCheck; - } - if (!tables_ready) make_tables(); @@ -340,21 +330,11 @@ int main(int argc, char *argv[]) length = (info->component_size * 512) / chunk_size - start; } - disk_name = malloc(raid_disks * sizeof(*disk_name)); - fds = malloc(raid_disks * sizeof(*fds)); - offsets = malloc(raid_disks * sizeof(*offsets)); - buf = malloc(raid_disks * chunk_size); + disk_name = xmalloc(raid_disks * sizeof(*disk_name)); + fds = xmalloc(raid_disks * sizeof(*fds)); + offsets = xcalloc(raid_disks, sizeof(*offsets)); + buf = xmalloc(raid_disks * chunk_size); - if((disk_name == NULL) || - (fds == NULL) || - (offsets == NULL) || - (buf == NULL)) { - fprintf(stderr, "%s: allocation fail\n", prg); - exit_err = 5; - goto exitHere; - } - - memset(offsets, 0, raid_disks * sizeof(*offsets)); for(i=0; i zero_size) { if (zero) free(zero); - zero = malloc(chunk_size); - if (zero) - memset(zero, 0, chunk_size); + zero = xcalloc(1, chunk_size); zero_size = chunk_size; } @@ -684,8 +682,8 @@ int restore_stripes(int *dest, unsigned long long *offsets, char *src_buf) { char *stripe_buf; - char **stripes = malloc(raid_disks * sizeof(char*)); - char **blocks = malloc(raid_disks * sizeof(char*)); + char **stripes = xmalloc(raid_disks * sizeof(char*)); + char **blocks = xmalloc(raid_disks * sizeof(char*)); int i; int rv; @@ -697,9 +695,7 @@ int restore_stripes(int *dest, unsigned long long *offsets, if (zero == NULL || chunk_size > zero_size) { if (zero) free(zero); - zero = malloc(chunk_size); - if (zero) - memset(zero, 0, chunk_size); + zero = xcalloc(1, chunk_size); zero_size = chunk_size; } @@ -816,11 +812,11 @@ int test_stripes(int *source, unsigned long long *offsets, unsigned long long start, unsigned long long length) { /* ready the data and p (and q) blocks, and check we got them right */ - char *stripe_buf = malloc(raid_disks * chunk_size); - char **stripes = malloc(raid_disks * sizeof(char*)); - char **blocks = malloc(raid_disks * sizeof(char*)); - char *p = malloc(chunk_size); - char *q = malloc(chunk_size); + char *stripe_buf = xmalloc(raid_disks * chunk_size); + char **stripes = xmalloc(raid_disks * sizeof(char*)); + char **blocks = xmalloc(raid_disks * sizeof(char*)); + char *p = xmalloc(chunk_size); + char *q = xmalloc(chunk_size); int i; int diskP, diskQ; @@ -935,9 +931,8 @@ main(int argc, char *argv[]) raid_disks, argc-9); exit(2); } - fds = malloc(raid_disks * sizeof(*fds)); - offsets = malloc(raid_disks * sizeof(*offsets)); - memset(offsets, 0, raid_disks * sizeof(*offsets)); + fds = xmalloc(raid_disks * sizeof(*fds)); + offsets = xcalloc(raid_disks, sizeof(*offsets)); storefd = open(file, O_RDWR); if (storefd < 0) { @@ -962,7 +957,7 @@ main(int argc, char *argv[]) } } - buf = malloc(raid_disks * chunk_size); + buf = xmalloc(raid_disks * chunk_size); if (save == 1) { int rv = save_stripes(fds, offsets, @@ -998,4 +993,26 @@ main(int argc, char *argv[]) exit(0); } + +void *xmalloc(size_t len) +{ + void *rv = malloc(len); + char *msg; + if (rv) + return rv; + msg = Name ": memory allocation failure - aborting\n"; + write(2, msg, strlen(msg)); + exit(4); +} + +void *xcalloc(size_t num, size_t size) +{ + void *rv = calloc(num, size); + char *msg; + if (rv) + return rv; + msg = Name ": memory allocation failure - aborting\n"; + write(2, msg, strlen(msg)); + exit(4); +} #endif /* MAIN */ diff --git a/super-ddf.c b/super-ddf.c index e8ea7e3..1ddf9d1 100644 --- a/super-ddf.c +++ b/super-ddf.c @@ -658,7 +658,7 @@ static int load_ddf_local(int fd, struct ddf_super *super, super->active->data_section_offset, super->active->data_section_length, 0); - dl->devname = devname ? strdup(devname) : NULL; + dl->devname = devname ? xstrdup(devname) : NULL; fstat(fd, &stb); dl->major = major(stb.st_rdev); @@ -884,8 +884,7 @@ static struct supertype *match_metadata_desc_ddf(char *arg) ) return NULL; - st = malloc(sizeof(*st)); - memset(st, 0, sizeof(*st)); + st = xcalloc(1, sizeof(*st)); st->container_dev = NoMdDev; st->ss = &super_ddf; st->max_devs = 512; @@ -1918,9 +1917,7 @@ FIXME ignore DDF_Legacy devices? int n = 0; unsigned int i, j; - rv = malloc(sizeof(struct extent) * (ddf->max_part + 2)); - if (!rv) - return NULL; + rv = xmalloc(sizeof(struct extent) * (ddf->max_part + 2)); for (i = 0; i < ddf->max_part; i++) { struct vcl *v = dl->vlist[i]; @@ -2244,7 +2241,7 @@ static int add_to_super_ddf(struct supertype *st, sizeof(struct phys_disk_entry)); struct phys_disk *pd; - pd = malloc(len); + pd = xmalloc(len); pd->magic = DDF_PHYS_RECORDS_MAGIC; pd->used_pdes = __cpu_to_be16(n); pde = &pd->entries[0]; @@ -2302,7 +2299,7 @@ static int remove_from_super_ddf(struct supertype *st, mdu_disk_info_t *dk) sizeof(struct phys_disk_entry)); struct phys_disk *pd; - pd = malloc(len); + pd = xmalloc(len); pd->magic = DDF_PHYS_RECORDS_MAGIC; pd->used_pdes = __cpu_to_be16(dl->pdnum); pd->entries[0].state = __cpu_to_be16(DDF_Missing); @@ -2463,7 +2460,7 @@ static int write_init_super_ddf(struct supertype *st) /* First the virtual disk. We have a slightly fake header */ len = sizeof(struct virtual_disk) + sizeof(struct virtual_entry); - vd = malloc(len); + vd = xmalloc(len); *vd = *ddf->virt; vd->entries[0] = ddf->virt->entries[currentconf->vcnum]; vd->populated_vdes = __cpu_to_be16(currentconf->vcnum); @@ -2471,7 +2468,7 @@ static int write_init_super_ddf(struct supertype *st) /* Then the vd_config */ len = ddf->conf_rec_len * 512; - vc = malloc(len); + vc = xmalloc(len); memcpy(vc, ¤tconf->conf, len); append_metadata_update(st, vc, len); @@ -2970,8 +2967,7 @@ static struct mdinfo *container_content_ddf(struct supertype *st, char *subarray *ep != '\0')) continue; - this = malloc(sizeof(*this)); - memset(this, 0, sizeof(*this)); + this = xcalloc(1, sizeof(*this)); this->next = rest; rest = this; @@ -3049,8 +3045,7 @@ static struct mdinfo *container_content_ddf(struct supertype *st, char *subarray /* Haven't found that one yet, maybe there are others */ continue; - dev = malloc(sizeof(*dev)); - memset(dev, 0, sizeof(*dev)); + dev = xcalloc(1, sizeof(*dev)); dev->next = this->devs; this->devs = dev; @@ -3742,10 +3737,7 @@ static struct mdinfo *ddf_activate_spare(struct active_array *a, } /* Cool, we have a device with some space at pos */ - di = malloc(sizeof(*di)); - if (!di) - continue; - memset(di, 0, sizeof(*di)); + di = xcalloc(1, sizeof(*di)); di->disk.number = i; di->disk.raid_disk = i; di->disk.major = dl->major; @@ -3777,22 +3769,12 @@ static struct mdinfo *ddf_activate_spare(struct active_array *a, * Create a metadata_update record to update the * phys_refnum and lba_offset values */ - mu = malloc(sizeof(*mu)); - if (mu && posix_memalign(&mu->space, 512, sizeof(struct vcl)) != 0) { + mu = xmalloc(sizeof(*mu)); + if (posix_memalign(&mu->space, 512, sizeof(struct vcl)) != 0) { free(mu); mu = NULL; } - if (!mu) { - while (rv) { - struct mdinfo *n = rv->next; - - free(rv); - rv = n; - } - return NULL; - } - - mu->buf = malloc(ddf->conf_rec_len * 512); + mu->buf = xmalloc(ddf->conf_rec_len * 512); mu->len = ddf->conf_rec_len * 512; mu->space = NULL; mu->space_list = NULL; diff --git a/super-gpt.c b/super-gpt.c index 9360aea..fa7fdab 100644 --- a/super-gpt.c +++ b/super-gpt.c @@ -175,7 +175,7 @@ static void getinfo_gpt(struct supertype *st, struct mdinfo *info, char *map) static struct supertype *match_metadata_desc(char *arg) { - struct supertype *st = malloc(sizeof(*st)); + struct supertype *st = xmalloc(sizeof(*st)); if (!st) return st; diff --git a/super-intel.c b/super-intel.c index 2ecb958..2f04302 100644 --- a/super-intel.c +++ b/super-intel.c @@ -525,14 +525,14 @@ const char *get_sys_dev_type(enum sys_dev_type type) static struct intel_hba * alloc_intel_hba(struct sys_dev *device) { - struct intel_hba *result = malloc(sizeof(*result)); - if (result) { - result->type = device->type; - result->path = strdup(device->path); - result->next = NULL; - if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL) - result->pci_id++; - } + struct intel_hba *result = xmalloc(sizeof(*result)); + + result->type = device->type; + result->path = xstrdup(device->path); + result->next = NULL; + if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL) + result->pci_id++; + return result; } @@ -625,10 +625,7 @@ static struct supertype *match_metadata_desc_imsm(char *arg) ) return NULL; - st = malloc(sizeof(*st)); - if (!st) - return NULL; - memset(st, 0, sizeof(*st)); + st = xcalloc(1, sizeof(*st)); st->container_dev = NoMdDev; st->ss = &super_imsm; st->max_devs = IMSM_MAX_DEVICES; @@ -964,9 +961,7 @@ static struct extent *get_extents(struct intel_super *super, struct dl *dl) else reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; - rv = malloc(sizeof(struct extent) * (memberships + 1)); - if (!rv) - return NULL; + rv = xcalloc(sizeof(struct extent), (memberships + 1)); e = rv; for (i = 0; i < super->anchor->num_raid_devs; i++) { @@ -2317,7 +2312,7 @@ static int imsm_create_metadata_checkpoint_update( update_memory_size = sizeof(struct imsm_update_general_migration_checkpoint); - *u = calloc(1, update_memory_size); + *u = xcalloc(1, update_memory_size); if (*u == NULL) { dprintf("error: cannot get memory for " "imsm_create_metadata_checkpoint_update update\n"); @@ -2845,23 +2840,11 @@ struct mdinfo *getinfo_super_disks_imsm(struct supertype *st) if (!super || !super->disks) return NULL; dl = super->disks; - mddev = malloc(sizeof(*mddev)); - if (!mddev) { - pr_err("Failed to allocate memory.\n"); - return NULL; - } - memset(mddev, 0, sizeof(*mddev)); + mddev = xcalloc(1, sizeof(*mddev)); while (dl) { struct mdinfo *tmp; disk = &dl->disk; - tmp = malloc(sizeof(*tmp)); - if (!tmp) { - pr_err("Failed to allocate memory.\n"); - if (mddev) - sysfs_free(mddev); - return NULL; - } - memset(tmp, 0, sizeof(*tmp)); + tmp = xcalloc(1, sizeof(*tmp)); if (mddev->devs) tmp->next = mddev->devs; mddev->devs = tmp; @@ -3063,14 +3046,8 @@ static int compare_super_imsm(struct supertype *st, struct supertype *tst) * fails here we don't associate the spare */ for (i = 0; i < sec->anchor->num_raid_devs; i++) { - dv = malloc(sizeof(*dv)); - if (!dv) - break; - dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1)); - if (!dev) { - free(dv); - break; - } + dv = xmalloc(sizeof(*dv)); + dev = xmalloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1)); dv->dev = dev; dv->index = i; dv->next = first->devlist; @@ -3243,13 +3220,7 @@ load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) if (rv != 0) return 2; - dl = calloc(1, sizeof(*dl)); - if (!dl) { - if (devname) - pr_err("failed to allocate disk buffer for %s\n", - devname); - return 2; - } + dl = xcalloc(1, sizeof(*dl)); fstat(fd, &stb); dl->major = major(stb.st_rdev); @@ -3263,9 +3234,9 @@ load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) dl->e = NULL; fd2devname(fd, name); if (devname) - dl->devname = strdup(devname); + dl->devname = xstrdup(devname); else - dl->devname = strdup(name); + dl->devname = xstrdup(name); /* look up this disk's index in the current anchor */ disk = __serial_to_disk(dl->serial, super->anchor, &dl->index); @@ -3392,18 +3363,12 @@ static int parse_raid_devices(struct intel_super *super) if (len_migr > len) space_needed += len_migr - len; - dv = malloc(sizeof(*dv)); - if (!dv) - return 1; + dv = xmalloc(sizeof(*dv)); if (max_len < len_migr) max_len = len_migr; if (max_len > len_migr) space_needed += max_len - len_migr; - dev_new = malloc(max_len); - if (!dev_new) { - free(dv); - return 1; - } + dev_new = xmalloc(max_len); imsm_copy_dev(dev_new, dev_iter); dv->dev = dev_new; dv->index = i; @@ -3729,13 +3694,10 @@ static void free_super_imsm(struct supertype *st) static struct intel_super *alloc_super(void) { - struct intel_super *super = malloc(sizeof(*super)); + struct intel_super *super = xcalloc(1, sizeof(*super)); - if (super) { - memset(super, 0, sizeof(*super)); - super->current_vol = -1; - super->create_offset = ~((unsigned long long) 0); - } + super->current_vol = -1; + super->create_offset = ~((unsigned long long) 0); return super; } @@ -3811,13 +3773,11 @@ static int find_missing(struct intel_super *super) if (dl) continue; - dl = malloc(sizeof(*dl)); - if (!dl) - return 1; + dl = xmalloc(sizeof(*dl)); dl->major = 0; dl->minor = 0; dl->fd = -1; - dl->devname = strdup("missing"); + dl->devname = xstrdup("missing"); dl->index = i; serialcpy(dl->serial, disk->serial); dl->disk = *disk; @@ -3932,9 +3892,7 @@ static int __prep_thunderdome(struct intel_super **table, int tbl_size, is_failed(&idisk->disk)) idisk->disk.status &= ~(SPARE_DISK); } else { - idisk = calloc(1, sizeof(*idisk)); - if (!idisk) - return -1; + idisk = xcalloc(1, sizeof(*idisk)); idisk->owner = IMSM_UNKNOWN_OWNER; idisk->disk = *disk; idisk->next = *disk_list; @@ -4394,11 +4352,6 @@ static int load_super_imsm(struct supertype *st, int fd, char *devname) free_super_imsm(st); super = alloc_super(); - if (!super) { - pr_err("malloc of %zu failed.\n", - sizeof(*super)); - return 1; - } /* Load hba and capabilities if they exist. * But do not preclude loading metadata in case capabilities or hba are * non-compliant and ignore_hw_compat is set. @@ -4621,18 +4574,8 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, if (!check_name(super, name, 0)) return 0; - dv = malloc(sizeof(*dv)); - if (!dv) { - pr_err("failed to allocate device list entry\n"); - return 0; - } - dev = calloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); - if (!dev) { - free(dv); - pr_err("could not allocate raid device\n"); - return 0; - } - + dv = xmalloc(sizeof(*dv)); + dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN); array_blocks = calc_array_size(info->level, info->raid_disks, info->layout, info->chunk_size, @@ -4937,15 +4880,10 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, return add_to_super_imsm_volume(st, dk, fd, devname); fstat(fd, &stb); - dd = malloc(sizeof(*dd)); - if (!dd) { - pr_err("malloc failed %s:%d.\n", __func__, __LINE__); - return 1; - } - memset(dd, 0, sizeof(*dd)); + dd = xcalloc(sizeof(*dd), 1); dd->major = major(stb.st_rdev); dd->minor = minor(stb.st_rdev); - dd->devname = devname ? strdup(devname) : NULL; + dd->devname = devname ? xstrdup(devname) : NULL; dd->fd = fd; dd->e = NULL; dd->action = DISK_ADD; @@ -4997,12 +4935,7 @@ static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk) "(line %d).\n", __func__, __LINE__); return 1; } - dd = malloc(sizeof(*dd)); - if (!dd) { - pr_err("malloc failed %s:%d.\n", __func__, __LINE__); - return 1; - } - memset(dd, 0, sizeof(*dd)); + dd = xcalloc(1, sizeof(*dd)); dd->major = dk->major; dd->minor = dk->minor; dd->fd = -1; @@ -5183,13 +5116,7 @@ static int create_array(struct supertype *st, int dev_idx) len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) + sizeof(*inf) * map->num_members; - u = malloc(len); - if (!u) { - fprintf(stderr, "%s: failed to allocate update buffer\n", - __func__); - return 1; - } - + u = xmalloc(len); u->type = update_create_array; u->dev_idx = dev_idx; imsm_copy_dev(&u->dev, dev); @@ -5215,13 +5142,7 @@ static int mgmt_disk(struct supertype *st) return 0; len = sizeof(*u); - u = malloc(len); - if (!u) { - fprintf(stderr, "%s: failed to allocate update buffer\n", - __func__); - return 1; - } - + u = xmalloc(len); u->type = update_add_remove_disk; append_metadata_update(st, u, len); @@ -5313,13 +5234,6 @@ static int validate_geometry_imsm_container(struct supertype *st, int level, * note that there is no fd for the disks in array. */ super = alloc_super(); - if (!super) { - pr_err("malloc of %zu failed.\n", - sizeof(*super)); - close(fd); - return 0; - } - rv = find_intel_hba_capability(fd, super, verbose ? dev : NULL); if (rv != 0) { #if DEBUG @@ -5391,7 +5305,7 @@ static unsigned long long merge_extents(struct intel_super *super, int sum_exten * 'maxsize' given the "all disks in an array must share a common start * offset" constraint */ - struct extent *e = calloc(sum_extents, sizeof(*e)); + struct extent *e = xcalloc(sum_extents, sizeof(*e)); struct dl *dl; int i, j; int start_extent; @@ -5400,9 +5314,6 @@ static unsigned long long merge_extents(struct intel_super *super, int sum_exten unsigned long long maxsize; unsigned long reserve; - if (!e) - return 0; - /* coalesce and sort all extents. also, check to see if we need to * reserve space between member arrays */ @@ -5509,17 +5420,15 @@ active_arrays_by_format(char *name, char* hba, struct md_list **devlist, struct dev_member *dev = memb->members; int fd = -1; while(dev && (fd < 0)) { - char *path = malloc(strlen(dev->name) + strlen("/dev/") + 1); - if (path) { - num = sprintf(path, "%s%s", "/dev/", dev->name); - if (num > 0) - fd = open(path, O_RDONLY, 0); - if ((num <= 0) || (fd < 0)) { - pr_vrb(": Cannot open %s: %s\n", - dev->name, strerror(errno)); - } - free(path); + char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1); + num = sprintf(path, "%s%s", "/dev/", dev->name); + if (num > 0) + fd = open(path, O_RDONLY, 0); + if ((num <= 0) || (fd < 0)) { + pr_vrb(": Cannot open %s: %s\n", + dev->name, strerror(errno)); } + free(path); dev = dev->next; } found = 0; @@ -5534,20 +5443,13 @@ active_arrays_by_format(char *name, char* hba, struct md_list **devlist, } } if (*devlist && (found < dpa)) { - dv = calloc(1, sizeof(*dv)); - if (dv == NULL) - pr_err("calloc failed\n"); - else { - dv->devname = malloc(strlen(memb->dev) + strlen("/dev/") + 1); - if (dv->devname != NULL) { - sprintf(dv->devname, "%s%s", "/dev/", memb->dev); - dv->found = found; - dv->used = 0; - dv->next = *devlist; - *devlist = dv; - } else - free(dv); - } + dv = xcalloc(1, sizeof(*dv)); + dv->devname = xmalloc(strlen(memb->dev) + strlen("/dev/") + 1); + sprintf(dv->devname, "%s%s", "/dev/", memb->dev); + dv->found = found; + dv->used = 0; + dv->next = *devlist; + *devlist = dv; } } if (fd >= 0) @@ -5567,17 +5469,8 @@ get_loop_devices(void) struct md_list *dv = NULL; for(i = 0; i < 12; i++) { - dv = calloc(1, sizeof(*dv)); - if (dv == NULL) { - pr_err("calloc failed\n"); - break; - } - dv->devname = malloc(40); - if (dv->devname == NULL) { - pr_err("malloc failed\n"); - free(dv); - break; - } + dv = xcalloc(1, sizeof(*dv)); + dv->devname = xmalloc(40); sprintf(dv->devname, "/dev/loop%d", i); dv->next = devlist; devlist = dv; @@ -5631,19 +5524,8 @@ get_devices(const char *hba_path) } - dv = calloc(1, sizeof(*dv)); - if (dv == NULL) { - pr_err("malloc failed\n"); - err = 1; - break; - } - dv->devname = strdup(buf); - if (dv->devname == NULL) { - pr_err("malloc failed\n"); - err = 1; - free(dv); - break; - } + dv = xcalloc(1, sizeof(*dv)); + dv->devname = xstrdup(buf); dv->next = devlist; devlist = dv; } @@ -6369,10 +6251,8 @@ static int kill_subarray_imsm(struct supertype *st) } if (st->update_tail) { - struct imsm_update_kill_array *u = malloc(sizeof(*u)); + struct imsm_update_kill_array *u = xmalloc(sizeof(*u)); - if (!u) - return 2; u->type = update_kill_array; u->dev_idx = current_vol; append_metadata_update(st, u, sizeof(*u)); @@ -6431,10 +6311,8 @@ static int update_subarray_imsm(struct supertype *st, char *subarray, return 2; if (st->update_tail) { - struct imsm_update_rename_array *u = malloc(sizeof(*u)); + struct imsm_update_rename_array *u = xmalloc(sizeof(*u)); - if (!u) - return 2; u->type = update_rename_array; u->dev_idx = vol; snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name); @@ -6619,12 +6497,7 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra * OROM/EFI */ - this = malloc(sizeof(*this)); - if (!this) { - pr_err("failed to allocate %zu bytes\n", - sizeof(*this)); - break; - } + this = xmalloc(sizeof(*this)); super->current_vol = i; getinfo_super_imsm_volume(st, this, NULL); @@ -6688,21 +6561,7 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra if (skip) continue; - info_d = calloc(1, sizeof(*info_d)); - if (!info_d) { - pr_err("failed to allocate disk" - " for volume %.16s\n", dev->volume); - info_d = this->devs; - while (info_d) { - struct mdinfo *d = info_d->next; - - free(info_d); - info_d = d; - } - free(this); - this = rest; - break; - } + info_d = xcalloc(1, sizeof(*info_d)); info_d->next = this->devs; this->devs = info_d; @@ -7699,10 +7558,7 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a, continue; /* found a usable disk with enough space */ - di = malloc(sizeof(*di)); - if (!di) - continue; - memset(di, 0, sizeof(*di)); + di = xcalloc(1, sizeof(*di)); /* dl->index will be -1 in the case we are activating a * pristine spare. imsm_process_update() will create a @@ -7740,24 +7596,9 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a, * Create a metadata_update record to update the * disk_ord_tbl for the array */ - mu = malloc(sizeof(*mu)); - if (mu) { - mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares); - if (mu->buf == NULL) { - free(mu); - mu = NULL; - } - } - if (!mu) { - while (rv) { - struct mdinfo *n = rv->next; - - free(rv); - rv = n; - } - return NULL; - } - + mu = xmalloc(sizeof(*mu)); + mu->buf = xcalloc(num_spares, + sizeof(struct imsm_update_activate_spare)); mu->space = NULL; mu->space_list = NULL; mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; @@ -8687,15 +8528,10 @@ static void imsm_prepare_update(struct supertype *st, int num_members = map->num_members; void *space; int size, i; - int err = 0; /* allocate memory for added disks */ for (i = 0; i < num_members; i++) { size = sizeof(struct dl); - space = malloc(size); - if (!space) { - err++; - break; - } + space = xmalloc(size); *tail = space; tail = space; *tail = NULL; @@ -8703,24 +8539,11 @@ static void imsm_prepare_update(struct supertype *st, /* allocate memory for new device */ size = sizeof_imsm_dev(super->devlist->dev, 0) + (num_members * sizeof(__u32)); - space = malloc(size); - if (!space) - err++; - else { - *tail = space; - tail = space; - *tail = NULL; - } - if (!err) { - len = disks_to_mpb_size(num_members * 2); - } else { - /* if allocation didn't success, free buffer */ - while (update->space_list) { - void **sp = update->space_list; - update->space_list = *sp; - free(sp); - } - } + space = xmalloc(size); + *tail = space; + tail = space; + *tail = NULL; + len = disks_to_mpb_size(num_members * 2); } break; @@ -8746,9 +8569,7 @@ static void imsm_prepare_update(struct supertype *st, if (u->new_raid_disks > u->old_raid_disks) size += sizeof(__u32)*2* (u->new_raid_disks - u->old_raid_disks); - s = malloc(size); - if (!s) - break; + s = xmalloc(size); *space_tail = s; space_tail = s; *space_tail = NULL; @@ -8782,9 +8603,7 @@ static void imsm_prepare_update(struct supertype *st, if (u->new_raid_disks > u->old_raid_disks) size += sizeof(__u32)*2* (u->new_raid_disks - u->old_raid_disks); - s = malloc(size); - if (!s) - break; + s = xmalloc(size); *space_tail = s; space_tail = s; *space_tail = NULL; @@ -8797,12 +8616,7 @@ static void imsm_prepare_update(struct supertype *st, /* add space for disk in update */ size = sizeof(struct dl); - s = malloc(size); - if (!s) { - free(update->space_list); - update->space_list = NULL; - break; - } + s = xmalloc(size); *space_tail = s; space_tail = s; *space_tail = NULL; @@ -8861,16 +8675,9 @@ static void imsm_prepare_update(struct supertype *st, inf = get_disk_info(u); len = sizeof_imsm_dev(dev, 1); /* allocate a new super->devlist entry */ - dv = malloc(sizeof(*dv)); - if (dv) { - dv->dev = malloc(len); - if (dv->dev) - update->space = dv; - else { - free(dv); - update->space = NULL; - } - } + dv = xmalloc(sizeof(*dv)); + dv->dev = xmalloc(len); + update->space = dv; /* count how many spares will be converted to members */ for (i = 0; i < map->num_members; i++) { @@ -9202,16 +9009,12 @@ int save_backup_imsm(struct supertype *st, unsigned long long start; int data_disks = imsm_num_data_members(dev, MAP_0); - targets = malloc(new_disks * sizeof(int)); - if (!targets) - goto abort; + targets = xmalloc(new_disks * sizeof(int)); for (i = 0; i < new_disks; i++) targets[i] = -1; - target_offsets = malloc(new_disks * sizeof(unsigned long long)); - if (!target_offsets) - goto abort; + target_offsets = xcalloc(new_disks, sizeof(unsigned long long)); start = info->reshape_progress * 512; for (i = 0; i < new_disks; i++) { @@ -9373,9 +9176,7 @@ int recover_backup_imsm(struct supertype *st, struct mdinfo *info) unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512; if (posix_memalign((void **)&buf, 512, unit_len) != 0) goto abort; - targets = malloc(new_disks * sizeof(int)); - if (!targets) - goto abort; + targets = xcalloc(new_disks, sizeof(int)); if (open_backup_targets(info, new_disks, targets, super, id->dev)) { pr_err("Cannot open some devices belonging to array.\n"); @@ -9647,12 +9448,7 @@ static int imsm_create_metadata_update_for_reshape( /* now add space for spare disks that we need to add. */ update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1); - u = calloc(1, update_memory_size); - if (u == NULL) { - dprintf("error: " - "cannot get memory for imsm_update_reshape update\n"); - return 0; - } + u = xcalloc(1, update_memory_size); u->type = update_reshape_container_disks; u->old_raid_disks = old_raid_disks; u->new_raid_disks = geo->raid_disks; @@ -9727,12 +9523,7 @@ static int imsm_create_metadata_update_for_size_change( /* size of all update data without anchor */ update_memory_size = sizeof(struct imsm_update_size_change); - u = calloc(1, update_memory_size); - if (u == NULL) { - dprintf("error: cannot get memory for " - "imsm_create_metadata_update_for_size_change\n"); - return 0; - } + u = xcalloc(1, update_memory_size); u->type = update_size_change; u->subdev = super->current_vol; u->new_size = geo->size; @@ -9765,12 +9556,7 @@ static int imsm_create_metadata_update_for_migration( /* size of all update data without anchor */ update_memory_size = sizeof(struct imsm_update_reshape_migration); - u = calloc(1, update_memory_size); - if (u == NULL) { - dprintf("error: cannot get memory for " - "imsm_create_metadata_update_for_migration\n"); - return 0; - } + u = xcalloc(1, update_memory_size); u->type = update_reshape_migration; u->subdev = super->current_vol; u->new_level = geo->level; @@ -10069,9 +9855,7 @@ int imsm_takeover(struct supertype *st, struct geo_params *geo) struct intel_super *super = st->sb; struct imsm_update_takeover *u; - u = malloc(sizeof(struct imsm_update_takeover)); - if (u == NULL) - return 1; + u = xmalloc(sizeof(struct imsm_update_takeover)); u->type = update_takeover; u->subarray = super->current_vol; diff --git a/super-mbr.c b/super-mbr.c index 3b0cc8d..bbbc65b 100644 --- a/super-mbr.c +++ b/super-mbr.c @@ -174,9 +174,7 @@ static struct supertype *match_metadata_desc(char *arg) if (strcmp(arg, "mbr") != 0) return NULL; - st = malloc(sizeof(*st)); - if (!st) - return st; + st = xmalloc(sizeof(*st)); st->ss = &mbr; st->info = NULL; st->minor_version = 0; diff --git a/super0.c b/super0.c index c8cbb9d..55e71e8 100644 --- a/super0.c +++ b/super0.c @@ -418,7 +418,7 @@ static struct mdinfo *container_content0(struct supertype *st, char *subarray) if (subarray) return NULL; - info = malloc(sizeof(*info)); + info = xmalloc(sizeof(*info)); getinfo_super0(st, info, NULL); return info; } @@ -710,7 +710,7 @@ static int add_to_super0(struct supertype *st, mdu_disk_info_t *dinfo, dip = (struct devinfo **)&st->info; while (*dip) dip = &(*dip)->next; - di = malloc(sizeof(struct devinfo)); + di = xmalloc(sizeof(struct devinfo)); di->fd = fd; di->devname = devname; di->disk = *dinfo; @@ -942,9 +942,7 @@ static int load_super0(struct supertype *st, int fd, char *devname) static struct supertype *match_metadata_desc0(char *arg) { - struct supertype *st = calloc(1, sizeof(*st)); - if (!st) - return st; + struct supertype *st = xcalloc(1, sizeof(*st)); st->container_dev = NoMdDev; st->ss = &super0; diff --git a/super1.c b/super1.c index 5c6530e..ff7c967 100644 --- a/super1.c +++ b/super1.c @@ -695,7 +695,7 @@ static struct mdinfo *container_content1(struct supertype *st, char *subarray) if (subarray) return NULL; - info = malloc(sizeof(*info)); + info = xmalloc(sizeof(*info)); getinfo_super1(st, info, NULL); return info; } @@ -978,7 +978,7 @@ static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk, dip = (struct devinfo **)&st->info; while (*dip) dip = &(*dip)->next; - di = malloc(sizeof(struct devinfo)); + di = xmalloc(sizeof(struct devinfo)); di->fd = fd; di->devname = devname; di->disk = *dk; @@ -1454,9 +1454,7 @@ static int load_super1(struct supertype *st, int fd, char *devname) static struct supertype *match_metadata_desc1(char *arg) { - struct supertype *st = calloc(1, sizeof(*st)); - if (!st) - return st; + struct supertype *st = xcalloc(1, sizeof(*st)); st->container_dev = NoMdDev; st->ss = &super1; diff --git a/sysfs.c b/sysfs.c index 036a4ff..0043d27 100644 --- a/sysfs.c +++ b/sysfs.c @@ -105,10 +105,7 @@ struct mdinfo *sysfs_read(int fd, int devnum, unsigned long options) DIR *dir = NULL; struct dirent *de; - sra = malloc(sizeof(*sra)); - if (sra == NULL) - return sra; - memset(sra, 0, sizeof(*sra)); + sra = xcalloc(1, sizeof(*sra)); sysfs_init(sra, fd, devnum); if (sra->sys_name[0] == 0) { free(sra); @@ -250,9 +247,7 @@ struct mdinfo *sysfs_read(int fd, int devnum, unsigned long options) dbase = base + strlen(base); *dbase++ = '/'; - dev = malloc(sizeof(*dev)); - if (!dev) - goto abort; + dev = xmalloc(sizeof(*dev)); /* Always get slot, major, minor */ strcpy(dbase, "slot"); diff --git a/util.c b/util.c index d939e06..d0765a6 100644 --- a/util.c +++ b/util.c @@ -244,7 +244,7 @@ int parse_layout_faulty(char *layout) { /* Parse the layout string for 'faulty' */ int ln = strcspn(layout, "0123456789"); - char *m = strdup(layout); + char *m = xstrdup(layout); int mode; m[ln] = 0; mode = map_name(faultylayout, m); @@ -377,7 +377,7 @@ int enough_fd(int fd) if (ioctl(fd, GET_ARRAY_INFO, &array) != 0 || array.raid_disks <= 0) return 0; - avail = calloc(array.raid_disks, 1); + avail = xcalloc(array.raid_disks, 1); for (i=0; i < MAX_DISKS && array.nr_disks > 0; i++) { disk.number = i; if (ioctl(fd, GET_DISK_INFO, &disk) != 0) @@ -971,7 +971,7 @@ struct supertype *super_by_fd(int fd, char **subarrayp) subarray = strchr(dev, '/'); if (subarray) { *subarray++ = '\0'; - subarray = strdup(subarray); + subarray = xstrdup(subarray); } container = devname2devnum(dev); if (sra) @@ -1024,10 +1024,7 @@ struct supertype *dup_super(struct supertype *orig) if (!orig) return orig; - st = malloc(sizeof(*st)); - if (!st) - return st; - memset(st, 0, sizeof(*st)); + st = xcalloc(1, sizeof(*st)); st->ss = orig->ss; st->max_devs = orig->max_devs; st->minor_version = orig->minor_version; @@ -1047,8 +1044,7 @@ struct supertype *guess_super_type(int fd, enum guess_types guess_type) int bestsuper = -1; int i; - st = malloc(sizeof(*st)); - memset(st, 0, sizeof(*st)); + st = xcalloc(1, sizeof(*st)); st->container_dev = NoMdDev; for (i=0 ; superlist[i]; i++) { @@ -1488,7 +1484,7 @@ int add_disk(int mdfd, struct supertype *st, if (sd2 == info) break; if (sd2 == NULL) { - sd2 = malloc(sizeof(*sd2)); + sd2 = xmalloc(sizeof(*sd2)); *sd2 = *info; sd2->next = sra->devs; sra->devs = sd2; @@ -1704,7 +1700,7 @@ int flush_metadata_updates(struct supertype *st) void append_metadata_update(struct supertype *st, void *buf, int len) { - struct metadata_update *mu = malloc(sizeof(*mu)); + struct metadata_update *mu = xmalloc(sizeof(*mu)); mu->buf = buf; mu->len = len; @@ -1792,3 +1788,43 @@ struct mdinfo *container_choose_spares(struct supertype *st, } return disks; } + +void *xmalloc(size_t len) +{ + void *rv = malloc(len); + char *msg; + if (rv) + return rv; + msg = Name ": memory allocation failure - aborting\n"; + exit(4+!!write(2, msg, strlen(msg))); +} + +void *xrealloc(void *ptr, size_t len) +{ + void *rv = realloc(ptr, len); + char *msg; + if (rv) + return rv; + msg = Name ": memory allocation failure - aborting\n"; + exit(4+!!write(2, msg, strlen(msg))); +} + +void *xcalloc(size_t num, size_t size) +{ + void *rv = calloc(num, size); + char *msg; + if (rv) + return rv; + msg = Name ": memory allocation failure - aborting\n"; + exit(4+!!write(2, msg, strlen(msg))); +} + +char *xstrdup(const char *str) +{ + char *rv = strdup(str); + char *msg; + if (rv) + return rv; + msg = Name ": memory allocation failure - aborting\n"; + exit(4+!!write(2, msg, strlen(msg))); +}