diff mbox series

[iproute2,1/3] lib/fs: fix memory leak in get_task_name()

Message ID c7d57346ddc4d9eaaabc0f004911d038c95238af.1643736038.git.aclaudi@redhat.com (mailing list archive)
State Changes Requested
Delegated to: Stephen Hemminger
Headers show
Series some memory leak fixes | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Andrea Claudi Feb. 1, 2022, 5:39 p.m. UTC
asprintf() allocates memory which is not freed on the error path of
get_task_name(), thus potentially leading to memory leaks.

This commit fixes this using free() on error paths.

Fixes: 81bfd01a4c9e ("lib: move get_task_name() from rdma")
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
---
 lib/fs.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Comments

Stephen Hemminger Feb. 1, 2022, 6:27 p.m. UTC | #1
On Tue,  1 Feb 2022 18:39:24 +0100
Andrea Claudi <aclaudi@redhat.com> wrote:

> +	if (fscanf(f, "%ms\n", &comm) != 1) {
> +		free(comm);
> +		return NULL;

This is still leaking the original comm.

Why not change it to use a local variable for the path
(avoid asprintf) and not reuse comm for both pathname
and return value.
Andrea Claudi Feb. 2, 2022, 2:38 p.m. UTC | #2
On Tue, Feb 01, 2022 at 10:27:12AM -0800, Stephen Hemminger wrote:
> On Tue,  1 Feb 2022 18:39:24 +0100
> Andrea Claudi <aclaudi@redhat.com> wrote:
> 
> > +	if (fscanf(f, "%ms\n", &comm) != 1) {
> > +		free(comm);
> > +		return NULL;
> 
> This is still leaking the original comm.
>

Thanks Stephen, I missed the %m over there :(

> Why not change it to use a local variable for the path
> (avoid asprintf) and not reuse comm for both pathname
> and return value.
> 

Thanks for the suggestion. 

What about taking an extra-step and get rid of the %m too?
We can do something similar to the get_command_name() function so that
we don't need to use free in places where we use get_task_name().
Stephen Hemminger Feb. 2, 2022, 3:49 p.m. UTC | #3
On Wed, 2 Feb 2022 15:38:16 +0100
Andrea Claudi <aclaudi@redhat.com> wrote:

> On Tue, Feb 01, 2022 at 10:27:12AM -0800, Stephen Hemminger wrote:
> > On Tue,  1 Feb 2022 18:39:24 +0100
> > Andrea Claudi <aclaudi@redhat.com> wrote:
> >   
> > > +	if (fscanf(f, "%ms\n", &comm) != 1) {
> > > +		free(comm);
> > > +		return NULL;  
> > 
> > This is still leaking the original comm.
> >  
> 
> Thanks Stephen, I missed the %m over there :(
> 
> > Why not change it to use a local variable for the path
> > (avoid asprintf) and not reuse comm for both pathname
> > and return value.
> >   
> 
> Thanks for the suggestion. 
> 
> What about taking an extra-step and get rid of the %m too?
> We can do something similar to the get_command_name() function so that
> we don't need to use free in places where we use get_task_name().

What ever works best.
diff mbox series

Patch

diff --git a/lib/fs.c b/lib/fs.c
index f6f5f8a0..5692e2d3 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -354,11 +354,15 @@  char *get_task_name(pid_t pid)
 		return NULL;
 
 	f = fopen(comm, "r");
-	if (!f)
+	if (!f) {
+		free(comm);
 		return NULL;
+	}
 
-	if (fscanf(f, "%ms\n", &comm) != 1)
-		comm = NULL;
+	if (fscanf(f, "%ms\n", &comm) != 1) {
+		free(comm);
+		return NULL;
+	}
 
 	fclose(f);