]> www.infradead.org Git - mtd-utils.git/commitdiff
libmtd: make malloc failures fatal
authorMike Frysinger <vapier@gentoo.org>
Fri, 1 Oct 2010 17:13:10 +0000 (13:13 -0400)
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Sat, 2 Oct 2010 14:19:44 +0000 (17:19 +0300)
This converts libmtd to the common xalloc helpers and in doing so, makes
memory allocation failures fatal rather than returning an error to the
caller.  I think this is acceptable behavior.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
lib/libmtd.c
lib/libmtd_legacy.c

index 83ae81252acbe6e2d38af5de2efe643913323260..e0c093468e2d263e80dc8728fcb55e1d66d3bc71 100644 (file)
 static char *mkpath(const char *path, const char *name)
 {
        char *n;
-       int len1 = strlen(path);
-       int len2 = strlen(name);
+       size_t len1 = strlen(path);
+       size_t len2 = strlen(name);
 
-       n = malloc(len1 + len2 + 2);
-       if (!n) {
-               sys_errmsg("cannot allocate %d bytes", len1 + len2 + 2);
-               return NULL;
-       }
+       n = xmalloc(len1 + len2 + 2);
 
        memcpy(n, path, len1);
        if (n[len1 - 1] != '/')
@@ -556,9 +552,7 @@ libmtd_t libmtd_open(void)
 {
        struct libmtd *lib;
 
-       lib = calloc(1, sizeof(struct libmtd));
-       if (!lib)
-               return NULL;
+       lib = xzalloc(sizeof(*lib));
 
        lib->offs64_ioctls = OFFS64_IOCTLS_UNKNOWN;
 
@@ -917,11 +911,7 @@ int mtd_torture(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb)
        normsg("run torture test for PEB %d", eb);
        patt_count = ARRAY_SIZE(patterns);
 
-       buf = malloc(mtd->eb_size);
-       if (!buf) {
-               errmsg("cannot allocate %d bytes of memory", mtd->eb_size);
-               return -1;
-       }
+       buf = xmalloc(mtd->eb_size);
 
        for (i = 0; i < patt_count; i++) {
                err = mtd_erase(desc, mtd, fd, eb);
@@ -1240,11 +1230,7 @@ int mtd_write_img(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
                goto out_close;
        }
 
-       buf = malloc(mtd->eb_size);
-       if (!buf) {
-               sys_errmsg("cannot allocate %d bytes of memory", mtd->eb_size);
-               goto out_close;
-       }
+       buf = xmalloc(mtd->eb_size);
 
        while (written < len) {
                int rd = 0;
index 3d129c1787b83b641f0ebfd222fceb9d6f202e69..7488275fea457d59379bb38d8ab08ed76d979163 100644 (file)
@@ -75,12 +75,7 @@ static int proc_parse_start(struct proc_parse_info *pi)
        if (fd == -1)
                return -1;
 
-       pi->buf = malloc(PROC_MTD_MAX_LEN);
-       if (!pi->buf) {
-               sys_errmsg("cannot allocate %d bytes of memory",
-                          PROC_MTD_MAX_LEN);
-               goto out_close;
-       }
+       pi->buf = xmalloc(PROC_MTD_MAX_LEN);
 
        ret = read(fd, pi->buf, PROC_MTD_MAX_LEN);
        if (ret == -1) {
@@ -103,7 +98,6 @@ static int proc_parse_start(struct proc_parse_info *pi)
 
 out_free:
        free(pi->buf);
-out_close:
        close(fd);
        return -1;
 }