From: Coly Li <colyli@suse.de>
To: Zhiqiang Liu <liuzhiqiang26@huawei.com>, linux-bcache@vger.kernel.org
Cc: linfeilong <linfeilong@huawei.com>,
lixiaokeng <lixiaokeng@huawei.com>,
"lijinlin (A)" <lijinlin3@huawei.com>
Subject: Re: [PATCH] bcache-tools: check whether allocating memory fails in tree()
Date: Tue, 2 Mar 2021 21:33:55 +0800 [thread overview]
Message-ID: <66442e3c-7939-e4af-1924-2f742529383e@suse.de> (raw)
In-Reply-To: <655607db-abc7-3241-cf14-e6efbec8331a@huawei.com>
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;
> }
>
next prev parent reply other threads:[~2021-03-03 0:35 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-27 2:35 [PATCH] bcache-tools: check whether allocating memory fails in tree() Zhiqiang Liu
2021-03-02 13:33 ` Coly Li [this message]
2021-03-03 0:50 ` Zhiqiang Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=66442e3c-7939-e4af-1924-2f742529383e@suse.de \
--to=colyli@suse.de \
--cc=lijinlin3@huawei.com \
--cc=linfeilong@huawei.com \
--cc=linux-bcache@vger.kernel.org \
--cc=liuzhiqiang26@huawei.com \
--cc=lixiaokeng@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).