[CRIU] [PATCH 6/9] criu: use xmalloc()

Kir Kolyshkin kir at openvz.org
Mon Nov 7 13:37:50 PST 2016


1. Make sure to use xmalloc() where an error message makes sense
2. Make sure to not ignore if NULL is returned

Signed-off-by: Kir Kolyshkin <kir at openvz.org>
---
 criu/autofs.c |  3 +--
 criu/uffd.c   | 16 +++++++---------
 2 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/criu/autofs.c b/criu/autofs.c
index 6adc378..151980d 100644
--- a/criu/autofs.c
+++ b/criu/autofs.c
@@ -290,9 +290,8 @@ static int autofs_revisit_options(struct mount_info *pm)
 	char *str;
 	int ret = -ENOMEM;
 
-	str = malloc(1024);
+	str = xmalloc(1024);
 	if (!str) {
-		pr_err("failed to allocate\n");
 		return -ENOMEM;
 	}
 
diff --git a/criu/uffd.c b/criu/uffd.c
index e9ba4ae..2dea090 100644
--- a/criu/uffd.c
+++ b/criu/uffd.c
@@ -67,11 +67,9 @@ static struct lazy_pages_info *lpi_init(void)
 {
 	struct lazy_pages_info *lpi = NULL;
 
-	lpi = malloc(sizeof(*lpi));
-	if (!lpi) {
-		pr_err("allocation failed\n");
+	lpi = xmalloc(sizeof(*lpi));
+	if (!lpi)
 		return NULL;
-	}
 
 	memset(lpi, 0, sizeof(*lpi));
 	INIT_LIST_HEAD(&lpi->pages);
@@ -751,7 +749,9 @@ static int handle_requests(int epollfd, struct epoll_event *events)
 
 	/* All operations will be done on page size */
 	ps = page_size();
-	dest = malloc(ps);
+	dest = xmalloc(ps);
+	if (!dest)
+		return ret;
 
 	while (1) {
 		/*
@@ -820,11 +820,9 @@ static int prepare_epoll(int nr_fds, struct epoll_event **events)
 {
 	int epollfd;
 
-	*events = malloc(sizeof(struct epoll_event) * nr_fds);
-	if (!*events) {
-		pr_err("memory allocation failed\n");
+	*events = xmalloc(sizeof(struct epoll_event) * nr_fds);
+	if (!*events)
 		return -1;
-	}
 
 	epollfd = epoll_create(nr_fds);
 	if (epollfd == -1) {
-- 
2.7.4



More information about the CRIU mailing list