[libvirt] [PATCH 5/6] conf: fix memleak in virDomainNetIpParseXML

Ján Tomko jtomko at redhat.com
Mon Apr 27 08:04:07 UTC 2015


On Mon, Apr 27, 2015 at 02:41:44PM +0800, Zhang Bo wrote:
> use cleanup instead of error, so that the allocated strings could also get freed
> when there's no error.
> 
> Signed-off-by: Zhang Bo <oscar.zhangbo at huawei.com>
> ---
>  src/conf/domain_conf.c | 16 +++++++++-------
>  1 file changed, 9 insertions(+), 7 deletions(-)
> 
> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> index 41963cc..8350fe7 100644
> --- a/src/conf/domain_conf.c
> +++ b/src/conf/domain_conf.c
> @@ -5129,6 +5129,7 @@ virDomainNetIpParseXML(xmlNodePtr node)
>      char *familyStr = NULL;
>      int family = AF_UNSPEC;
>      char *address = NULL;
> +    int ret = -1;

The 'ret' variable should contain the return value of the function.

I am squashing this in:

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index b2640b0..1b520b9 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -5124,13 +5124,12 @@ static virDomainNetIpDefPtr
 virDomainNetIpParseXML(xmlNodePtr node)
 {
     /* Parse the prefix in every case */
-    virDomainNetIpDefPtr ip = NULL;
+    virDomainNetIpDefPtr ip = NULL, ret = NULL;
     char *prefixStr = NULL;
     unsigned int prefixValue = 0;
     char *familyStr = NULL;
     int family = AF_UNSPEC;
     char *address = NULL;
-    int ret = -1;
 
     if (!(prefixStr = virXMLPropString(node, "prefix")) ||
         (virStrToLong_ui(prefixStr, NULL, 10, &prefixValue) < 0)) {
@@ -5163,15 +5162,15 @@ virDomainNetIpParseXML(xmlNodePtr node)
     }
     ip->prefix = prefixValue;
 
-    ret = 0;
+    ret = ip;
+    ip = NULL;
 
  cleanup:
     VIR_FREE(prefixStr);
     VIR_FREE(familyStr);
     VIR_FREE(address);
-    if (ret)
-        VIR_FREE(ip);
-    return ip;
+    VIR_FREE(ip);
+    return ret;
 }
 

Jan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20150427/317c7557/attachment-0001.sig>


More information about the libvir-list mailing list