Re: [PATCH v2] tools/mm: Use calloc and check the memory allocation failure

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 29 Aug 2024 02:21:44 -0700 Zhu Jun <zhujun2@xxxxxxxxxxxxxxxxxxxx> 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?





[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux