From 03b6bdcab333ef2298c90cd982490c15b6aaa10f Mon Sep 17 00:00:00 2001 From: Laine Stump Date: Thu, 21 May 2015 15:51:02 -0400 Subject: [PATCH] conf: reorganize virNetworkDHCPDefParseXML This makes the range and static host array management in virNetworkDHCPDefParseXML() more similar to what is done in virNetworkDefUpdateIPDHCPRange() and virNetworkDefUpdateIPDHCPHost() - they use VIR_APPEND_ELEMENT rather than a combination of VIR_REALLOC_N() and separate incrementing of the array size. The one functional change here is that a memory leak of the contents of the last (unsuccessful) virNetworkDHCPHostDef was previously leaked in certain failure conditions, but it is now properly cleaned up. --- src/conf/network_conf.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 72006e9822..f77af15bfc 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -1023,33 +1023,32 @@ virNetworkDHCPDefParseXML(const char *networkName, xmlNodePtr node, virNetworkIpDefPtr def) { - + int ret = -1; xmlNodePtr cur; + virSocketAddrRange range; + virNetworkDHCPHostDef host; + + memset(&range, 0, sizeof(range)); + memset(&host, 0, sizeof(host)); cur = node->children; while (cur != NULL) { if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, BAD_CAST "range")) { - if (VIR_REALLOC_N(def->ranges, def->nranges + 1) < 0) - return -1; - if (virSocketAddrRangeParseXML(networkName, def, cur, - &def->ranges[def->nranges]) < 0) { - return -1; - } - def->nranges++; + if (virSocketAddrRangeParseXML(networkName, def, cur, &range) < 0) + goto cleanup; + if (VIR_APPEND_ELEMENT(def->ranges, def->nranges, range) < 0) + goto cleanup; } else if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, BAD_CAST "host")) { - if (VIR_REALLOC_N(def->hosts, def->nhosts + 1) < 0) - return -1; if (virNetworkDHCPHostDefParseXML(networkName, def, cur, - &def->hosts[def->nhosts], - false) < 0) { - return -1; - } - def->nhosts++; + &host, false) < 0) + goto cleanup; + if (VIR_APPEND_ELEMENT(def->hosts, def->nhosts, host) < 0) + goto cleanup; } else if (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET) && cur->type == XML_ELEMENT_NODE && @@ -1069,7 +1068,7 @@ virNetworkDHCPDefParseXML(const char *networkName, virSocketAddrParse(&inaddr, server, AF_UNSPEC) < 0) { VIR_FREE(file); VIR_FREE(server); - return -1; + goto cleanup; } def->bootfile = file; @@ -1080,7 +1079,10 @@ virNetworkDHCPDefParseXML(const char *networkName, cur = cur->next; } - return 0; + ret = 0; + cleanup: + virNetworkDHCPHostDefClear(&host); + return ret; } static int -- GitLab