Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: Esben Haabendal <esben@geanix.com>
Cc: linux-gpio@vger.kernel.org, brgl@bgdev.pl
Subject: Re: [PATCH] gpioset: fix memory leak in interactive mode
Date: Wed, 7 Dec 2022 17:28:44 +0800	[thread overview]
Message-ID: <Y5BczAGN78xiKgDp@sol> (raw)
In-Reply-To: <f36e4eeee8c5bb96cddcad31a3762c10406a99e9.1670316057.git.esben@geanix.com>

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
> 

  parent reply	other threads:[~2022-12-07  9:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2022-12-07  9:41   ` Bartosz Golaszewski
2022-12-07 11:20     ` Esben Haabendal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y5BczAGN78xiKgDp@sol \
    --to=warthog618@gmail.com \
    --cc=brgl@bgdev.pl \
    --cc=esben@geanix.com \
    --cc=linux-gpio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox