Skip to contentSkip to conversation

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

PATCH Applied Zhiqiang Liu 2 participants 3 messages Feb 27, 2021 – Mar 3, 2021 bcache (block layer cache)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 bcache.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
bcache.c +8 -1
index 044d401..1c4cef9 100644
@@ -174,7 +174,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 = "└─";
@@ -184,8 +184,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;
 	}
Signature · 1 line
2.30.0
Applied
#1 lore raw
Quoted 7 lines · Zhiqiang Liu

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.

Applied. BTW, I change your above name string from ZhiqiangLiu to Zhiqiang Liu, because of the checkpatch.pl warning.

Thanks.

Coly Li

Quoted 34 lines
---
 bcache.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
bcache.c +8 -1
index 044d401..1c4cef9 100644
@@ -174,7 +174,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 = "└─";
@@ -184,8 +184,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;
 	}
#2 lore raw

Thank you for doing that.

Zhiqiang Liu

Quoted 56 lines · Coly Li
Quoted 7 lines · Zhiqiang Liu

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.

Applied. BTW, I change your above name string from ZhiqiangLiu to Zhiqiang Liu, because of the checkpatch.pl warning.

Thanks.

Coly Li

Quoted 24 lines
---
 bcache.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
bcache.c +3 -1
index 044d401..1c4cef9 100644
@@ -174,7 +174,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 = "└─";
@@ -184,8 +184,15 @@ int tree(void)
 	INIT_LIST_HEAD(&head);
 	int ret;
 
+	out = (char *)malloc(4096);
+	if (out == NULL) {

bcache.c int tree(void) · line 198

Quoted 10 lines
bcache.c +5 -0
+		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;
 	}

.