qemu-trivial
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] core/qdev: fix memleak in qdev_get_gpio_out_connector()


From: Daniel P . Berrangé
Subject: Re: [PATCH] core/qdev: fix memleak in qdev_get_gpio_out_connector()
Date: Mon, 9 Mar 2020 10:48:51 +0000
User-agent: Mutt/1.13.3 (2020-01-12)

On Mon, Mar 09, 2020 at 10:16:28AM +0100, Laurent Vivier wrote:
> Le 07/03/2020 à 11:39, Marc-André Lureau a écrit :
> > Hi
> > 
> > On Sat, Mar 7, 2020 at 3:53 AM Pan Nengyuan <address@hidden> wrote:
> >>
> >> Fix a memory leak in qdev_get_gpio_out_connector().
> >>
> >> Reported-by: Euler Robot <address@hidden>
> >> Signed-off-by: Pan Nengyuan <address@hidden>
> > 
> > good catch,
> > Reviewed-by: Marc-André Lureau <address@hidden>
> 
> trivial question:
> 
> Why do we prefer g_autofree() to the g_free() function?

The g_autofree annotation guarantees that g_free() gets invoked
when the variable goes out of scope, in all code paths.

This avoids the need to do the classic "goto cleanup;" jumps
with manuall free calls. So as well as simplifying code it
makes it less error prone in general. Of course the method
in this patch is only three lines long so you don't really
see the benefits, but there's also no real harm. So I'd
personally always pick g_autofree in situations where it
is usable, as it is a good habit to be in when you get to
more complex codepaths.

> >> ---
> >>  hw/core/qdev.c | 2 +-
> >>  1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/hw/core/qdev.c b/hw/core/qdev.c
> >> index 3937d1eb1a..85f062def7 100644
> >> --- a/hw/core/qdev.c
> >> +++ b/hw/core/qdev.c
> >> @@ -557,7 +557,7 @@ void qdev_connect_gpio_out_named(DeviceState *dev, 
> >> const char *name, int n,
> >>
> >>  qemu_irq qdev_get_gpio_out_connector(DeviceState *dev, const char *name, 
> >> int n)
> >>  {
> >> -    char *propname = g_strdup_printf("%s[%d]",
> >> +    g_autofree char *propname = g_strdup_printf("%s[%d]",
> >>                                       name ? name : "unnamed-gpio-out", n);
> >>
> >>      qemu_irq ret = (qemu_irq)object_property_get_link(OBJECT(dev), 
> >> propname,
> >> --

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|




reply via email to

[Prev in Thread] Current Thread [Next in Thread]