linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Marc Kleine-Budde <mkl@pengutronix.de>
To: linux-mtd@lists.infradead.org
Cc: kernel@pengutronix.de, Sascha Hauer <s.hauer@pengutronix.de>,
	Marc Kleine-Budde <mkl@pengutronix.de>
Subject: [PATCH 2/5] mkfs.ubifs: use xmalloc/xzalloc for allocating memory
Date: Fri, 16 Oct 2015 17:15:16 +0200	[thread overview]
Message-ID: <1445008519-19928-3-git-send-email-mkl@pengutronix.de> (raw)
In-Reply-To: <1445008519-19928-1-git-send-email-mkl@pengutronix.de>

From: Sascha Hauer <s.hauer@pengutronix.de>

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 mkfs.ubifs/mkfs.ubifs.c | 64 +++++++++++--------------------------------------
 1 file changed, 14 insertions(+), 50 deletions(-)

diff --git a/mkfs.ubifs/mkfs.ubifs.c b/mkfs.ubifs/mkfs.ubifs.c
index 1b816ae483e0..049219b52cc5 100644
--- a/mkfs.ubifs/mkfs.ubifs.c
+++ b/mkfs.ubifs/mkfs.ubifs.c
@@ -259,13 +259,8 @@ static int is_contained(const char *file, const char *dir)
 	file_base = dirname(copy);
 
 	/* Turn the paths into the canonical form */
-	real_file = malloc(PATH_MAX);
-	if (!real_file)
-		goto out_free;
-
-	real_dir = malloc(PATH_MAX);
-	if (!real_dir)
-		goto out_free;
+	real_file = xmalloc(PATH_MAX);
+	real_dir = xmalloc(PATH_MAX);
 
 	if (!realpath(file_base, real_file)) {
 		perror("Could not canonicalize file path");
@@ -500,9 +495,7 @@ static int get_options(int argc, char**argv)
 		case 'r':
 		case 'd':
 			root_len = strlen(optarg);
-			root = malloc(root_len + 2);
-			if (!root)
-				return err_msg("cannot allocate memory");
+			root = xmalloc(root_len + 2);
 
 			/*
 			 * The further code expects '/' at the end of the root
@@ -910,9 +903,7 @@ static int add_to_index(union ubifs_key *key, char *name, int lnum, int offs,
 	struct idx_entry *e;
 
 	dbg_msg(3, "LEB %d offs %d len %d", lnum, offs, len);
-	e = malloc(sizeof(struct idx_entry));
-	if (!e)
-		return err_msg("out of memory");
+	e = xmalloc(sizeof(struct idx_entry));
 	e->next = NULL;
 	e->prev = idx_list_last;
 	e->key = *key;
@@ -1193,9 +1184,7 @@ static struct inum_mapping *lookup_inum_mapping(dev_t dev, ino_t inum)
 			return im;
 		im = im->next;
 	}
-	im = malloc(sizeof(struct inum_mapping));
-	if (!im)
-		return NULL;
+	im = xmalloc(sizeof(struct inum_mapping));
 	im->next = hash_table[k];
 	im->prev = NULL;
 	im->dev = dev;
@@ -1355,9 +1344,7 @@ static int add_non_dir(const char *path_name, ino_t *inum, unsigned int nlink,
 			/* New entry */
 			im->use_inum = *inum;
 			im->use_nlink = 1;
-			im->path_name = malloc(strlen(path_name) + 1);
-			if (!im->path_name)
-				return err_msg("out of memory");
+			im->path_name = xmalloc(strlen(path_name) + 1);
 			strcpy(im->path_name, path_name);
 		} else {
 			/* Existing entry */
@@ -1707,21 +1694,14 @@ static int write_index(void)
 	head_flags = LPROPS_INDEX;
 	/* Allocate index node */
 	idx_sz = ubifs_idx_node_sz(c, c->fanout);
-	idx = malloc(idx_sz);
-	if (!idx)
-		return err_msg("out of memory");
+	idx = xmalloc(idx_sz);
 	/* Make an array of pointers to sort the index list */
 	sz = idx_cnt * sizeof(struct idx_entry *);
 	if (sz / sizeof(struct idx_entry *) != idx_cnt) {
 		free(idx);
 		return err_msg("index is too big (%zu entries)", idx_cnt);
 	}
-	idx_ptr = malloc(sz);
-	if (!idx_ptr) {
-		free(idx);
-		return err_msg("out of memory - needed %zu bytes for index",
-			       sz);
-	}
+	idx_ptr = xmalloc(sz);
 	idx_ptr[0] = idx_list_first;
 	for (i = 1; i < idx_cnt; i++)
 		idx_ptr[i] = idx_ptr[i - 1]->next;
@@ -2164,13 +2144,8 @@ static int init(void)
 	c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
 	c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
 
-	c->lpt = malloc(c->main_lebs * sizeof(struct ubifs_lprops));
-	if (!c->lpt)
-		return err_msg("unable to allocate LPT");
-
-	c->ltab = malloc(c->lpt_lebs * sizeof(struct ubifs_lprops));
-	if (!c->ltab)
-		return err_msg("unable to allocate LPT ltab");
+	c->lpt = xmalloc(c->main_lebs * sizeof(struct ubifs_lprops));
+	c->ltab = xmalloc(c->lpt_lebs * sizeof(struct ubifs_lprops));
 
 	/* Initialize LPT's own lprops */
 	for (i = 0; i < c->lpt_lebs; i++) {
@@ -2182,23 +2157,12 @@ static int init(void)
 	c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
 	dbg_msg(1, "dead_wm %d  dark_wm %d", c->dead_wm, c->dark_wm);
 
-	leb_buf = malloc(c->leb_size);
-	if (!leb_buf)
-		return err_msg("out of memory");
-
-	node_buf = malloc(NODE_BUFFER_SIZE);
-	if (!node_buf)
-		return err_msg("out of memory");
-
-	block_buf = malloc(UBIFS_BLOCK_SIZE);
-	if (!block_buf)
-		return err_msg("out of memory");
+	leb_buf = xmalloc(c->leb_size);
+	node_buf = xmalloc(NODE_BUFFER_SIZE);
+	block_buf = xmalloc(UBIFS_BLOCK_SIZE);
 
 	sz = sizeof(struct inum_mapping *) * HASH_TABLE_SIZE;
-	hash_table = malloc(sz);
-	if (!hash_table)
-		return err_msg("out of memory");
-	memset(hash_table, 0, sz);
+	hash_table = xzalloc(sz);
 
 	err = init_compression();
 	if (err)
-- 
2.6.1

  parent reply	other threads:[~2015-10-16 15:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-16 15:15 [PATCH 0/5] mkfs.ubifs: cleanups + extended attribute support Marc Kleine-Budde
2015-10-16 15:15 ` [PATCH 1/5] mkfs.ubifs: change add_directory argument to 'existing' Marc Kleine-Budde
2015-10-16 15:15 ` Marc Kleine-Budde [this message]
2015-10-16 15:15 ` [PATCH 3/5] mkfs.ubifs: simplify make_path with xasprintf Marc Kleine-Budde
2015-10-16 15:15 ` [PATCH 4/5] mkfs.ubifs: Add extended attribute support Marc Kleine-Budde
2015-10-16 15:15 ` [PATCH 5/5] mkfs.ubifs: Optionally create extended attribute with inode number Marc Kleine-Budde
2015-11-11 14:56 ` [PATCH 0/5] mkfs.ubifs: cleanups + extended attribute support Daniel Walter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1445008519-19928-3-git-send-email-mkl@pengutronix.de \
    --to=mkl@pengutronix.de \
    --cc=kernel@pengutronix.de \
    --cc=linux-mtd@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).