[PATCH v2 1/21] hyperv: avoid query memleaks on failure

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

 



The function hypervEnumAndPull consumes query on success, but leaked
it on failure.  Rather than having to change all callers (many of
them indirect callers through the generated
hypervGetMsvmComputerSystemList), it was easier to just guarantee
that the buffer is cleaned on return from the function.

Reported by Yves Vinter.

* src/hyperv/hyperv_wmi.c (hypervEnumAndPull): Don't leak query on
failure.

Signed-off-by: Eric Blake <eblake@xxxxxxxxxx>
---
 src/hyperv/hyperv_wmi.c | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)

diff --git a/src/hyperv/hyperv_wmi.c b/src/hyperv/hyperv_wmi.c
index acb705c..3027601 100644
--- a/src/hyperv/hyperv_wmi.c
+++ b/src/hyperv/hyperv_wmi.c
@@ -2,6 +2,7 @@
  * hyperv_wmi.c: general WMI over WSMAN related functions and structures for
  *               managing Microsoft Hyper-V hosts
  *
+ * Copyright (C) 2014 Red Hat, Inc.
  * Copyright (C) 2011 Matthias Bolte <matthias.bolte@xxxxxxxxxxxxxx>
  * Copyright (C) 2009 Michael Sievers <msievers83@xxxxxxxxxxxxxx>
  *
@@ -105,6 +106,7 @@ hyperyVerifyResponse(WsManClient *client, WsXmlDocH response,
  * Object
  */

+/* This function guarantees that query is freed, even on failure */
 int
 hypervEnumAndPull(hypervPrivate *priv, virBufferPtr query, const char *root,
                   XmlSerializerInfo *serializerInfo, const char *resourceUri,
@@ -123,25 +125,28 @@ hypervEnumAndPull(hypervPrivate *priv, virBufferPtr query, const char *root,
     XML_TYPE_PTR data = NULL;
     hypervObject *object;

-    if (list == NULL || *list != NULL) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
+    if (virBufferCheckError(query) < 0) {
+        virBufferFreeAndReset(query);
         return -1;
     }
-
-    if (virBufferCheckError(query) < 0)
-        return -1;
-
-    serializerContext = wsmc_get_serialization_context(priv->client);
-
-    options = wsmc_options_init();
-
-    if (options == NULL) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("Could not initialize options"));
-        goto cleanup;
-    }
-
     query_string = virBufferContentAndReset(query);
+
+    if (list == NULL || *list != NULL) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
+        VIR_FREE(query_string);
+        return -1;
+    }
+
+    serializerContext = wsmc_get_serialization_context(priv->client);
+
+    options = wsmc_options_init();
+
+    if (options == NULL) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Could not initialize options"));
+        goto cleanup;
+    }
+
     filter = filter_create_simple(WSM_WQL_FILTER_DIALECT, query_string);

     if (filter == NULL) {
-- 
1.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]