btrfs-progs: mkfs: handler memory allocation error in make_btrfs
authorDavid Sterba <dsterba@suse.com>
Fri, 6 Nov 2015 17:52:09 +0000 (18:52 +0100)
committerDavid Sterba <dsterba@suse.com>
Mon, 16 Nov 2015 13:23:44 +0000 (14:23 +0100)
Do the allocation early, no need to cleanup.

Signed-off-by: David Sterba <dsterba@suse.com>
utils.c

diff --git a/utils.c b/utils.c
index 9c7cb89..17f4757 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -182,7 +182,7 @@ int test_uuid_unique(char *fs_uuid)
 int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
 {
        struct btrfs_super_block super;
-       struct extent_buffer *buf = NULL;
+       struct extent_buffer *buf;
        struct btrfs_root_item root_item;
        struct btrfs_disk_key disk_key;
        struct btrfs_extent_item *extent_item;
@@ -204,6 +204,10 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
                                 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
        u64 num_bytes;
 
+       buf = malloc(sizeof(*buf) + max(cfg->sectorsize, cfg->nodesize));
+       if (!buf)
+               return -ENOMEM;
+
        first_free = BTRFS_SUPER_INFO_OFFSET + cfg->sectorsize * 2 - 1;
        first_free &= ~((u64)cfg->sectorsize - 1);
 
@@ -249,8 +253,6 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
        if (cfg->label)
                strncpy(super.label, cfg->label, BTRFS_LABEL_SIZE - 1);
 
-       buf = malloc(sizeof(*buf) + max(cfg->sectorsize, cfg->nodesize));
-
        /* create the tree of root objects */
        memset(buf->data, 0, cfg->nodesize);
        buf->len = cfg->nodesize;