* [PATCH] tools/mm: Use calloc and check the potential memory allocation failure
@ 2024-08-29 5:56 Zhu Jun
2024-08-29 6:25 ` Markus Elfring
0 siblings, 1 reply; 5+ messages in thread
From: Zhu Jun @ 2024-08-29 5:56 UTC (permalink / raw)
To: akpm; +Cc: linux-mm, linux-kernel, zhujun2
Replace malloc with calloc and add memory allocating check
of comm_str before used.
Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>
---
tools/mm/page_owner_sort.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/tools/mm/page_owner_sort.c b/tools/mm/page_owner_sort.c
index e1f264444342..4e2329831810 100644
--- a/tools/mm/page_owner_sort.c
+++ b/tools/mm/page_owner_sort.c
@@ -368,9 +368,12 @@ 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) {
+ fprintf(stderr, "Out of memory\n");
+ return NULL;
+ }
search_pattern(&comm_pattern, comm_str, buf);
errno = 0;
--
2.17.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] tools/mm: Use calloc and check the potential memory allocation failure
2024-08-29 5:56 [PATCH] tools/mm: Use calloc and check the potential memory allocation failure Zhu Jun
@ 2024-08-29 6:25 ` Markus Elfring
2024-08-29 7:39 ` Dev Jain
0 siblings, 1 reply; 5+ messages in thread
From: Markus Elfring @ 2024-08-29 6:25 UTC (permalink / raw)
To: Zhu Jun, linux-mm, kernel-janitors, Andrew Morton; +Cc: LKML
> Replace malloc with calloc and add memory allocating check
memset(…, 0, …) call by calloc()?
> of comm_str before used.
* Add also a null pointer check for the detection of a memory allocation failure.
* Would you like to improve such a change description another bit
(with tags like “Fixes” and “Cc”)?
* How do you think about to omit the statement “fprintf(stderr, "Out of memory\n");”?
* I suggest to omit the word “potential” from the summary phrase.
Regards,
Markus
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] tools/mm: Use calloc and check the potential memory allocation failure
2024-08-29 6:25 ` Markus Elfring
@ 2024-08-29 7:39 ` Dev Jain
2024-08-29 7:55 ` Markus Elfring
0 siblings, 1 reply; 5+ messages in thread
From: Dev Jain @ 2024-08-29 7:39 UTC (permalink / raw)
To: Markus Elfring, Zhu Jun, linux-mm, kernel-janitors, Andrew Morton; +Cc: LKML
On 8/29/24 11:55, Markus Elfring wrote:
>> Replace malloc with calloc and add memory allocating check
> memset(…, 0, …) call by calloc()?
Calloc returns zeroed-out memory.
>
>
>> of comm_str before used.
> * Add also a null pointer check for the detection of a memory allocation failure.
Which is exactly what Zhu has done?
>
> * Would you like to improve such a change description another bit
> (with tags like “Fixes” and “Cc”)?
>
> * How do you think about to omit the statement “fprintf(stderr, "Out of memory\n");”?
Why?
>
> * I suggest to omit the word “potential” from the summary phrase.
>
>
> Regards,
> Markus
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: tools/mm: Use calloc and check the potential memory allocation failure
2024-08-29 7:39 ` Dev Jain
@ 2024-08-29 7:55 ` Markus Elfring
2024-08-29 8:14 ` Dev Jain
0 siblings, 1 reply; 5+ messages in thread
From: Markus Elfring @ 2024-08-29 7:55 UTC (permalink / raw)
To: Dev Jain, Zhu Jun, linux-mm, kernel-janitors, Andrew Morton; +Cc: LKML
>>> Replace malloc with calloc and add memory allocating check
>> memset(…, 0, …) call by calloc()?
>
> Calloc returns zeroed-out memory.
I propose to improve the change description considerably.
>>> of comm_str before used.
>> * Add also a null pointer check for the detection of a memory allocation failure.
>
> Which is exactly what Zhu has done?
Can the commit message become nicer anyhow?
…
>> * How do you think about to omit the statement “fprintf(stderr, "Out of memory\n");”?
>
> Why?
I imagine that a returned null pointer can eventually be sufficient already.
Would you get helpful background information from the variable “errno”?
Regards,
Markus
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: tools/mm: Use calloc and check the potential memory allocation failure
2024-08-29 7:55 ` Markus Elfring
@ 2024-08-29 8:14 ` Dev Jain
0 siblings, 0 replies; 5+ messages in thread
From: Dev Jain @ 2024-08-29 8:14 UTC (permalink / raw)
To: Markus Elfring, Zhu Jun, linux-mm, kernel-janitors, Andrew Morton; +Cc: LKML
On 8/29/24 13:25, Markus Elfring wrote:
>>>> Replace malloc with calloc and add memory allocating check
>>> memset(…, 0, …) call by calloc()?
>> Calloc returns zeroed-out memory.
> I propose to improve the change description considerably.
>
>
>>>> of comm_str before used.
>>> * Add also a null pointer check for the detection of a memory allocation failure.
>> Which is exactly what Zhu has done?
> Can the commit message become nicer anyhow?
I agree. The commit message should note two things, first that
a malloc followed by a memset to 0 can be reduced to single calloc,
and second that, "add memory allocating check" can be replaced by
"add null pointer check in case of allocation failure".
>
>
> …
>>> * How do you think about to omit the statement “fprintf(stderr, "Out of memory\n");”?
>> Why?
> I imagine that a returned null pointer can eventually be sufficient already.
> Would you get helpful background information from the variable “errno”?
In case of calloc failure, errno is always set to ENOMEM, so we are guaranteed
that any failure is an out of memory failure.
>
> Regards,
> Markus
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-08-29 8:14 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-29 5:56 [PATCH] tools/mm: Use calloc and check the potential memory allocation failure Zhu Jun
2024-08-29 6:25 ` Markus Elfring
2024-08-29 7:39 ` Dev Jain
2024-08-29 7:55 ` Markus Elfring
2024-08-29 8:14 ` Dev Jain
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox