[libvirt] [PATCHv3 7/7] xen_common: Fix a few memory leaks

Fabiano Fidêncio posted 7 patches 6 years, 11 months ago
There is a newer version of this series
[libvirt] [PATCHv3 7/7] xen_common: Fix a few memory leaks
Posted by Fabiano Fidêncio 6 years, 11 months ago
While converting the functions of xen_common to use typesafe virConf
acessors, I've spotted a few memory leaks, which are fixed in this
patch.

Signed-off-by: Fabiano Fidêncio <fabiano@fidencio.org>
---
 src/xenconfig/xen_common.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/xenconfig/xen_common.c b/src/xenconfig/xen_common.c
index 2ba1a19c39..6d9ce9bd66 100644
--- a/src/xenconfig/xen_common.c
+++ b/src/xenconfig/xen_common.c
@@ -458,14 +458,16 @@ xenParsePCIList(virConfPtr conf, virDomainDefPtr def)
     for (entries = pcis; *entries; entries++) {
         char *entry = *entries;
         virDomainHostdevDefPtr hostdev;
+        int rc;
 
         if (!(hostdev = xenParsePCI(entry)))
             goto cleanup;
 
-        if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev) < 0) {
-            virDomainHostdevDefFree(hostdev);
+        rc = VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev);
+        virDomainHostdevDefFree(hostdev);
+
+        if (rc < 0)
             goto cleanup;
-        }
     }
 
     ret = 0;
@@ -787,6 +789,7 @@ xenParseCharDev(virConfPtr conf, virDomainDefPtr def, const char *nativeFormat)
 
             for (entries = serials; *entries; entries++) {
                 char *port = *entries;
+                int rc;
 
                 portnum++;
                 if (STREQ(port, "none"))
@@ -796,10 +799,11 @@ xenParseCharDev(virConfPtr conf, virDomainDefPtr def, const char *nativeFormat)
                     goto cleanup;
                 chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
                 chr->target.port = portnum;
-                if (VIR_APPEND_ELEMENT(def->serials, def->nserials, chr) < 0) {
-                    virDomainChrDefFree(chr);
+                rc = VIR_APPEND_ELEMENT(def->serials, def->nserials, chr);
+                virDomainChrDefFree(chr);
+
+                if (rc < 0)
                     goto cleanup;
-                }
             }
         } else {
             /* If domain is not using multiple serial ports we parse data old way */
@@ -1047,10 +1051,10 @@ xenParseVifList(virConfPtr conf, virDomainDefPtr def, const char *vif_typename)
             goto cleanup;
 
         rc = VIR_APPEND_ELEMENT(def->nets, def->nnets, net);
-        if (rc < 0) {
-            virDomainNetDefFree(net);
+        virDomainNetDefFree(net);
+
+        if (rc < 0)
             goto cleanup;
-        }
     }
 
     ret = 0;
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCHv3 7/7] xen_common: Fix a few memory leaks
Posted by Ján Tomko 6 years, 11 months ago
On Mon, May 28, 2018 at 12:28:26AM +0200, Fabiano Fidêncio wrote:
>While converting the functions of xen_common to use typesafe virConf
>acessors, I've spotted a few memory leaks, which are fixed in this
>patch.
>
>Signed-off-by: Fabiano Fidêncio <fabiano@fidencio.org>
>---
> src/xenconfig/xen_common.c | 22 +++++++++++++---------
> 1 file changed, 13 insertions(+), 9 deletions(-)
>
>diff --git a/src/xenconfig/xen_common.c b/src/xenconfig/xen_common.c
>index 2ba1a19c39..6d9ce9bd66 100644
>--- a/src/xenconfig/xen_common.c
>+++ b/src/xenconfig/xen_common.c
>@@ -458,14 +458,16 @@ xenParsePCIList(virConfPtr conf, virDomainDefPtr def)
>     for (entries = pcis; *entries; entries++) {
>         char *entry = *entries;
>         virDomainHostdevDefPtr hostdev;
>+        int rc;
>
>         if (!(hostdev = xenParsePCI(entry)))
>             goto cleanup;
>
>-        if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev) < 0) {
>-            virDomainHostdevDefFree(hostdev);
>+        rc = VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev);
>+        virDomainHostdevDefFree(hostdev);
>+

If VIR_APPEND_ELEMENT returns 0, hostdev has been added to the domain
definition and it should be freed by whoever frees the virDomainDef.

Also, on success the arguments of VIR_APPEND_ELEMENT are zeroed, so
this change is a no-op.

Jano
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list