From: Andrew Morton <akpm@linux-foundation.org>
To: Zhu Jun <zhujun2@cmss.chinamobile.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] tools/mm: Use calloc and check the memory allocation failure
Date: Fri, 30 Aug 2024 17:59:02 -0700 [thread overview]
Message-ID: <20240830175902.2cbdb98ed746001896f57fe5@linux-foundation.org> (raw)
In-Reply-To: <20240829092144.5851-1-zhujun2@cmss.chinamobile.com>
On Thu, 29 Aug 2024 02:21:44 -0700 Zhu Jun <zhujun2@cmss.chinamobile.com> wrote:
> Replace malloc with calloc and add null pointer check
> in case of allocation failure.
>
> ...
>
> --- a/tools/mm/page_owner_sort.c
> +++ b/tools/mm/page_owner_sort.c
> @@ -368,9 +368,10 @@ static __u64 get_ts_nsec(char *buf)
>
> static char *get_comm(char *buf)
> {
> - char *comm_str = malloc(TASK_COMM_LEN);
> + char *comm_str = calloc(TASK_COMM_LEN, sizeof(char));
>
> - memset(comm_str, 0, TASK_COMM_LEN);
> + if (!comm_str)
> + return NULL;
It seems rather pointless doing this when the caller aren't coded to
handle the NULL return.
And really, for these little userspace tools it's OK for us to just
assume that malloc() alway succeeds, isn't it?
prev parent reply other threads:[~2024-08-31 0:59 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-29 9:21 Zhu Jun
2024-08-31 0:59 ` Andrew Morton [this message]
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=20240830175902.2cbdb98ed746001896f57fe5@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=zhujun2@cmss.chinamobile.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