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

[libvirt] [PATCH] Add virBufferFreeAndReset() and replace free()



Replace free(virBufferContentAndReset()) with virBufferFreeAndReset().
Update documentation and replace all remaining calls to free() with
calls to VIR_FREE(). Also add missing calls to virBufferFreeAndReset()
and virReportOOMError() in OOM error cases.
---
 HACKING                            |    5 ++-
 daemon/libvirtd.c                  |    6 ++--
 docs/hacking.html.in               |    7 ++--
 src/conf/capabilities.c            |    4 ++-
 src/conf/domain_conf.c             |   11 +++---
 src/conf/interface_conf.c          |    5 +--
 src/conf/network_conf.c            |    4 +--
 src/conf/node_device_conf.c        |    4 +--
 src/conf/secret_conf.c             |    4 +--
 src/conf/storage_conf.c            |    8 ++---
 src/esx/esx_driver.c               |    3 ++
 src/esx/esx_vi.c                   |   10 ++---
 src/esx/esx_vi_methods.c           |   68 +++++++++---------------------------
 src/esx/esx_vmx.c                  |   11 +-----
 src/libvirt_private.syms           |    1 +
 src/node_device/node_device_udev.c |    1 +
 src/opennebula/one_conf.c          |    3 +-
 src/openvz/openvz_conf.c           |    8 +++--
 src/openvz/openvz_driver.c         |    3 +-
 src/phyp/phyp_driver.c             |    5 +--
 src/qemu/qemu_conf.c               |   39 ++++++++++++++-------
 src/qemu/qemu_driver.c             |    5 ++-
 src/security/virt-aa-helper.c      |    9 +++--
 src/storage/storage_backend.c      |    3 +-
 src/uml/uml_conf.c                 |    4 +--
 src/util/buf.c                     |   13 +++++++
 src/util/buf.h                     |    1 +
 src/util/conf.c                    |    5 ++-
 src/util/logging.c                 |    8 +++-
 src/util/qparams.c                 |    1 +
 src/xen/xend_internal.c            |   14 +++++---
 src/xen/xm_internal.c              |   19 +++++-----
 tools/virsh.c                      |    4 +-
 33 files changed, 146 insertions(+), 150 deletions(-)

diff --git a/HACKING b/HACKING
index 548e2a0..3fb1113 100644
--- a/HACKING
+++ b/HACKING
@@ -293,10 +293,11 @@ eg typical usage is as follows:
      ...
      virBufferAddLit(&buf, "</domain>\n");
 
-     ....
+     ...
 
      if (virBufferError(&buf)) {
-         __virRaiseError(...);
+         virBufferFreeAndReset(&buf);
+         virReportOOMError(...);
          return NULL;
      }
 
diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index 1caa4ce..806970c 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -582,7 +582,7 @@ static int qemudListenUnix(struct qemud_server *server,
  cleanup:
     if (sock->fd)
         close(sock->fd);
-    free(sock);
+    VIR_FREE(sock);
     return -1;
 }
 
@@ -803,8 +803,8 @@ static int qemudInitPaths(struct qemud_server *server,
                   _("Resulting path too long for buffer in qemudInitPaths()"));
 
  cleanup:
-    free (dir_prefix);
-    free (sock_dir_prefix);
+    VIR_FREE(dir_prefix);
+    VIR_FREE(sock_dir_prefix);
     return ret;
 }
 
diff --git a/docs/hacking.html.in b/docs/hacking.html.in
index 94b7238..af63411 100644
--- a/docs/hacking.html.in
+++ b/docs/hacking.html.in
@@ -306,14 +306,15 @@
      ...
 
      virBufferAddLit(&amp;buf, "&lt;domain&gt;\n");
-     virBufferVSprint(&amp;buf, "  &lt;memory>%d&lt;/memory&gt;\n", memory);
+     virBufferVSprint(&amp;buf, "  &lt;memory&gt;%d&lt;/memory&gt;\n", memory);
      ...
      virBufferAddLit(&amp;buf, "&lt;/domain&gt;\n");
 
-     ....
+     ...
 
      if (virBufferError(&amp;buf)) {
-         __virRaiseError(...);
+         virBufferFreeAndReset(&amp;buf);
+         virReportOOMError(...);
          return NULL;
      }
 
diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c
index 6ebddf5..476bba6 100644
--- a/src/conf/capabilities.c
+++ b/src/conf/capabilities.c
@@ -769,8 +769,10 @@ virCapabilitiesFormatXML(virCapsPtr caps)
 
     virBufferAddLit(&xml, "</capabilities>\n");
 
-    if (virBufferError(&xml))
+    if (virBufferError(&xml)) {
+        virBufferFreeAndReset(&xml);
         return NULL;
+    }
 
     return virBufferContentAndReset(&xml);
 }
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 0a7eef7..355004b 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -3775,6 +3775,7 @@ virDomainCpuSetFormat(virConnectPtr conn, char *cpuset, int maxcpu)
     }
 
     if (virBufferError(&buf)) {
+        virBufferFreeAndReset(&buf);
         virReportOOMError(conn);
         return NULL;
     }
@@ -4570,7 +4571,7 @@ char *virDomainDefFormat(virConnectPtr conn,
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     unsigned char *uuid;
     char uuidstr[VIR_UUID_STRING_BUFLEN];
-    const char *type = NULL, *tmp;
+    const char *type = NULL;
     int n, allones = 1;
 
     if (!(type = virDomainVirtTypeToString(def->virtType))) {
@@ -4826,8 +4827,7 @@ char *virDomainDefFormat(virConnectPtr conn,
  no_memory:
     virReportOOMError(conn);
  cleanup:
-    tmp = virBufferContentAndReset(&buf);
-    VIR_FREE(tmp);
+    virBufferFreeAndReset(&buf);
     return NULL;
 }
 
@@ -4835,7 +4835,7 @@ char *virDomainObjFormat(virConnectPtr conn,
                          virDomainObjPtr obj,
                          int flags)
 {
-    char *config_xml = NULL, *xml = NULL;
+    char *config_xml = NULL;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     const char *monitorpath;
 
@@ -4887,8 +4887,7 @@ char *virDomainObjFormat(virConnectPtr conn,
 no_memory:
     virReportOOMError(conn);
 error:
-    xml = virBufferContentAndReset(&buf);
-    VIR_FREE(xml);
+    virBufferFreeAndReset(&buf);
     return NULL;
 }
 
diff --git a/src/conf/interface_conf.c b/src/conf/interface_conf.c
index 31abf12..3c99027 100644
--- a/src/conf/interface_conf.c
+++ b/src/conf/interface_conf.c
@@ -1208,7 +1208,7 @@ char *virInterfaceDefFormat(virConnectPtr conn,
                           const virInterfaceDefPtr def)
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
-    const char *type = NULL, *tmp;
+    const char *type = NULL;
 
     if ((def == NULL) ||
         ((def->name == NULL) && (def->type != VIR_INTERFACE_TYPE_VLAN))) {
@@ -1271,8 +1271,7 @@ char *virInterfaceDefFormat(virConnectPtr conn,
 no_memory:
     virReportOOMError(conn);
 cleanup:
-    tmp = virBufferContentAndReset(&buf);
-    VIR_FREE(tmp);
+    virBufferFreeAndReset(&buf);
     return NULL;
 }
 
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 62b3007..ab76f21 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -642,7 +642,6 @@ char *virNetworkDefFormat(virConnectPtr conn,
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     unsigned char *uuid;
-    char *tmp;
     char uuidstr[VIR_UUID_STRING_BUFLEN];
 
     virBufferAddLit(&buf, "<network>\n");
@@ -731,8 +730,7 @@ char *virNetworkDefFormat(virConnectPtr conn,
 
  no_memory:
     virReportOOMError(conn);
-    tmp = virBufferContentAndReset(&buf);
-    VIR_FREE(tmp);
+    virBufferFreeAndReset(&buf);
     return NULL;
 }
 
diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c
index 6003ab1..4bf71da 100644
--- a/src/conf/node_device_conf.c
+++ b/src/conf/node_device_conf.c
@@ -246,7 +246,6 @@ char *virNodeDeviceDefFormat(virConnectPtr conn,
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     virNodeDevCapsDefPtr caps;
-    char *tmp;
 
     virBufferAddLit(&buf, "<device>\n");
     virBufferEscapeString(&buf, "  <name>%s</name>\n", def->name);
@@ -464,8 +463,7 @@ char *virNodeDeviceDefFormat(virConnectPtr conn,
 
  no_memory:
     virReportOOMError(conn);
-    tmp = virBufferContentAndReset(&buf);
-    VIR_FREE(tmp);
+    virBufferFreeAndReset(&buf);
     return NULL;
 }
 
diff --git a/src/conf/secret_conf.c b/src/conf/secret_conf.c
index 21215b2..538fa91 100644
--- a/src/conf/secret_conf.c
+++ b/src/conf/secret_conf.c
@@ -303,7 +303,6 @@ virSecretDefFormat(virConnectPtr conn, const virSecretDefPtr def)
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     unsigned char *uuid;
     char uuidstr[VIR_UUID_STRING_BUFLEN];
-    char *tmp;
 
     virBufferVSprintf(&buf, "<secret ephemeral='%s' private='%s'>\n",
                       def->ephemeral ? "yes" : "no",
@@ -328,7 +327,6 @@ virSecretDefFormat(virConnectPtr conn, const virSecretDefPtr def)
  no_memory:
     virReportOOMError(conn);
  error:
-    tmp = virBufferContentAndReset(&buf);
-    VIR_FREE(tmp);
+    virBufferFreeAndReset(&buf);
     return NULL;
 }
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 065cd04..0aefa06 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -954,7 +954,7 @@ virStoragePoolDefFormat(virConnectPtr conn,
  no_memory:
     virReportOOMError(conn);
  cleanup:
-    free(virBufferContentAndReset(&buf));
+    virBufferFreeAndReset(&buf);
     return NULL;
 }
 
@@ -1292,7 +1292,6 @@ virStorageVolDefFormat(virConnectPtr conn,
                        virStorageVolDefPtr def) {
     virStorageVolOptionsPtr options;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
-    char *tmp;
 
     options = virStorageVolOptionsForPoolType(pool->type);
     if (options == NULL)
@@ -1351,8 +1350,7 @@ virStorageVolDefFormat(virConnectPtr conn,
  no_memory:
     virReportOOMError(conn);
  cleanup:
-    tmp = virBufferContentAndReset(&buf);
-    VIR_FREE(tmp);
+    virBufferFreeAndReset(&buf);
     return NULL;
 }
 
@@ -1742,7 +1740,7 @@ char *virStoragePoolSourceListFormat(virConnectPtr conn,
  no_memory:
     virReportOOMError(conn);
  cleanup:
-    free(virBufferContentAndReset(&buf));
+    virBufferFreeAndReset(&buf);
     return NULL;
 }
 
diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
index 5737fe9..ea464a3 100644
--- a/src/esx/esx_driver.c
+++ b/src/esx/esx_driver.c
@@ -2293,6 +2293,7 @@ esxDomainDumpXML(virDomainPtr domain, int flags)
     return xml;
 
   failure:
+    virBufferFreeAndReset(&buffer);
     VIR_FREE(xml);
 
     goto cleanup;
@@ -2723,6 +2724,8 @@ esxDomainDefineXML(virConnectPtr conn, const char *xml ATTRIBUTE_UNUSED)
     return domain;
 
   failure:
+    virBufferFreeAndReset(&buffer);
+
     domain = NULL;
 
     goto cleanup;
diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c
index 04860e2..5725b2f 100644
--- a/src/esx/esx_vi.c
+++ b/src/esx/esx_vi.c
@@ -532,7 +532,7 @@ esxVI_Context_DownloadFile(virConnectPtr conn, esxVI_Context *ctx,
     return 0;
 
   failure:
-    free(virBufferContentAndReset(&buffer));
+    virBufferFreeAndReset(&buffer);
 
     return -1;
 }
@@ -708,7 +708,7 @@ esxVI_Context_Execute(virConnectPtr conn, esxVI_Context *ctx,
     return 0;
 
   failure:
-    free(virBufferContentAndReset(&buffer));
+    virBufferFreeAndReset(&buffer);
     esxVI_Response_Free(response);
     esxVI_Fault_Free(&fault);
 
@@ -2065,7 +2065,7 @@ esxVI_StartSimpleVirtualMachineTask
     return result;
 
   failure:
-    free(virBufferContentAndReset(&buffer));
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -2124,9 +2124,7 @@ esxVI_SimpleVirtualMachineMethod(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
diff --git a/src/esx/esx_vi_methods.c b/src/esx/esx_vi_methods.c
index a80d1bf..06b5fd4 100644
--- a/src/esx/esx_vi_methods.c
+++ b/src/esx/esx_vi_methods.c
@@ -158,9 +158,7 @@ esxVI_Login(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -214,9 +212,7 @@ esxVI_Logout(virConnectPtr conn, esxVI_Context *ctx)
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -283,9 +279,7 @@ esxVI_SessionIsActive(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -352,9 +346,7 @@ esxVI_RetrieveProperties(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -451,9 +443,7 @@ esxVI_MigrateVM_Task(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -509,9 +499,7 @@ esxVI_ReconfigVM_Task(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -579,9 +567,7 @@ esxVI_RegisterVM_Task(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -630,9 +616,7 @@ esxVI_UnregisterVM(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -702,9 +686,7 @@ esxVI_CreateFilter(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -758,9 +740,7 @@ esxVI_DestroyPropertyFilter(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -824,9 +804,7 @@ esxVI_WaitForUpdates(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -928,9 +906,7 @@ esxVI_ValidateMigration(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -1003,9 +979,7 @@ esxVI_FindByIp(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -1089,9 +1063,7 @@ esxVI_FindByUuid(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -1165,9 +1137,7 @@ esxVI_QueryAvailablePerfMetric(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -1232,9 +1202,7 @@ esxVI_QueryPerfCounter(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
@@ -1299,9 +1267,7 @@ esxVI_QueryPerf(virConnectPtr conn, esxVI_Context *ctx,
     return result;
 
   failure:
-    if (request == NULL) {
-        request = virBufferContentAndReset(&buffer);
-    }
+    virBufferFreeAndReset(&buffer);
 
     result = -1;
 
diff --git a/src/esx/esx_vmx.c b/src/esx/esx_vmx.c
index 9a9fe0a..ff08fdb 100644
--- a/src/esx/esx_vmx.c
+++ b/src/esx/esx_vmx.c
@@ -2150,7 +2150,6 @@ esxVMX_FormatConfig(virConnectPtr conn, esxVI_Context *ctx,
     int sched_cpu_affinity_length;
     unsigned char zero[VIR_UUID_BUFLEN];
     virBuffer buffer = VIR_BUFFER_INITIALIZER;
-    char *vmx = NULL;
 
     memset(zero, 0, VIR_UUID_BUFLEN);
 
@@ -2373,16 +2372,10 @@ esxVMX_FormatConfig(virConnectPtr conn, esxVI_Context *ctx,
         goto failure;
     }
 
-    vmx = virBufferContentAndReset(&buffer);
-
-    return vmx;
+    return virBufferContentAndReset(&buffer);
 
   failure:
-    if (vmx == NULL) {
-        vmx = virBufferContentAndReset(&buffer);
-    }
-
-    VIR_FREE(vmx);
+    virBufferFreeAndReset(&buffer);
 
     return NULL;
 }
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c473d49..f182630 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -11,6 +11,7 @@ virBufferAddChar;
 virBufferContentAndReset;
 virBufferError;
 virBufferURIEncodeString;
+virBufferFreeAndReset;
 
 
 # caps.h
diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index 9b48052..10b4416 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -323,6 +323,7 @@ static int udevGenerateDeviceName(struct udev_device *device,
     }
 
     if (virBufferError(&buf)) {
+        virBufferFreeAndReset(&buf);
         VIR_ERROR("Buffer error when generating device name for device "
                   "with sysname '%s'\n", udev_device_get_sysname(device));
         ret = -1;
diff --git a/src/opennebula/one_conf.c b/src/opennebula/one_conf.c
index 415f832..a4768b1 100644
--- a/src/opennebula/one_conf.c
+++ b/src/opennebula/one_conf.c
@@ -278,7 +278,6 @@ char* xmlOneTemplate(virConnectPtr conn,virDomainDefPtr def)
 
 no_memory:
     virReportOOMError(conn);
-    char* tmp = virBufferContentAndReset(&buf);
-    VIR_FREE(tmp);
+    virBufferFreeAndReset(&buf);
     return NULL;
 };
diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c
index 33c2fd3..930f841 100644
--- a/src/openvz/openvz_conf.c
+++ b/src/openvz/openvz_conf.c
@@ -345,10 +345,12 @@ openvz_replace(const char* str,
         str_start = offset + from_len;
     }
 
-     virBufferAdd(&buf, str_start, strlen(str_start));
+    virBufferAdd(&buf, str_start, strlen(str_start));
 
-    if(virBufferError(&buf))
-      return NULL;
+    if (virBufferError(&buf)) {
+        virBufferFreeAndReset(&buf);
+        return NULL;
+    }
 
     return virBufferContentAndReset(&buf);
 }
diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c
index aac2eb8..1c0fccc 100644
--- a/src/openvz/openvz_driver.c
+++ b/src/openvz/openvz_driver.c
@@ -792,8 +792,7 @@ openvzDomainSetNetworkConfig(virConnectPtr conn,
     return 0;
 
 exit:
-    param = virBufferContentAndReset(&buf);
-    VIR_FREE(param);
+    virBufferFreeAndReset(&buf);
     return -1;
 }
 
diff --git a/src/phyp/phyp_driver.c b/src/phyp/phyp_driver.c
index ea9555a..c21d99d 100644
--- a/src/phyp/phyp_driver.c
+++ b/src/phyp/phyp_driver.c
@@ -486,13 +486,12 @@ phypExec(LIBSSH2_SESSION * session, char *cmd, int *exit_status,
 
   err:
     (*exit_status) = SSH_CMD_ERR;
-    char *cleanup_buf = virBufferContentAndReset(&tex_ret);
-
-    VIR_FREE(cleanup_buf);
+    virBufferFreeAndReset(&tex_ret);
     return NULL;
 
   exit:
     if (virBufferError(&tex_ret)) {
+        virBufferFreeAndReset(&tex_ret);
         virReportOOMError(conn);
         return NULL;
     }
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index f26e94d..09a298b 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1376,6 +1376,7 @@ qemuBuildHostNetStr(virConnectPtr conn,
                 type_sep = ','; /* dead-store, but leave it, in case... */
             }
             if (virBufferError(&buf)) {
+                virBufferFreeAndReset(&buf);
                 virReportOOMError(conn);
                 return -1;
             }
@@ -1859,8 +1860,10 @@ int qemudBuildCommandLine(virConnectPtr conn,
         virBuffer buf = VIR_BUFFER_INITIALIZER;
 
         qemudBuildCommandLineChrDevStr(monitor_chr, &buf);
-        if (virBufferError(&buf))
-            goto error;
+        if (virBufferError(&buf)) {
+            virBufferFreeAndReset(&buf);
+            goto no_memory;
+        }
 
         ADD_ARG_LIT("-monitor");
         ADD_ARG(virBufferContentAndReset(&buf));
@@ -2024,8 +2027,8 @@ int qemudBuildCommandLine(virConnectPtr conn,
             }
 
             if (virBufferError(&opt)) {
-                virReportOOMError(conn);
-                goto error;
+                virBufferFreeAndReset(&opt);
+                goto no_memory;
             }
 
             optstr = virBufferContentAndReset(&opt);
@@ -2147,8 +2150,10 @@ int qemudBuildCommandLine(virConnectPtr conn,
             virDomainChrDefPtr serial = def->serials[i];
 
             qemudBuildCommandLineChrDevStr(serial, &buf);
-            if (virBufferError(&buf))
-                goto error;
+            if (virBufferError(&buf)) {
+                virBufferFreeAndReset(&buf);
+                goto no_memory;
+            }
 
             ADD_ARG_LIT("-serial");
             ADD_ARG(virBufferContentAndReset(&buf));
@@ -2164,8 +2169,10 @@ int qemudBuildCommandLine(virConnectPtr conn,
             virDomainChrDefPtr parallel = def->parallels[i];
 
             qemudBuildCommandLineChrDevStr(parallel, &buf);
-            if (virBufferError(&buf))
-                goto error;
+            if (virBufferError(&buf)) {
+                virBufferFreeAndReset(&buf);
+                goto no_memory;
+            }
 
             ADD_ARG_LIT("-parallel");
             ADD_ARG(virBufferContentAndReset(&buf));
@@ -2190,8 +2197,10 @@ int qemudBuildCommandLine(virConnectPtr conn,
             }
 
             qemudBuildCommandLineChrDevChardevStr(channel, id, &buf);
-            if (virBufferError(&buf))
-                goto error;
+            if (virBufferError(&buf)) {
+                virBufferFreeAndReset(&buf);
+                goto no_memory;
+            }
 
             ADD_ARG_LIT("-chardev");
             ADD_ARG(virBufferContentAndReset(&buf));
@@ -2204,8 +2213,10 @@ int qemudBuildCommandLine(virConnectPtr conn,
 
             VIR_FREE(addr);
 
-            if (virBufferError(&buf))
-                goto error;
+            if (virBufferError(&buf)) {
+                virBufferFreeAndReset(&buf);
+                goto no_memory;
+            }
 
             ADD_ARG_LIT("-net");
             ADD_ARG(virBufferContentAndReset(&buf));
@@ -2263,8 +2274,10 @@ int qemudBuildCommandLine(virConnectPtr conn,
             virBufferVSprintf(&opt, "%d",
                               def->graphics[0]->data.vnc.port - 5900);
         }
-        if (virBufferError(&opt))
+        if (virBufferError(&opt)) {
+            virBufferFreeAndReset(&opt);
             goto no_memory;
+        }
 
         optstr = virBufferContentAndReset(&opt);
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index a4a87ac..d449669 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4308,8 +4308,11 @@ static char *qemuDomainXMLToNative(virConnectPtr conn,
         tmp++;
     }
 
-    if (virBufferError(&buf))
+    if (virBufferError(&buf)) {
+        virBufferFreeAndReset(&buf);
+        virReportOOMError(conn);
         goto cleanup;
+    }
 
     ret = virBufferContentAndReset(&buf);
 
diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c
index 62f0977..e1e141b 100644
--- a/src/security/virt-aa-helper.c
+++ b/src/security/virt-aa-helper.c
@@ -883,8 +883,9 @@ get_files(vahControl * ctl)
             goto clean;
 
     if (virBufferError(&buf)) {
-         vah_error(NULL, 0, "failed to allocate file buffer");
-         goto clean;
+        virBufferFreeAndReset(&buf);
+        vah_error(NULL, 0, "failed to allocate file buffer");
+        goto clean;
     }
 
     rc = 0;
@@ -1048,8 +1049,10 @@ main(int argc, char **argv)
         if (ctl->files)
             virBufferVSprintf(&buf, "%s", ctl->files);
 
-        if (virBufferError(&buf))
+        if (virBufferError(&buf)) {
+            virBufferFreeAndReset(&buf);
             vah_error(ctl, 1, "failed to allocate buffer");
+        }
 
         included_files = virBufferContentAndReset(&buf);
 
diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index bb926cd..dd977f4 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -445,8 +445,7 @@ cleanup:
             conn->secretDriver->undefine(secret);
         virSecretFree(secret);
     }
-    xml = virBufferContentAndReset(&buf);
-    VIR_FREE(xml);
+    virBufferFreeAndReset(&buf);
     virSecretDefFree(def);
     VIR_FREE(enc_secret);
     return ret;
diff --git a/src/uml/uml_conf.c b/src/uml/uml_conf.c
index 0ace58f..895a94d 100644
--- a/src/uml/uml_conf.c
+++ b/src/uml/uml_conf.c
@@ -163,7 +163,6 @@ umlBuildCommandLineNet(virConnectPtr conn,
                        virDomainNetDefPtr def,
                        int idx)
 {
-    char *ret;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
 
     /* General format:  ethNN=type,options */
@@ -265,8 +264,7 @@ umlBuildCommandLineNet(virConnectPtr conn,
     return virBufferContentAndReset(&buf);
 
 error:
-    ret = virBufferContentAndReset(&buf);
-    VIR_FREE(ret);
+    virBufferFreeAndReset(&buf);
     return NULL;
 }
 
diff --git a/src/util/buf.c b/src/util/buf.c
index 9681635..e683928 100644
--- a/src/util/buf.c
+++ b/src/util/buf.c
@@ -167,6 +167,19 @@ virBufferContentAndReset(const virBufferPtr buf)
 }
 
 /**
+ * virBufferFreeAndReset:
+ * @buf: the buffer to free and reset
+ *
+ * Frees the buffer content and resets the buffer structure.
+ */
+void virBufferFreeAndReset(const virBufferPtr buf)
+{
+    char *str = virBufferContentAndReset(buf);
+
+    VIR_FREE(str);
+}
+
+/**
  * virBufferError:
  * @buf: the buffer
  *
diff --git a/src/util/buf.h b/src/util/buf.h
index 94ee8a3..d9a1708 100644
--- a/src/util/buf.h
+++ b/src/util/buf.h
@@ -35,6 +35,7 @@ struct _virBuffer {
 #endif
 
 char *virBufferContentAndReset(const virBufferPtr buf);
+void virBufferFreeAndReset(const virBufferPtr buf);
 int virBufferError(const virBufferPtr buf);
 unsigned int virBufferUse(const virBufferPtr buf);
 void virBufferAdd(const virBufferPtr buf, const char *str, int len);
diff --git a/src/util/conf.c b/src/util/conf.c
index 8126f69..0c7e556 100644
--- a/src/util/conf.c
+++ b/src/util/conf.c
@@ -927,15 +927,15 @@ virConfWriteFile(const char *filename, virConfPtr conf)
     }
 
     if (virBufferError(&buf)) {
+        virBufferFreeAndReset(&buf);
         virReportOOMError(NULL);
         return -1;
     }
 
     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR );
     if (fd < 0) {
-        char *tmp = virBufferContentAndReset(&buf);
+        virBufferFreeAndReset(&buf);
         virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to open file"));
-        VIR_FREE(tmp);
         return -1;
     }
 
@@ -983,6 +983,7 @@ virConfWriteMem(char *memory, int *len, virConfPtr conf)
     }
 
     if (virBufferError(&buf)) {
+        virBufferFreeAndReset(&buf);
         virReportOOMError(NULL);
         return -1;
     }
diff --git a/src/util/logging.c b/src/util/logging.c
index 757f78c..6bd8469 100644
--- a/src/util/logging.c
+++ b/src/util/logging.c
@@ -867,8 +867,10 @@ char *virLogGetFilters(void) {
     }
     virLogUnlock();
 
-    if (virBufferError(&filterbuf))
+    if (virBufferError(&filterbuf)) {
+        virBufferFreeAndReset(&filterbuf);
         return NULL;
+    }
 
     return virBufferContentAndReset(&filterbuf);
 }
@@ -905,8 +907,10 @@ char *virLogGetOutputs(void) {
     }
     virLogUnlock();
 
-    if (virBufferError(&outputbuf))
+    if (virBufferError(&outputbuf)) {
+        virBufferFreeAndReset(&outputbuf);
         return NULL;
+    }
 
     return virBufferContentAndReset(&outputbuf);
 }
diff --git a/src/util/qparams.c b/src/util/qparams.c
index d0a84b3..9b96c2a 100644
--- a/src/util/qparams.c
+++ b/src/util/qparams.c
@@ -154,6 +154,7 @@ qparam_get_query (const struct qparam_set *ps)
     }
 
     if (virBufferError(&buf)) {
+        virBufferFreeAndReset(&buf);
         virReportOOMError(NULL);
         return NULL;
     }
diff --git a/src/xen/xend_internal.c b/src/xen/xend_internal.c
index d61e9e6..db21940 100644
--- a/src/xen/xend_internal.c
+++ b/src/xen/xend_internal.c
@@ -525,6 +525,7 @@ xend_op_ext(virConnectPtr xend, const char *path, char *error,
     }
 
     if (virBufferError(&buf)) {
+        virBufferFreeAndReset(&buf);
         virReportOOMError(NULL);
         return -1;
     }
@@ -5279,8 +5280,10 @@ xenDaemonFormatSxprChr(virConnectPtr conn,
         break;
     }
 
-    if (virBufferError(buf))
+    if (virBufferError(buf)) {
+        virReportOOMError(conn);
         return -1;
+    }
 
     return 0;
 }
@@ -5605,8 +5608,10 @@ xenDaemonFormatSxprSound(virConnectPtr conn,
         virBufferVSprintf(buf, "%s%s", i ? "," : "", str);
     }
 
-    if (virBufferError(buf))
+    if (virBufferError(buf)) {
+        virReportOOMError(conn);
         return -1;
+    }
 
     return 0;
 }
@@ -5878,14 +5883,13 @@ xenDaemonFormatSxpr(virConnectPtr conn,
 
     if (virBufferError(&buf)) {
         virReportOOMError(conn);
-        return NULL;
+        goto error;
     }
 
     return virBufferContentAndReset(&buf);
 
 error:
-    tmp = virBufferContentAndReset(&buf);
-    VIR_FREE(tmp);
+    virBufferFreeAndReset(&buf);
     return NULL;
 }
 
diff --git a/src/xen/xm_internal.c b/src/xen/xm_internal.c
index 2ee321f..7f43784 100644
--- a/src/xen/xm_internal.c
+++ b/src/xen/xm_internal.c
@@ -1735,6 +1735,7 @@ int xenXMDomainPinVcpu(virDomainPtr domain,
             }
 
     if (virBufferError(&mapbuf)) {
+        virBufferFreeAndReset(&mapbuf);
         virReportOOMError(domain->conn);
         goto cleanup;
     }
@@ -1965,7 +1966,6 @@ static int xenXMDomainConfigFormatDisk(virConnectPtr conn,
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     virConfValuePtr val, tmp;
-    char *str;
 
     if(disk->src) {
         if (disk->driverName) {
@@ -1996,7 +1996,7 @@ static int xenXMDomainConfigFormatDisk(virConnectPtr conn,
 
     if (virBufferError(&buf)) {
         virReportOOMError(conn);
-        return -1;
+        goto cleanup;
     }
 
     if (VIR_ALLOC(val) < 0) {
@@ -2017,8 +2017,7 @@ static int xenXMDomainConfigFormatDisk(virConnectPtr conn,
     return 0;
 
 cleanup:
-    str = virBufferContentAndReset(&buf);
-    VIR_FREE(str);
+    virBufferFreeAndReset(&buf);
     return -1;
 }
 
@@ -2029,7 +2028,6 @@ static int xenXMDomainConfigFormatNet(virConnectPtr conn,
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     virConfValuePtr val, tmp;
-    char *str;
     xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) conn->privateData;
 
     virBufferVSprintf(&buf, "mac=%02x:%02x:%02x:%02x:%02x:%02x",
@@ -2093,8 +2091,10 @@ static int xenXMDomainConfigFormatNet(virConnectPtr conn,
         virBufferVSprintf(&buf, ",vifname=%s",
                           net->ifname);
 
-    if (virBufferError(&buf))
+    if (virBufferError(&buf)) {
+        virReportOOMError(conn);
         goto cleanup;
+    }
 
     if (VIR_ALLOC(val) < 0) {
         virReportOOMError(conn);
@@ -2114,8 +2114,7 @@ static int xenXMDomainConfigFormatNet(virConnectPtr conn,
     return 0;
 
 cleanup:
-    str = virBufferContentAndReset(&buf);
-    VIR_FREE(str);
+    virBufferFreeAndReset(&buf);
     return -1;
 }
 
@@ -2444,8 +2443,10 @@ virConfPtr xenXMDomainConfigFormat(virConnectPtr conn,
                     virBufferVSprintf(&buf, ",keymap=%s",
                                       def->graphics[0]->data.vnc.keymap);
             }
-            if (virBufferError(&buf))
+            if (virBufferError(&buf)) {
+                virBufferFreeAndReset(&buf);
                 goto no_memory;
+            }
 
             vfbstr = virBufferContentAndReset(&buf);
 
diff --git a/tools/virsh.c b/tools/virsh.c
index 9faac35..94a99e9 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -3800,7 +3800,7 @@ static int buildPoolXML(const vshCmd *cmd, char **retname, char **xml) {
     return TRUE;
 
 cleanup:
-    free(virBufferContentAndReset(&buf));
+    virBufferFreeAndReset(&buf);
     return FALSE;
 }
 
@@ -4663,7 +4663,7 @@ cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd)
     }
 
  cleanup:
-    free(virBufferContentAndReset(&buf));
+    virBufferFreeAndReset(&buf);
     virStoragePoolFree(pool);
     return FALSE;
 }
-- 
1.6.0.4


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