All of lore.kernel.org
 help / color / mirror / Atom feed
From: Leon Alrae <leon.alrae@imgtec.com>
To: <arei.gonglei@huawei.com>, <qemu-devel@nongnu.org>
Cc: qemu-trivial@nongnu.org, pbonzini@redhat.com
Subject: Re: [Qemu-trivial] [Qemu-devel] [PATCH 4/4] vl.c: fix memory leak
Date: Fri, 10 Jul 2015 10:28:48 +0100	[thread overview]
Message-ID: <559F9050.5080402@imgtec.com> (raw)
In-Reply-To: <1436489490-236-5-git-send-email-arei.gonglei@huawei.com>

On 10/07/2015 01:51, arei.gonglei@huawei.com wrote:
> From: Gonglei <arei.gonglei@huawei.com>
> 
> Failing to save or free storage allocated
> by "g_strdup(cmd)" leaks it. Let's use a
> variable to storage it.
> 
> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
> ---
>  vl.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/vl.c b/vl.c
> index 3f269dc..399e816 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -1326,16 +1326,19 @@ static int add_semihosting_arg(void *opaque,
>  static inline void semihosting_arg_fallback(const char *file, const char *cmd)
>  {
>      char *cmd_token;
> +    char *cmd_str;
>  
>      /* argv[0] */
>      add_semihosting_arg(&semihosting, "arg", file, NULL);
>  
> +    cmd_str = g_strdup(cmd);
>      /* split -append and initialize argv[1..n] */
> -    cmd_token = strtok(g_strdup(cmd), " ");
> +    cmd_token = strtok(cmd_str, " ");
>      while (cmd_token) {
>          add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
>          cmd_token = strtok(NULL, " ");
>      }
> +    g_free(cmd_str);
>  }
>  
>  /***********************************************************/
> 

I don't think this is correct as there's no leak here. This duplicated string
is modified (i.e. split into tokens) and each pointer to the beginning of a
token is saved in the global semihosting.argv[] array which is used later in
target semihosting code. It shouldn't be freed.

Regards,
Leon



WARNING: multiple messages have this Message-ID (diff)
From: Leon Alrae <leon.alrae@imgtec.com>
To: arei.gonglei@huawei.com, qemu-devel@nongnu.org
Cc: qemu-trivial@nongnu.org, pbonzini@redhat.com
Subject: Re: [Qemu-devel] [PATCH 4/4] vl.c: fix memory leak
Date: Fri, 10 Jul 2015 10:28:48 +0100	[thread overview]
Message-ID: <559F9050.5080402@imgtec.com> (raw)
In-Reply-To: <1436489490-236-5-git-send-email-arei.gonglei@huawei.com>

On 10/07/2015 01:51, arei.gonglei@huawei.com wrote:
> From: Gonglei <arei.gonglei@huawei.com>
> 
> Failing to save or free storage allocated
> by "g_strdup(cmd)" leaks it. Let's use a
> variable to storage it.
> 
> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
> ---
>  vl.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/vl.c b/vl.c
> index 3f269dc..399e816 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -1326,16 +1326,19 @@ static int add_semihosting_arg(void *opaque,
>  static inline void semihosting_arg_fallback(const char *file, const char *cmd)
>  {
>      char *cmd_token;
> +    char *cmd_str;
>  
>      /* argv[0] */
>      add_semihosting_arg(&semihosting, "arg", file, NULL);
>  
> +    cmd_str = g_strdup(cmd);
>      /* split -append and initialize argv[1..n] */
> -    cmd_token = strtok(g_strdup(cmd), " ");
> +    cmd_token = strtok(cmd_str, " ");
>      while (cmd_token) {
>          add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
>          cmd_token = strtok(NULL, " ");
>      }
> +    g_free(cmd_str);
>  }
>  
>  /***********************************************************/
> 

I don't think this is correct as there's no leak here. This duplicated string
is modified (i.e. split into tokens) and each pointer to the beginning of a
token is saved in the global semihosting.argv[] array which is used later in
target semihosting code. It shouldn't be freed.

Regards,
Leon

  reply	other threads:[~2015-07-10  9:29 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-10  0:51 [Qemu-trivial] [PATCH 0/4] fix memory leak arei.gonglei
2015-07-10  0:51 ` [Qemu-devel] " arei.gonglei
2015-07-10  0:51 ` [Qemu-trivial] [PATCH 1/4] cpu: " arei.gonglei
2015-07-10  0:51   ` [Qemu-devel] " arei.gonglei
2015-07-10  0:51 ` [Qemu-trivial] [PATCH 2/4] ppc/spapr_drc: " arei.gonglei
2015-07-10  0:51   ` [Qemu-devel] " arei.gonglei
2015-07-10 18:49   ` [Qemu-trivial] " Michael Roth
2015-07-10 18:49     ` [Qemu-devel] " Michael Roth
2015-07-10  0:51 ` [Qemu-trivial] [PATCH 3/4] arm/xlnx-zynqmp: " arei.gonglei
2015-07-10  0:51   ` [Qemu-devel] " arei.gonglei
2015-07-11  8:54   ` [Qemu-trivial] " Peter Crosthwaite
2015-07-11  8:54     ` Peter Crosthwaite
2015-07-10  0:51 ` [Qemu-trivial] [PATCH 4/4] vl.c: " arei.gonglei
2015-07-10  0:51   ` [Qemu-devel] " arei.gonglei
2015-07-10  9:28   ` Leon Alrae [this message]
2015-07-10  9:28     ` Leon Alrae
2015-07-10 10:39     ` [Qemu-trivial] " Gonglei
2015-07-10 10:39       ` Gonglei
2015-07-10 12:54 ` [Qemu-trivial] [PATCH 0/4] " Paolo Bonzini
2015-07-10 12:54   ` [Qemu-devel] " Paolo Bonzini

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=559F9050.5080402@imgtec.com \
    --to=leon.alrae@imgtec.com \
    --cc=arei.gonglei@huawei.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@nongnu.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.