bcache-tools: check whether allocating memory fails in tree()

In tree(), we do not check whether malloc() returns NULL,
it may cause potential Null pointer dereference problem.
In addition, when we fail to list devices, we should free(out)
before return.

Signed-off-by: Zhiqiang Liu <lzhq28@mail.ustc.edu.cn>
Signed-off-by: Coly Li <colyli@suse.de>
diff --git a/bcache.c b/bcache.c
index 5558914..18dfa42 100644
--- a/bcache.c
+++ b/bcache.c
@@ -173,7 +173,7 @@
 
 int tree(void)
 {
-	char *out = (char *)malloc(4096);
+	char *out;
 	const char *begin = ".\n";
 	const char *middle = "├─";
 	const char *tail = "└─";
@@ -183,8 +183,15 @@
 	INIT_LIST_HEAD(&head);
 	int ret;
 
+	out = (char *)malloc(4096);
+	if (out == NULL) {
+		fprintf(stderr, "Error: fail to allocate memory buffer\n");
+		return 1;
+	}
+
 	ret = list_bdevs(&head);
 	if (ret != 0) {
+		free(out);
 		fprintf(stderr, "Failed to list devices\n");
 		return ret;
 	}