[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 3/n] network: plug memory leak



* src/conf/network_conf.c (virNetworkDHCPRangeDefParseXML): Free
xml strings when no longer referenced.
---

Actually plug the leak.  I've increased the context of this diff to
make it easier to analyze.

 src/conf/network_conf.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 9868250..b469269 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -233,123 +233,127 @@ virNetworkDHCPRangeDefParseXML(virNetworkDefPtr def,
             }

             if (virSocketParseAddr(start, &saddr, AF_UNSPEC) < 0) {
                 VIR_FREE(start);
                 VIR_FREE(end);
                 return -1;
             }
             if (virSocketParseAddr(end, &eaddr, AF_UNSPEC) < 0) {
                 VIR_FREE(start);
                 VIR_FREE(end);
                 return -1;
             }

             range = virSocketGetRange(&saddr, &eaddr);
             if (range < 0) {
                 virNetworkReportError(VIR_ERR_XML_ERROR,
                                       _("dhcp range '%s' to '%s' invalid"),
                                       start, end);
                 VIR_FREE(start);
                 VIR_FREE(end);
                 return -1;
             }
+            VIR_FREE(start);
+            VIR_FREE(end);

             if (VIR_REALLOC_N(def->ranges, def->nranges + 1) < 0) {
-                VIR_FREE(start);
-                VIR_FREE(end);
                 virReportOOMError();
                 return -1;
             }
             def->ranges[def->nranges].start = saddr;
             def->ranges[def->nranges].end = eaddr;
             def->nranges++;
         } else if (cur->type == XML_ELEMENT_NODE &&
             xmlStrEqual(cur->name, BAD_CAST "host")) {
             char *mac, *name, *ip;
             unsigned char addr[6];
             virSocketAddr inaddr;

             mac = virXMLPropString(cur, "mac");
             if ((mac != NULL) &&
                 (virParseMacAddr(mac, &addr[0]) != 0)) {
                 virNetworkReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("cannot parse MAC address '%s'"),
                                       mac);
                 VIR_FREE(mac);
             }
             name = virXMLPropString(cur, "name");
             if ((name != NULL) && (!c_isalpha(name[0]))) {
                 virNetworkReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("cannot use name address '%s'"),
                                       name);
                 VIR_FREE(name);
             }
             /*
              * You need at least one MAC address or one host name
              */
             if ((mac == NULL) && (name == NULL)) {
                 VIR_FREE(mac);
                 VIR_FREE(name);
                 cur = cur->next;
                 continue;
             }
             ip = virXMLPropString(cur, "ip");
             if (virSocketParseAddr(ip, &inaddr, AF_UNSPEC) < 0) {
                 VIR_FREE(ip);
                 VIR_FREE(mac);
                 VIR_FREE(name);
                 cur = cur->next;
                 continue;
             }
+            VIR_FREE(ip);
             if (VIR_REALLOC_N(def->hosts, def->nhosts + 1) < 0) {
-                VIR_FREE(ip);
                 VIR_FREE(mac);
                 VIR_FREE(name);
                 virReportOOMError();
                 return -1;
             }
             def->hosts[def->nhosts].mac = mac;
             def->hosts[def->nhosts].name = name;
             def->hosts[def->nhosts].ip = inaddr;
             def->nhosts++;

         } else if (cur->type == XML_ELEMENT_NODE &&
             xmlStrEqual(cur->name, BAD_CAST "bootp")) {
             char *file;
             char *server;
             virSocketAddr inaddr;
             memset(&inaddr, 0, sizeof(inaddr));

             if (!(file = virXMLPropString(cur, "file"))) {
                 cur = cur->next;
                 continue;
             }
             server = virXMLPropString(cur, "server");

             if (server &&
-                virSocketParseAddr(server, &inaddr, AF_UNSPEC) < 0)
+                virSocketParseAddr(server, &inaddr, AF_UNSPEC) < 0) {
+                VIR_FREE(file);
+                VIR_FREE(server);
                 return -1;
+            }

             def->bootfile = file;
             def->bootserver = inaddr;
+            VIR_FREE(server);
         }

         cur = cur->next;
     }

     return 0;
 }

 static int
 virNetworkIPParseXML(virNetworkDefPtr def,
                      xmlNodePtr node) {
     xmlNodePtr cur;

     cur = node->children;
     while (cur != NULL) {
         if (cur->type == XML_ELEMENT_NODE &&
             xmlStrEqual(cur->name, BAD_CAST "dhcp")) {
             int result = virNetworkDHCPRangeDefParseXML(def, cur);
             if (result)
                 return result;

         } else if (cur->type == XML_ELEMENT_NODE &&
-- 
1.7.3.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]