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

[libvirt] [PATCH 2/4] phyp: avoid memory leaks in return values



* src/phyp/phyp_driver.c (phypCreateServerSCSIAdapter)
(phypAttachDevice, phypVolumeLookupByPath, phypVolumeGetPath):
Avoid leaking phypExec result.
(phypBuildStoragePool): Avoid NULL dereference.
(phypInterfaceDestroy): Avoid redundant free.
---
 src/phyp/phyp_driver.c |   49 +++++++++++++++++++++++++++++------------------
 1 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/src/phyp/phyp_driver.c b/src/phyp/phyp_driver.c
index ec2ac09..2f00461 100644
--- a/src/phyp/phyp_driver.c
+++ b/src/phyp/phyp_driver.c
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (C) 2010-2011 Red Hat, Inc.
  * Copyright IBM Corp. 2009
@@ -1853,6 +1852,7 @@ phypCreateServerSCSIAdapter(virConnectPtr conn)
     }
     cmd = virBufferContentAndReset(&buf);

+    VIR_FREE(ret);
     ret = phypExec(session, cmd, &exit_status, conn);

     if (exit_status < 0 || ret == NULL)
@@ -1861,6 +1861,7 @@ phypCreateServerSCSIAdapter(virConnectPtr conn)
     /* Finally I add the new scsi adapter to VIOS using the same slot
      * I used in the VIOS configuration.
      * */
+    VIR_FREE(ret);
     virBufferAddLit(&buf, "chhwres -r virtualio --rsubtype scsi");
     if (system_type == HMC)
         virBufferVSprintf(&buf, " -m %s", managed_system);
@@ -2047,6 +2048,7 @@ phypAttachDevice(virDomainPtr domain, const char *xml)

     /* Let's get the slot number for the adapter we just created
      * */
+    VIR_FREE(ret);
     virBufferAddLit(&buf, "lshwres -r virtualio --rsubtype scsi");
     if (system_type == HMC)
         virBufferVSprintf(&buf, " -m %s", managed_system);
@@ -2071,6 +2073,7 @@ phypAttachDevice(virDomainPtr domain, const char *xml)
     /* Listing all the virtual_scsi_adapter interfaces, the new adapter must
      * be appended to this list
      * */
+    VIR_FREE(ret);
     virBufferAddLit(&buf, "lssyscfg");
     if (system_type == HMC)
         virBufferVSprintf(&buf, " -m %s", managed_system);
@@ -2108,6 +2111,7 @@ phypAttachDevice(virDomainPtr domain, const char *xml)
     }
     cmd = virBufferContentAndReset(&buf);

+    VIR_FREE(ret);
     ret = phypExec(session, cmd, &exit_status, conn);

     if (virStrToLong_i(ret, &char_ptr, 10, &slot) == -1)
@@ -2116,6 +2120,7 @@ phypAttachDevice(virDomainPtr domain, const char *xml)
     /* Finally I add the new scsi adapter to VIOS using the same slot
      * I used in the VIOS configuration.
      * */
+    VIR_FREE(ret);
     virBufferAddLit(&buf, "chhwres -r virtualio --rsubtype scsi");
     if (system_type == HMC)
         virBufferVSprintf(&buf, " -m %s", managed_system);
@@ -2542,9 +2547,11 @@ phypVolumeLookupByPath(virConnectPtr conn, const char *volname)
     int vios_id = phyp_driver->vios_id;
     int exit_status = 0;
     char *cmd = NULL;
-    char *spname = NULL;
+    char *ret = NULL;
     char *key = NULL;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
+    char *char_ptr;
+    virStorageVolPtr result = NULL;

     if (system_type == HMC)
         virBufferVSprintf(&buf, "viosvrcmd -m %s --id %d -c '",
@@ -2564,25 +2571,30 @@ phypVolumeLookupByPath(virConnectPtr conn, const char *volname)
     }
     cmd = virBufferContentAndReset(&buf);

-    spname = phypExec(session, cmd, &exit_status, conn);
+    ret = phypExec(session, cmd, &exit_status, conn);

-    if (exit_status < 0 || spname == NULL)
-        return NULL;
+    if (exit_status < 0 || ret == NULL)
+        goto cleanup;

-    char *char_ptr = strchr(spname, '\n');
+    char_ptr = strchr(ret, '\n');

     if (char_ptr)
         *char_ptr = '\0';

     if (VIR_ALLOC_N(key, MAX_KEY_SIZE) < 0) {
         virReportOOMError();
-        return NULL;
+        goto cleanup;
     }

     if (phypVolumeGetKey(conn, key, volname) == -1)
-        return NULL;
+        goto cleanup;
+
+    result = virGetStorageVol(conn, ret, volname, key);

-    return virGetStorageVol(conn, spname, volname, key);
+cleanup:
+    VIR_FREE(ret);
+    VIR_FREE(key);
+    return result;
 }

 static int
@@ -2738,7 +2750,7 @@ phypVolumeGetPath(virStorageVolPtr vol)
     int vios_id = phyp_driver->vios_id;
     int exit_status = 0;
     char *cmd = NULL;
-    char *sp = NULL;
+    char *ret = NULL;
     char *path = NULL;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     char *char_ptr;
@@ -2763,20 +2775,20 @@ phypVolumeGetPath(virStorageVolPtr vol)
     }
     cmd = virBufferContentAndReset(&buf);

-    sp = phypExec(session, cmd, &exit_status, conn);
+    ret = phypExec(session, cmd, &exit_status, conn);

-    if (exit_status < 0 || sp == NULL)
+    if (exit_status < 0 || ret == NULL)
         goto err;

-    char_ptr = strchr(sp, '\n');
+    char_ptr = strchr(ret, '\n');

     if (char_ptr)
         *char_ptr = '\0';

-    pv = phypVolumeGetPhysicalVolumeByStoragePool(vol, sp);
+    pv = phypVolumeGetPhysicalVolumeByStoragePool(vol, ret);

     if (pv) {
-        if (virAsprintf(&path, "/%s/%s/%s", pv, sp, vol->name) < 0) {
+        if (virAsprintf(&path, "/%s/%s/%s", pv, ret, vol->name) < 0) {
             virReportOOMError();
             goto err;
         }
@@ -2785,11 +2797,12 @@ phypVolumeGetPath(virStorageVolPtr vol)
     }

     VIR_FREE(cmd);
+    VIR_FREE(ret);
     return path;

   err:
     VIR_FREE(cmd);
-    VIR_FREE(sp);
+    VIR_FREE(ret);
     VIR_FREE(path);
     return NULL;

@@ -3013,7 +3026,7 @@ phypBuildStoragePool(virConnectPtr conn, virStoragePoolDefPtr def)
     ret = phypExec(session, cmd, &exit_status, conn);

     if (exit_status < 0) {
-        VIR_ERROR(_("Unable to create Storage Pool: %s"), ret);
+        VIR_ERROR(_("Unable to create Storage Pool: %s"), NULLSTR(ret));
         goto err;
     }

@@ -3357,8 +3370,6 @@ phypInterfaceDestroy(virInterfacePtr iface,
     }
     cmd = virBufferContentAndReset(&buf);

-    VIR_FREE(ret);
-
     ret = phypExec(session, cmd, &exit_status, iface->conn);

     if (exit_status < 0 || ret == NULL)
-- 
1.7.4.2


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