Re: [libvirt] [PATCH 5/6] conf: fix memleak in virDomainNetIpParseXML
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 > --- > 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 signature.asc Description: Digital signature -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
[libvirt] [PATCH 5/6] conf: fix memleak in virDomainNetIpParseXML
use cleanup instead of error, so that the allocated strings could also get freed when there's no error. Signed-off-by: Zhang Bo --- 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; if (!(prefixStr = virXMLPropString(node, "prefix")) || (virStrToLong_ui(prefixStr, NULL, 10, &prefixValue) < 0)) { @@ -5139,7 +5140,7 @@ virDomainNetIpParseXML(xmlNodePtr node) if (!(address = virXMLPropString(node, "address"))) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("Missing network address")); -goto error; +goto cleanup; } familyStr = virXMLPropString(node, "family"); @@ -5151,24 +5152,25 @@ virDomainNetIpParseXML(xmlNodePtr node) family = virSocketAddrNumericFamily(address); if (VIR_ALLOC(ip) < 0) -goto error; +goto cleanup; if (virSocketAddrParse(&ip->address, address, family) < 0) { virReportError(VIR_ERR_INVALID_ARG, _("Failed to parse IP address: '%s'"), address); -goto error; +goto cleanup; } ip->prefix = prefixValue; -return ip; +ret = 0; - error: + cleanup: VIR_FREE(prefixStr); VIR_FREE(familyStr); VIR_FREE(address); -VIR_FREE(ip); -return NULL; +if (ret) +VIR_FREE(ip); +return ip; } static int -- 1.7.12.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list