aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZhiqiang Liu <lzhq28@mail.ustc.edu.cn>2021-02-27 10:35:46 +0800
committerColy Li <colyli@suse.de>2021-07-26 15:57:39 +0800
commita738909c244330ef413dc1739eb6b01549c1f346 (patch)
treeeee2b1c32e322b6bf94cc1515dd14e3f12590867
parentc23728288ac233b5d8e1c20731270eba87f1d769 (diff)
downloadbcache-tools-a738909c244330ef413dc1739eb6b01549c1f346.tar.gz
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>
-rw-r--r--bcache.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/bcache.c b/bcache.c
index 55589140..18dfa42e 100644
--- a/bcache.c
+++ b/bcache.c
@@ -173,7 +173,7 @@ void replace_line(char **dest, const char *from, const char *to)
int tree(void)
{
- char *out = (char *)malloc(4096);
+ char *out;
const char *begin = ".\n";
const char *middle = "├─";
const char *tail = "└─";
@@ -183,8 +183,15 @@ int tree(void)
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;
}