diff mbox series

[RESEND] tools/mm: Use calloc and check the memory allocation failure

Message ID 20240912032457.2780-1-zhujun2@cmss.chinamobile.com (mailing list archive)
State New
Headers show
Series [RESEND] tools/mm: Use calloc and check the memory allocation failure | expand

Commit Message

Zhu Jun Sept. 12, 2024, 3:24 a.m. UTC
Replace malloc with calloc and add null pointer check
in case of allocation failure.

Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>
---
 tools/mm/page_owner_sort.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Dev Jain Sept. 12, 2024, 4:28 a.m. UTC | #1
On 9/12/24 08:54, Zhu Jun wrote:
> Replace malloc with calloc and add null pointer check
> in case of allocation failure.
>
> Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>
> ---
>   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;

Hello Zhu,

It is pointless to resend if you haven't addressed the queries
raised by Andrew. He notes that the callers aren't handling a
possible NULL return, so there is no point in even checking for
a mem alloc failure. I haven't wandered in tools/mm but from a
programming PoV what Andrew says is correct...if you are returning
NULL then you also must decide what to do with that in the caller,
and so on, on which I cannot comment :)
diff mbox series

Patch

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;