[libvirt] [PATCH 1/2] conf: use VIR_EXPAND instead of VIR_REALLOC in virNetworkDHCPDefParseXML

Laine Stump laine at laine.org
Mon Feb 25 04:37:48 UTC 2013


VIR_REALLOC_N doesn't initialize the newly allocated memory to 0,
which can result in unpleasant surprises for those who have become
accustomed to the 0-initializing behavior of most libvirt memory
allocation functions, and add a new field to a struct thinking that it
will default to 0.
---

(in particular, this patch assures that the upcoming "force" attribute
in virNetworkDHCPOptionDef will be initialized to false.)

 src/conf/network_conf.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 34fd05a..b3e2858 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -820,29 +820,29 @@ virNetworkDHCPDefParseXML(const char *networkName,
         if (cur->type == XML_ELEMENT_NODE &&
             xmlStrEqual(cur->name, BAD_CAST "range")) {
 
-            if (VIR_REALLOC_N(def->ranges, def->nranges + 1) < 0) {
+            if (VIR_EXPAND_N(def->ranges, def->nranges, 1) < 0) {
                 virReportOOMError();
                 return -1;
             }
             if (virSocketAddrRangeParseXML(networkName, cur,
-                                               &def->ranges[def->nranges]) < 0) {
+                                           &def->ranges[def->nranges - 1]) < 0) {
+                def->nranges--;
                 return -1;
             }
-            def->nranges++;
 
         } else if (cur->type == XML_ELEMENT_NODE &&
             xmlStrEqual(cur->name, BAD_CAST "host")) {
 
-            if (VIR_REALLOC_N(def->hosts, def->nhosts + 1) < 0) {
+            if (VIR_EXPAND_N(def->hosts, def->nhosts, 1) < 0) {
                 virReportOOMError();
                 return -1;
             }
             if (virNetworkDHCPHostDefParseXML(networkName, def, cur,
-                                              &def->hosts[def->nhosts],
+                                              &def->hosts[def->nhosts - 1],
                                               false) < 0) {
+                def->nhosts--;
                 return -1;
             }
-            def->nhosts++;
 
         } else if (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET) &&
                    cur->type == XML_ELEMENT_NODE &&
@@ -870,15 +870,15 @@ virNetworkDHCPDefParseXML(const char *networkName,
             VIR_FREE(server);
         } else if (cur->type == XML_ELEMENT_NODE &&
             xmlStrEqual(cur->name, BAD_CAST "option")) {
-            if (VIR_REALLOC_N(def->options, def->noptions + 1) < 0) {
+            if (VIR_EXPAND_N(def->options, def->noptions, 1) < 0) {
                 virReportOOMError();
                 return -1;
             }
             if (virNetworkDHCPOptionDefParseXML(networkName, cur,
-                                                &def->options[def->noptions])) {
+                                                &def->options[def->noptions - 1])) {
+                def->noptions--;
                 return -1;
             }
-            def->noptions++;
         }
 
         cur = cur->next;
-- 
1.7.11.7




More information about the libvir-list mailing list