All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Dr. David Alan Gilbert" <dave@treblig.org>
To: Peter Xu <peterx@redhat.com>
Cc: qemu-devel@nongnu.org, Fabiano Rosas <farosas@suse.de>,
	Juraj Marcin <jmarcin@redhat.com>,
	Peter Maydell <peter.maydell@linaro.org>
Subject: Re: [PATCH] migration/cpr: Document obscure usage of g_autofree when parse str
Date: Thu, 23 Oct 2025 20:07:48 +0000	[thread overview]
Message-ID: <aPqLFI2vXGY0mzDb@gallifrey> (raw)
In-Reply-To: <aPp1IwqiGpBUToWg@x1.local>

* Peter Xu (peterx@redhat.com) wrote:
> On Thu, Oct 23, 2025 at 05:42:27PM +0000, Dr. David Alan Gilbert wrote:
> > * Peter Xu (peterx@redhat.com) wrote:
> > > HMP parsing of cpr_exec_command contains an obscure usage of g_autofree.
> > > Provide a document for it to be clear that it's intentional, rather than
> > > memory leaked.
> > > 
> > > Cc: Dr. David Alan Gilbert <dave@treblig.org>
> > > Reported-by: Peter Maydell <peter.maydell@linaro.org>
> > > Signed-off-by: Peter Xu <peterx@redhat.com>
> > > ---
> > >  migration/migration-hmp-cmds.c | 6 ++++++
> > >  1 file changed, 6 insertions(+)
> > > 
> > > diff --git a/migration/migration-hmp-cmds.c b/migration/migration-hmp-cmds.c
> > > index 847d18faaa..79426bf5d7 100644
> > > --- a/migration/migration-hmp-cmds.c
> > > +++ b/migration/migration-hmp-cmds.c
> > > @@ -734,6 +734,12 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
> > >          visit_type_bool(v, param, &p->direct_io, &err);
> > >          break;
> > >      case MIGRATION_PARAMETER_CPR_EXEC_COMMAND: {
> > > +        /*
> > > +         * NOTE: g_autofree will only auto g_free() the strv array when
> > > +         * needed, it will not free the strings within the array. It's
> > > +         * intentional: when strv is set, the ownership of the strings will
> > > +         * always be passed to p->cpr_exec_command via QAPI_LIST_APPEND().
> > > +         */
> > 
> > Eww that's a bit weird isn't it.
> > It's not clear to me if g_shell_parse_argv() might return an error part
> > way through its parsing, and if it does whether there may be valid entries in
> > strv which really do need freeing.
> > 
> > https://docs.gtk.org/glib/func.shell_parse_argv.html doesn't seem to say.
> 
> I checked glib code, it should be fine, the code looks like this since 2000:
> 
> gboolean
> g_shell_parse_argv (const gchar *command_line,
>                     gint        *argcp,
>                     gchar     ***argvp,
>                     GError     **error)
> {
>   ...
>  failed:
> 
>   g_assert (error == NULL || *error != NULL);
>   g_strfreev (argv);                            <-------------
>   g_slist_free_full (tokens, g_free);
>   
>   return FALSE;
> }

OK, thanks for checking.

> The hope is with the comment, the current way is still the most efficient,
> avoiding strdup()s.

Yeh, it's at least better with the comment!

Reviewed-by: Dr. David Alan Gilbert <dave@treblig.org>


> Still, let me know if any of us still prefer changing the code instead of
> adding the comment..  The perf isn't a major issue, afaiu.  But it's still
> good to consider that always, I believe that was when Steve developed this.
> 
> Thanks!

Thanks for adding it.

Dave

> -- 
> Peter Xu
> 
-- 
 -----Open up your eyes, open up your mind, open up your code -------   
/ Dr. David Alan Gilbert    |       Running GNU/Linux       | Happy  \ 
\        dave @ treblig.org |                               | In Hex /
 \ _________________________|_____ http://www.treblig.org   |_______/


      reply	other threads:[~2025-10-23 20:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-23 16:16 [PATCH] migration/cpr: Document obscure usage of g_autofree when parse str Peter Xu
2025-10-23 17:42 ` Dr. David Alan Gilbert
2025-10-23 18:34   ` Peter Xu
2025-10-23 20:07     ` Dr. David Alan Gilbert [this message]

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=aPqLFI2vXGY0mzDb@gallifrey \
    --to=dave@treblig.org \
    --cc=farosas@suse.de \
    --cc=jmarcin@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@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.