[libvirt PATCH 2/9] esx: use g_autofree when made possible by reducing scope

Laine Stump laine at redhat.com
Fri Feb 12 22:07:10 UTC 2021


These strings were being VIR_FREEd multiple times because they were
defined at the top of a function, but then set each time through a
loop. But they are only used inside that loop, so they can be
converted to use g_autofree if their definition is also placed inside
that loop.

Signed-off-by: Laine Stump <laine at redhat.com>
---
 src/esx/esx_driver.c                | 13 ++++---------
 src/esx/esx_storage_backend_iscsi.c | 12 ++++--------
 src/esx/esx_storage_backend_vmfs.c  | 18 ++++--------------
 src/esx/esx_vi.c                    |  5 +----
 4 files changed, 13 insertions(+), 35 deletions(-)

diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
index df257341b8..8afec464dd 100644
--- a/src/esx/esx_driver.c
+++ b/src/esx/esx_driver.c
@@ -1391,7 +1391,6 @@ esxDomainLookupByID(virConnectPtr conn, int id)
     esxVI_ObjectContent *virtualMachine = NULL;
     esxVI_VirtualMachinePowerState powerState;
     int id_candidate = -1;
-    char *name_candidate = NULL;
     unsigned char uuid_candidate[VIR_UUID_BUFLEN];
     virDomainPtr domain = NULL;
 
@@ -1410,6 +1409,8 @@ esxDomainLookupByID(virConnectPtr conn, int id)
 
     for (virtualMachine = virtualMachineList; virtualMachine;
          virtualMachine = virtualMachine->_next) {
+        g_autofree char *name_candidate = NULL;
+
         if (esxVI_GetVirtualMachinePowerState(virtualMachine,
                                               &powerState) < 0) {
             goto cleanup;
@@ -1419,8 +1420,6 @@ esxDomainLookupByID(virConnectPtr conn, int id)
         if (powerState == esxVI_VirtualMachinePowerState_PoweredOff)
             continue;
 
-        VIR_FREE(name_candidate);
-
         if (esxVI_GetVirtualMachineIdentity(virtualMachine,
                                             &id_candidate, &name_candidate,
                                             uuid_candidate) < 0) {
@@ -1444,8 +1443,6 @@ esxDomainLookupByID(virConnectPtr conn, int id)
  cleanup:
     esxVI_String_Free(&propertyNameList);
     esxVI_ObjectContent_Free(&virtualMachineList);
-    VIR_FREE(name_candidate);
-
     return domain;
 }
 
@@ -4754,7 +4751,6 @@ esxConnectListAllDomains(virConnectPtr conn,
     esxVI_AutoStartPowerInfo *powerInfoList = NULL;
     esxVI_AutoStartPowerInfo *powerInfo = NULL;
     esxVI_VirtualMachineSnapshotTree *rootSnapshotTreeList = NULL;
-    char *name = NULL;
     int id;
     unsigned char uuid[VIR_UUID_BUFLEN];
     int count = 0;
@@ -4839,9 +4835,9 @@ esxConnectListAllDomains(virConnectPtr conn,
 
     for (virtualMachine = virtualMachineList; virtualMachine;
          virtualMachine = virtualMachine->_next) {
-        if (needIdentity) {
-            VIR_FREE(name);
+        g_autofree char *name = NULL;
 
+        if (needIdentity) {
             if (esxVI_GetVirtualMachineIdentity(virtualMachine, &id,
                                                 &name, uuid) < 0) {
                 goto cleanup;
@@ -4959,7 +4955,6 @@ esxConnectListAllDomains(virConnectPtr conn,
         VIR_FREE(doms);
     }
 
-    VIR_FREE(name);
     esxVI_AutoStartDefaults_Free(&autoStartDefaults);
     esxVI_AutoStartPowerInfo_Free(&powerInfoList);
     esxVI_String_Free(&propertyNameList);
diff --git a/src/esx/esx_storage_backend_iscsi.c b/src/esx/esx_storage_backend_iscsi.c
index 4d16ba2520..41bb9f6094 100644
--- a/src/esx/esx_storage_backend_iscsi.c
+++ b/src/esx/esx_storage_backend_iscsi.c
@@ -494,7 +494,6 @@ esxStorageVolLookupByPath(virConnectPtr conn, const char *path)
     esxVI_ScsiLun *scsiLunList = NULL;
     esxVI_ScsiLun *scsiLun;
     esxVI_HostScsiDisk *hostScsiDisk = NULL;
-    char *poolName = NULL;
     /* VIR_CRYPTO_HASH_SIZE_MD5 = VIR_UUID_BUFLEN = 16 */
     unsigned char md5[VIR_CRYPTO_HASH_SIZE_MD5];
     char uuid_string[VIR_UUID_STRING_BUFLEN] = "";
@@ -503,11 +502,12 @@ esxStorageVolLookupByPath(virConnectPtr conn, const char *path)
         goto cleanup;
 
     for (scsiLun = scsiLunList; scsiLun; scsiLun = scsiLun->_next) {
+        g_autofree char *poolName = NULL;
+
         hostScsiDisk = esxVI_HostScsiDisk_DynamicCast(scsiLun);
 
         if (hostScsiDisk && STREQ(hostScsiDisk->devicePath, path)) {
             /* Found matching device */
-            VIR_FREE(poolName);
 
             if (esxVI_LookupStoragePoolNameByScsiLunKey(priv->primary,
                                                         hostScsiDisk->key,
@@ -527,8 +527,6 @@ esxStorageVolLookupByPath(virConnectPtr conn, const char *path)
 
  cleanup:
     esxVI_ScsiLun_Free(&scsiLunList);
-    VIR_FREE(poolName);
-
     return volume;
 }
 
@@ -539,7 +537,6 @@ esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
 {
     virStorageVolPtr volume = NULL;
     esxPrivate *priv = conn->privateData;
-    char *poolName = NULL;
     esxVI_ScsiLun *scsiLunList = NULL;
     esxVI_ScsiLun *scsiLun;
     /* VIR_CRYPTO_HASH_SIZE_MD5 = VIR_UUID_BUFLEN = 16 */
@@ -555,6 +552,8 @@ esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
 
     for (scsiLun = scsiLunList; scsiLun;
          scsiLun = scsiLun->_next) {
+        g_autofree char *poolName = NULL;
+
         memset(uuid_string, '\0', sizeof(uuid_string));
         memset(md5, '\0', sizeof(md5));
 
@@ -564,7 +563,6 @@ esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
 
         if (STREQ(key, uuid_string)) {
             /* Found matching UUID */
-            VIR_FREE(poolName);
 
             if (esxVI_LookupStoragePoolNameByScsiLunKey(priv->primary,
                                                         scsiLun->key,
@@ -581,8 +579,6 @@ esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
 
  cleanup:
     esxVI_ScsiLun_Free(&scsiLunList);
-    VIR_FREE(poolName);
-
     return volume;
 }
 
diff --git a/src/esx/esx_storage_backend_vmfs.c b/src/esx/esx_storage_backend_vmfs.c
index 9466ec81cb..63959ec237 100644
--- a/src/esx/esx_storage_backend_vmfs.c
+++ b/src/esx/esx_storage_backend_vmfs.c
@@ -598,7 +598,6 @@ esxStoragePoolListVolumes(virStoragePoolPtr pool, char **const names,
     esxVI_HostDatastoreBrowserSearchResults *searchResultsList = NULL;
     esxVI_HostDatastoreBrowserSearchResults *searchResults = NULL;
     esxVI_FileInfo *fileInfo = NULL;
-    char *directoryAndFileName = NULL;
     size_t length;
     int count = 0;
     size_t i;
@@ -619,7 +618,7 @@ esxStoragePoolListVolumes(virStoragePoolPtr pool, char **const names,
     /* Interpret search result */
     for (searchResults = searchResultsList; searchResults;
          searchResults = searchResults->_next) {
-        VIR_FREE(directoryAndFileName);
+        g_autofree char *directoryAndFileName = NULL;
 
         if (esxUtil_ParseDatastorePath(searchResults->folderPath, NULL, NULL,
                                        &directoryAndFileName) < 0) {
@@ -659,8 +658,6 @@ esxStoragePoolListVolumes(virStoragePoolPtr pool, char **const names,
     }
 
     esxVI_HostDatastoreBrowserSearchResults_Free(&searchResultsList);
-    VIR_FREE(directoryAndFileName);
-
     return count;
 }
 
@@ -730,12 +727,9 @@ esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
     char *datastoreName = NULL;
     esxVI_HostDatastoreBrowserSearchResults *searchResultsList = NULL;
     esxVI_HostDatastoreBrowserSearchResults *searchResults = NULL;
-    char *directoryAndFileName = NULL;
     size_t length;
-    char *datastorePath = NULL;
     char *volumeName = NULL;
     esxVI_FileInfo *fileInfo = NULL;
-    char *uuid_string = NULL;
     char key_candidate[VIR_UUID_STRING_BUFLEN] = "";
 
     if (STRPREFIX(key, "[")) {
@@ -777,7 +771,7 @@ esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
         /* Interpret search result */
         for (searchResults = searchResultsList; searchResults;
              searchResults = searchResults->_next) {
-            VIR_FREE(directoryAndFileName);
+            g_autofree char *directoryAndFileName = NULL;
 
             if (esxUtil_ParseDatastorePath(searchResults->folderPath, NULL,
                                            NULL, &directoryAndFileName) < 0) {
@@ -795,7 +789,8 @@ esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
             /* Build datastore path and query the UUID */
             for (fileInfo = searchResults->file; fileInfo;
                  fileInfo = fileInfo->_next) {
-                VIR_FREE(datastorePath);
+                g_autofree char *datastorePath = NULL;
+                g_autofree char *uuid_string = NULL;
 
                 if (length < 1) {
                     volumeName = g_strdup(fileInfo->path);
@@ -811,8 +806,6 @@ esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
                     continue;
                 }
 
-                VIR_FREE(uuid_string);
-
                 if (esxVI_QueryVirtualDiskUuid
                       (priv->primary, datastorePath,
                        priv->primary->datacenter->_reference,
@@ -838,10 +831,7 @@ esxStorageVolLookupByKey(virConnectPtr conn, const char *key)
     esxVI_String_Free(&propertyNameList);
     esxVI_ObjectContent_Free(&datastoreList);
     esxVI_HostDatastoreBrowserSearchResults_Free(&searchResultsList);
-    VIR_FREE(directoryAndFileName);
-    VIR_FREE(datastorePath);
     VIR_FREE(volumeName);
-    VIR_FREE(uuid_string);
 
     return volume;
 }
diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c
index 7ff43adaaf..987259be4b 100644
--- a/src/esx/esx_vi.c
+++ b/src/esx/esx_vi.c
@@ -2761,7 +2761,6 @@ esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name,
     esxVI_String *completePropertyNameList = NULL;
     esxVI_ObjectContent *virtualMachineList = NULL;
     esxVI_ObjectContent *candidate = NULL;
-    char *name_candidate = NULL;
 
     ESX_VI_CHECK_ARG_LIST(virtualMachine);
 
@@ -2775,7 +2774,7 @@ esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name,
 
     for (candidate = virtualMachineList; candidate;
          candidate = candidate->_next) {
-        VIR_FREE(name_candidate);
+        g_autofree char *name_candidate = NULL;
 
         if (esxVI_GetVirtualMachineIdentity(candidate, NULL, &name_candidate,
                                             NULL) < 0) {
@@ -2802,8 +2801,6 @@ esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name,
  cleanup:
     esxVI_String_Free(&completePropertyNameList);
     esxVI_ObjectContent_Free(&virtualMachineList);
-    VIR_FREE(name_candidate);
-
     return result;
 }
 
-- 
2.29.2




More information about the libvir-list mailing list