linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rtla/utils: Fix the potential memory allocation failure
@ 2022-06-14  9:38 jianchunfu
  2022-06-14 12:44 ` Daniel Bristot de Oliveira
  0 siblings, 1 reply; 2+ messages in thread
From: jianchunfu @ 2022-06-14  9:38 UTC (permalink / raw)
  To: bristot, rostedt; +Cc: linux-trace-devel, jianchunfu

Add memory allocating check of mon_cpus before used in utils.

Signed-off-by: jianchunfu <jianchunfu@cmss.chinamobile.com>
---
 tools/tracing/rtla/src/utils.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/tracing/rtla/src/utils.c b/tools/tracing/rtla/src/utils.c
index ffaf8ec84..2a86440b3 100644
--- a/tools/tracing/rtla/src/utils.c
+++ b/tools/tracing/rtla/src/utils.c
@@ -106,6 +106,10 @@ int parse_cpu_list(char *cpu_list, char **monitored_cpus)
 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
 
 	mon_cpus = malloc(nr_cpus * sizeof(char));
+	if (!mon_cpus) {
+		perror("Failed to allocate memory");
+		return -errno;
+	}
 	memset(mon_cpus, 0, (nr_cpus * sizeof(char)));
 
 	for (p = cpu_list; *p; ) {
-- 
2.18.4




^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] rtla/utils: Fix the potential memory allocation failure
  2022-06-14  9:38 [PATCH] rtla/utils: Fix the potential memory allocation failure jianchunfu
@ 2022-06-14 12:44 ` Daniel Bristot de Oliveira
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Bristot de Oliveira @ 2022-06-14 12:44 UTC (permalink / raw)
  To: jianchunfu; +Cc: linux-trace-devel, rostedt

Hi

On 6/14/22 11:38, jianchunfu wrote:
> Add memory allocating check of mon_cpus before used in utils.
> 
Yeah, you found a problem, thanks for you patch! But it needs improvement.

Please, add the Fixes: tag.
Please Cc: linux-kernel

> Signed-off-by: jianchunfu <jianchunfu@cmss.chinamobile.com>
> ---
>  tools/tracing/rtla/src/utils.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/tools/tracing/rtla/src/utils.c b/tools/tracing/rtla/src/utils.c
> index ffaf8ec84..2a86440b3 100644
> --- a/tools/tracing/rtla/src/utils.c
> +++ b/tools/tracing/rtla/src/utils.c
> @@ -106,6 +106,10 @@ int parse_cpu_list(char *cpu_list, char **monitored_cpus)
>  	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
>  
>  	mon_cpus = malloc(nr_cpus * sizeof(char));
> +	if (!mon_cpus) {
> +		perror("Failed to allocate memory");
> +		return -errno;
> +	}
>  	memset(mon_cpus, 0, (nr_cpus * sizeof(char)));

Your patch is fixing the problem, but it can be improved by:
	- using calloc (as in other .c)
	- removing the memset
	- instead of returning, goto err;

Do you mind sending a v2?

-- Daniel

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-14 12:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-14  9:38 [PATCH] rtla/utils: Fix the potential memory allocation failure jianchunfu
2022-06-14 12:44 ` Daniel Bristot de Oliveira

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).