linux-bcache.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bcache-tools: check whether allocating memory fails in tree()
@ 2021-02-27  2:35 Zhiqiang Liu
  2021-03-02 13:33 ` Coly Li
  0 siblings, 1 reply; 3+ messages in thread
From: Zhiqiang Liu @ 2021-02-27  2:35 UTC (permalink / raw)
  To: linux-bcache, Coly Li; +Cc: linfeilong, lixiaokeng, lijinlin (A)


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: ZhiqiangLiu <lzhq28@mail.ustc.edu.cn>
---
 bcache.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/bcache.c b/bcache.c
index 044d401..1c4cef9 100644
--- a/bcache.c
+++ b/bcache.c
@@ -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.30.0



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] bcache-tools: check whether allocating memory fails in tree()
  2021-02-27  2:35 [PATCH] bcache-tools: check whether allocating memory fails in tree() Zhiqiang Liu
@ 2021-03-02 13:33 ` Coly Li
  2021-03-03  0:50   ` Zhiqiang Liu
  0 siblings, 1 reply; 3+ messages in thread
From: Coly Li @ 2021-03-02 13:33 UTC (permalink / raw)
  To: Zhiqiang Liu, linux-bcache; +Cc: linfeilong, lixiaokeng, lijinlin (A)

On 2/27/21 10:35 AM, Zhiqiang Liu wrote:
> 
> 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: ZhiqiangLiu <lzhq28@mail.ustc.edu.cn>

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


Thanks.

Coly Li


> ---
>  bcache.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/bcache.c b/bcache.c
> index 044d401..1c4cef9 100644
> --- a/bcache.c
> +++ b/bcache.c
> @@ -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;
>  	}
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] bcache-tools: check whether allocating memory fails in tree()
  2021-03-02 13:33 ` Coly Li
@ 2021-03-03  0:50   ` Zhiqiang Liu
  0 siblings, 0 replies; 3+ messages in thread
From: Zhiqiang Liu @ 2021-03-03  0:50 UTC (permalink / raw)
  To: Coly Li, linux-bcache; +Cc: linfeilong, lixiaokeng, lijinlin (A)

Thank you for doing that.

Zhiqiang Liu

On 2021/3/2 21:33, Coly Li wrote:
> On 2/27/21 10:35 AM, Zhiqiang Liu wrote:
>>
>> 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: ZhiqiangLiu <lzhq28@mail.ustc.edu.cn>
> 
> Applied. BTW, I change your above name string from ZhiqiangLiu to
> Zhiqiang Liu, because of the checkpatch.pl warning.
> 
> 
> Thanks.
> 
> Coly Li
> 
> 
>> ---
>>  bcache.c | 9 ++++++++-
>>  1 file changed, 8 insertions(+), 1 deletion(-)
>>
>> diff --git a/bcache.c b/bcache.c
>> index 044d401..1c4cef9 100644
>> --- a/bcache.c
>> +++ b/bcache.c
>> @@ -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;
>>  	}
>>
> 
> 
> .
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-03-03 15:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-27  2:35 [PATCH] bcache-tools: check whether allocating memory fails in tree() Zhiqiang Liu
2021-03-02 13:33 ` Coly Li
2021-03-03  0:50   ` Zhiqiang Liu

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).