* [PATCH v2] tools/mm: Use calloc and check the memory allocation failure
@ 2024-08-29 9:21 Zhu Jun
2024-08-31 0:59 ` Andrew Morton
0 siblings, 1 reply; 2+ messages in thread
From: Zhu Jun @ 2024-08-29 9:21 UTC (permalink / raw)
To: akpm; +Cc: linux-mm, linux-kernel, zhujun2
Replace malloc with calloc and add null pointer check
in case of allocation failure.
Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>
---
v1->v2:
modify commit message and delete fprintf line
tools/mm/page_owner_sort.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/tools/mm/page_owner_sort.c b/tools/mm/page_owner_sort.c
index e1f264444342..8c78265cef67 100644
--- 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;
search_pattern(&comm_pattern, comm_str, buf);
errno = 0;
--
2.17.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] tools/mm: Use calloc and check the memory allocation failure
2024-08-29 9:21 [PATCH v2] tools/mm: Use calloc and check the memory allocation failure Zhu Jun
@ 2024-08-31 0:59 ` Andrew Morton
0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2024-08-31 0:59 UTC (permalink / raw)
To: Zhu Jun; +Cc: linux-mm, linux-kernel
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?
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-08-31 0:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-29 9:21 [PATCH v2] tools/mm: Use calloc and check the memory allocation failure Zhu Jun
2024-08-31 0:59 ` Andrew Morton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox