Linux GPIO subsystem development
 help / color / mirror / Atom feed
* [PATCH] gpioset: fix memory leak in interactive mode
  2022-12-06  8:46 [PATCH] gpioset: fix memory leak in interactive mode Esben Haabendal
@ 2022-12-06  8:45 ` Esben Haabendal
  2022-12-06  8:46 ` Esben Haabendal
  2022-12-07  9:28 ` Kent Gibson
  2 siblings, 0 replies; 6+ messages in thread
From: Esben Haabendal @ 2022-12-06  8:45 UTC (permalink / raw)
  To: linux-gpio

Even when readline() returns an empty buffer, we still need to free() it to
avoid leaking memory.

Signed-off-by: Esben Haabendal <esben@geanix.com>
---
 tools/gpioset.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/tools/gpioset.c b/tools/gpioset.c
index c49d229870d2..f087003af1c9 100644
--- a/tools/gpioset.c
+++ b/tools/gpioset.c
@@ -768,8 +768,12 @@ static void interact(struct gpiod_line_request **requests,
 		fflush(stdout);
 
 		line = readline(PROMPT);
-		if (!line || line[0] == '\0')
+		if (!line)
 			continue;
+		if (line[0] == '\0') {
+			free(line);
+			continue;
+		}
 
 		for (i = strlen(line) - 1; (i > 0) && isspace(line[i]); i--)
 			line[i] = '\0';
-- 
2.38.1


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

* [PATCH] gpioset: fix memory leak in interactive mode
  2022-12-06  8:46 [PATCH] gpioset: fix memory leak in interactive mode Esben Haabendal
  2022-12-06  8:45 ` Esben Haabendal
@ 2022-12-06  8:46 ` Esben Haabendal
  2022-12-07  9:28 ` Kent Gibson
  2 siblings, 0 replies; 6+ messages in thread
From: Esben Haabendal @ 2022-12-06  8:46 UTC (permalink / raw)
  To: linux-gpio

Even when readline() returns an empty buffer, we still need to free() it to
avoid leaking memory.

Signed-off-by: Esben Haabendal <esben@geanix.com>
---
 tools/gpioset.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/tools/gpioset.c b/tools/gpioset.c
index c49d229870d2..f087003af1c9 100644
--- a/tools/gpioset.c
+++ b/tools/gpioset.c
@@ -768,8 +768,12 @@ static void interact(struct gpiod_line_request **requests,
 		fflush(stdout);
 
 		line = readline(PROMPT);
-		if (!line || line[0] == '\0')
+		if (!line)
 			continue;
+		if (line[0] == '\0') {
+			free(line);
+			continue;
+		}
 
 		for (i = strlen(line) - 1; (i > 0) && isspace(line[i]); i--)
 			line[i] = '\0';
-- 
2.38.1


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

* [PATCH] gpioset: fix memory leak in interactive mode
@ 2022-12-06  8:46 Esben Haabendal
  2022-12-06  8:45 ` Esben Haabendal
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Esben Haabendal @ 2022-12-06  8:46 UTC (permalink / raw)
  To: linux-gpio

Even when readline() returns an empty buffer, we still need to free() it to
avoid leaking memory.

Signed-off-by: Esben Haabendal <esben@geanix.com>
---
 tools/gpioset.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/tools/gpioset.c b/tools/gpioset.c
index c49d229870d2..f087003af1c9 100644
--- a/tools/gpioset.c
+++ b/tools/gpioset.c
@@ -768,8 +768,12 @@ static void interact(struct gpiod_line_request **requests,
 		fflush(stdout);
 
 		line = readline(PROMPT);
-		if (!line || line[0] == '\0')
+		if (!line)
 			continue;
+		if (line[0] == '\0') {
+			free(line);
+			continue;
+		}
 
 		for (i = strlen(line) - 1; (i > 0) && isspace(line[i]); i--)
 			line[i] = '\0';
-- 
2.38.1


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

* Re: [PATCH] gpioset: fix memory leak in interactive mode
  2022-12-06  8:46 [PATCH] gpioset: fix memory leak in interactive mode Esben Haabendal
  2022-12-06  8:45 ` Esben Haabendal
  2022-12-06  8:46 ` Esben Haabendal
@ 2022-12-07  9:28 ` Kent Gibson
  2022-12-07  9:41   ` Bartosz Golaszewski
  2 siblings, 1 reply; 6+ messages in thread
From: Kent Gibson @ 2022-12-07  9:28 UTC (permalink / raw)
  To: Esben Haabendal; +Cc: linux-gpio, brgl

On Tue, Dec 06, 2022 at 09:46:37AM +0100, Esben Haabendal wrote:
> Even when readline() returns an empty buffer, we still need to free() it to
> avoid leaking memory.
> 

Good point.

> Signed-off-by: Esben Haabendal <esben@geanix.com>

As per the README, you should prefix your subject with [libgpiod] to
better get the attention of the libgpiod team.

> ---
>  tools/gpioset.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/gpioset.c b/tools/gpioset.c
> index c49d229870d2..f087003af1c9 100644
> --- a/tools/gpioset.c
> +++ b/tools/gpioset.c
> @@ -768,8 +768,12 @@ static void interact(struct gpiod_line_request **requests,
>  		fflush(stdout);
>  
>  		line = readline(PROMPT);
> -		if (!line || line[0] == '\0')
> +		if (!line)
>  			continue;
> +		if (line[0] == '\0') {
> +			free(line);
> +			continue;
> +		}
>  

Given free() is null-aware, I would just add the free before the
continue, rather than splitting out the cases, so

                line = readline(PROMPT);
-               if (!line || line[0] == '\0')
+               if (!line || line[0] == '\0') {
+                       free(line);
                        continue;
+               }


Cheers,
Kent.

>  		for (i = strlen(line) - 1; (i > 0) && isspace(line[i]); i--)
>  			line[i] = '\0';
> -- 
> 2.38.1
> 

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

* Re: [PATCH] gpioset: fix memory leak in interactive mode
  2022-12-07  9:28 ` Kent Gibson
@ 2022-12-07  9:41   ` Bartosz Golaszewski
  2022-12-07 11:20     ` Esben Haabendal
  0 siblings, 1 reply; 6+ messages in thread
From: Bartosz Golaszewski @ 2022-12-07  9:41 UTC (permalink / raw)
  To: Kent Gibson; +Cc: Esben Haabendal, linux-gpio

On Wed, Dec 7, 2022 at 10:28 AM Kent Gibson <warthog618@gmail.com> wrote:
>
> On Tue, Dec 06, 2022 at 09:46:37AM +0100, Esben Haabendal wrote:
> > Even when readline() returns an empty buffer, we still need to free() it to
> > avoid leaking memory.
> >
>
> Good point.
>
> > Signed-off-by: Esben Haabendal <esben@geanix.com>
>
> As per the README, you should prefix your subject with [libgpiod] to
> better get the attention of the libgpiod team.
>
> > ---
> >  tools/gpioset.c | 6 +++++-
> >  1 file changed, 5 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/gpioset.c b/tools/gpioset.c
> > index c49d229870d2..f087003af1c9 100644
> > --- a/tools/gpioset.c
> > +++ b/tools/gpioset.c
> > @@ -768,8 +768,12 @@ static void interact(struct gpiod_line_request **requests,
> >               fflush(stdout);
> >
> >               line = readline(PROMPT);
> > -             if (!line || line[0] == '\0')
> > +             if (!line)
> >                       continue;
> > +             if (line[0] == '\0') {
> > +                     free(line);
> > +                     continue;
> > +             }
> >
>
> Given free() is null-aware, I would just add the free before the
> continue, rather than splitting out the cases, so
>
>                 line = readline(PROMPT);
> -               if (!line || line[0] == '\0')
> +               if (!line || line[0] == '\0') {
> +                       free(line);
>                         continue;
> +               }
>
>
> Cheers,
> Kent.
>
> >               for (i = strlen(line) - 1; (i > 0) && isspace(line[i]); i--)
> >                       line[i] = '\0';
> > --
> > 2.38.1
> >

I applied it with the change suggested by Kent.

Bart

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

* Re: [PATCH] gpioset: fix memory leak in interactive mode
  2022-12-07  9:41   ` Bartosz Golaszewski
@ 2022-12-07 11:20     ` Esben Haabendal
  0 siblings, 0 replies; 6+ messages in thread
From: Esben Haabendal @ 2022-12-07 11:20 UTC (permalink / raw)
  To: Bartosz Golaszewski; +Cc: Kent Gibson, linux-gpio

Bartosz Golaszewski <brgl@bgdev.pl> writes:

> I applied it with the change suggested by Kent.

Great, thanks.

/Esben

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

end of thread, other threads:[~2022-12-07 11:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-06  8:46 [PATCH] gpioset: fix memory leak in interactive mode Esben Haabendal
2022-12-06  8:45 ` Esben Haabendal
2022-12-06  8:46 ` Esben Haabendal
2022-12-07  9:28 ` Kent Gibson
2022-12-07  9:41   ` Bartosz Golaszewski
2022-12-07 11:20     ` Esben Haabendal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox