[PATCH v3 22/34] Adapt to VIR_STRDUP and VIR_STRNDUP in src/storage/*

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

 



---
 src/storage/parthelper.c               |  5 ++---
 src/storage/storage_backend.c          | 22 ++++++----------------
 src/storage/storage_backend_disk.c     | 26 +++++++-------------------
 src/storage/storage_backend_fs.c       | 34 ++++++++++------------------------
 src/storage/storage_backend_iscsi.c    | 17 ++++-------------
 src/storage/storage_backend_logical.c  | 32 ++++++++++----------------------
 src/storage/storage_backend_mpath.c    |  5 +----
 src/storage/storage_backend_rbd.c      |  5 ++---
 src/storage/storage_backend_scsi.c     | 19 +++++++------------
 src/storage/storage_backend_sheepdog.c |  6 +-----
 src/storage/storage_driver.c           | 18 ++++++------------
 11 files changed, 56 insertions(+), 133 deletions(-)

diff --git a/src/storage/parthelper.c b/src/storage/parthelper.c
index c4af48f..038487a 100644
--- a/src/storage/parthelper.c
+++ b/src/storage/parthelper.c
@@ -44,6 +44,7 @@
 #include "virutil.h"
 #include "c-ctype.h"
 #include "configmake.h"
+#include "virstring.h"
 
 /* we don't need to include the full internal.h just for this */
 #define STREQ(a,b) (strcmp(a,b) == 0)
@@ -86,10 +87,8 @@ int main(int argc, char **argv)
     path = argv[1];
     if (virIsDevMapperDevice(path)) {
         partsep = "p";
-        canonical_path = strdup(path);
-        if (canonical_path == NULL) {
+        if (VIR_STRDUP_QUIET(canonical_path, path) < 0)
             return 2;
-        }
     } else {
         if (virFileResolveLink(path, &canonical_path) != 0) {
             return 2;
diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index b85a5a9..7a728e0 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -486,11 +486,8 @@ virStorageGenerateQcowEncryption(virConnectPtr conn,
         goto cleanup;
 
     def->usage_type = VIR_SECRET_USAGE_TYPE_VOLUME;
-    def->usage.volume = strdup(vol->target.path);
-    if (def->usage.volume == NULL) {
-        virReportOOMError();
+    if (VIR_STRDUP(def->usage.volume, vol->target.path) < 0)
         goto cleanup;
-    }
     xml = virSecretDefFormat(def);
     virSecretDefFree(def);
     def = NULL;
@@ -1261,12 +1258,11 @@ virStorageBackendUpdateVolTargetInfoFD(virStorageVolTargetPtr target,
             target->perms.label = NULL;
         }
     } else {
-        target->perms.label = strdup(filecon);
-        freecon(filecon);
-        if (target->perms.label == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(target->perms.label, filecon) < 0) {
+            freecon(filecon);
             return -1;
         }
+        freecon(filecon);
     }
 #else
     target->perms.label = NULL;
@@ -1451,10 +1447,7 @@ virStorageBackendStablePath(virStoragePoolObjPtr pool,
      * the original non-stable dev path
      */
 
-    stablepath = strdup(devpath);
-
-    if (stablepath == NULL)
-        virReportOOMError();
+    ignore_value(VIR_STRDUP(stablepath, devpath));
 
     return stablepath;
 }
@@ -1556,11 +1549,8 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
                 for (j = 0 ; j < nvars[i] ; j++) {
                     /* NB vars[0] is the full pattern, so we offset j by 1 */
                     p[vars[j+1].rm_eo] = '\0';
-                    if ((groups[ngroup++] =
-                         strdup(p + vars[j+1].rm_so)) == NULL) {
-                        virReportOOMError();
+                    if (VIR_STRDUP(groups[ngroup++], p + vars[j+1].rm_so) < 0)
                         goto cleanup;
-                    }
                 }
 
                 /* We're matching on the last regex, so callback time */
diff --git a/src/storage/storage_backend_disk.c b/src/storage/storage_backend_disk.c
index 6da2d12..52bd572 100644
--- a/src/storage/storage_backend_disk.c
+++ b/src/storage/storage_backend_disk.c
@@ -66,17 +66,13 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
          * strip the path to form a reasonable pool-unique name
          */
         tmp = strrchr(groups[0], '/');
-        if ((vol->name = strdup(tmp ? tmp + 1 : groups[0])) == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(vol->name, tmp ? tmp + 1 : groups[0]) < 0)
             return -1;
-        }
     }
 
     if (vol->target.path == NULL) {
-        if ((devpath = strdup(groups[0])) == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(devpath, groups[0]) < 0)
             return -1;
-        }
 
         /* Now figure out the stable path
          *
@@ -92,10 +88,8 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
 
     if (vol->key == NULL) {
         /* XXX base off a unique key of the underlying disk */
-        if ((vol->key = strdup(vol->target.path)) == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(vol->key, vol->target.path) < 0)
             return -1;
-        }
     }
 
     if (vol->source.extents == NULL) {
@@ -119,11 +113,9 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
             return -1;
         }
 
-        if ((vol->source.extents[0].path =
-             strdup(pool->def->source.devices[0].path)) == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(vol->source.extents[0].path,
+                       pool->def->source.devices[0].path) < 0)
             return -1;
-        }
     }
 
     /* Refresh allocation/capacity/perms */
@@ -485,10 +477,8 @@ virStorageBackendDiskPartFormat(virStoragePoolObjPtr pool,
                     return -1;
                 }
             }
-            if ((*partFormat = strdup(partedFormat)) == NULL) {
-                virReportOOMError();
+            if (VIR_STRDUP(*partFormat, partedFormat) < 0)
                 return -1;
-            }
         } else {
             /* create primary partition as long as it is possible
                and after that check if an extended partition exists
@@ -527,10 +517,8 @@ virStorageBackendDiskPartFormat(virStoragePoolObjPtr pool,
             }
         }
     } else {
-        if ((*partFormat = strdup("primary")) == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(*partFormat, "primary") < 0)
             return -1;
-        }
     }
     return 0;
 }
diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c
index 8288b1e..9b83e57 100644
--- a/src/storage/storage_backend_fs.c
+++ b/src/storage/storage_backend_fs.c
@@ -207,11 +207,9 @@ virStorageBackendFileSystemNetFindPoolSourcesFunc(virStoragePoolObjPtr pool ATTR
     }
     src->nhost = 1;
 
-    if (!(src->hosts[0].name = strdup(state->host)) ||
-        !(src->dir = strdup(path))) {
-        virReportOOMError();
+    if (VIR_STRDUP(src->hosts[0].name, state->host) < 0 ||
+        VIR_STRDUP(src->dir, path) < 0)
         goto cleanup;
-    }
     src->format = VIR_STORAGE_POOL_NETFS_NFS;
 
     ret = 0;
@@ -395,10 +393,8 @@ virStorageBackendFileSystemMount(virStoragePoolObjPtr pool) {
         }
 
     } else {
-        if ((src = strdup(pool->def->source.devices[0].path)) == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(src, pool->def->source.devices[0].path) < 0)
             return -1;
-        }
     }
 
     if (netauto)
@@ -571,10 +567,8 @@ virStorageBackendFileSystemProbe(const char *device,
         goto error;
     }
 
-    if ((libblkid_format = strdup(format)) == NULL) {
-        virReportOOMError();
+    if (VIR_STRDUP(libblkid_format, format) < 0)
         goto error;
-    }
 
     names[0] = libblkid_format;
     names[1] = NULL;
@@ -749,10 +743,8 @@ virStorageBackendFileSystemBuild(virConnectPtr conn ATTRIBUTE_UNUSED,
         goto error;
     }
 
-    if ((parent = strdup(pool->def->target.path)) == NULL) {
-        virReportOOMError();
+    if (VIR_STRDUP(parent, pool->def->target.path) < 0)
         goto error;
-    }
     if (!(p = strrchr(parent, '/'))) {
         virReportError(VIR_ERR_INVALID_ARG,
                        _("path '%s' is not absolute"),
@@ -833,8 +825,8 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn ATTRIBUTE_UNUSED,
         if (VIR_ALLOC(vol) < 0)
             goto no_memory;
 
-        if ((vol->name = strdup(ent->d_name)) == NULL)
-            goto no_memory;
+        if (VIR_STRDUP(vol->name, ent->d_name) < 0)
+            goto cleanup;
 
         vol->type = VIR_STORAGE_VOL_FILE;
         vol->target.format = VIR_STORAGE_FILE_RAW; /* Real value is filled in during probe */
@@ -843,8 +835,8 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn ATTRIBUTE_UNUSED,
                         vol->name) == -1)
             goto no_memory;
 
-        if ((vol->key = strdup(vol->target.path)) == NULL)
-            goto no_memory;
+        if (VIR_STRDUP(vol->key, vol->target.path) < 0)
+            goto cleanup;
 
         if ((ret = virStorageBackendProbeTarget(&vol->target,
                                                 &backingStore,
@@ -1011,13 +1003,7 @@ virStorageBackendFileSystemVolCreate(virConnectPtr conn ATTRIBUTE_UNUSED,
     }
 
     VIR_FREE(vol->key);
-    vol->key = strdup(vol->target.path);
-    if (vol->key == NULL) {
-        virReportOOMError();
-        return -1;
-    }
-
-    return 0;
+    return VIR_STRDUP(vol->key, vol->target.path);
 }
 
 static int createFileDir(virConnectPtr conn ATTRIBUTE_UNUSED,
diff --git a/src/storage/storage_backend_iscsi.c b/src/storage/storage_backend_iscsi.c
index cf25919..a38f6ff 100644
--- a/src/storage/storage_backend_iscsi.c
+++ b/src/storage/storage_backend_iscsi.c
@@ -125,13 +125,8 @@ virStorageBackendISCSIExtractSession(virStoragePoolObjPtr pool,
 {
     char **session = data;
 
-    if (STREQ(groups[1], pool->def->source.devices[0].path)) {
-        if ((*session = strdup(groups[0])) == NULL) {
-            virReportOOMError();
-            return -1;
-        }
-    }
-
+    if (STREQ(groups[1], pool->def->source.devices[0].path))
+        return VIR_STRDUP(*session, groups[0]);
     return 0;
 }
 
@@ -247,10 +242,8 @@ virStorageBackendIQNFound(const char *initiatoriqn,
                                  "of '%s'"), ISCSIADM);
                 goto out;
             }
-            *ifacename = strndup(line, token - line);
-            if (*ifacename == NULL) {
+            if (VIR_STRNDUP(*ifacename, line, token - line) < 0) {
                 ret = IQN_ERROR;
-                virReportOOMError();
                 goto out;
             }
             VIR_DEBUG("Found interface '%s' with IQN '%s'", *ifacename, iqn);
@@ -499,10 +492,8 @@ virStorageBackendISCSIGetTargets(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
     struct virStorageBackendISCSITargetList *list = data;
     char *target;
 
-    if (!(target = strdup(groups[1]))) {
-        virReportOOMError();
+    if (VIR_STRDUP(target, groups[1]) < 0)
         return -1;
-    }
 
     if (VIR_REALLOC_N(list->targets, list->ntargets + 1) < 0) {
         VIR_FREE(target);
diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c
index 316043f..aceb650 100644
--- a/src/storage/storage_backend_logical.c
+++ b/src/storage/storage_backend_logical.c
@@ -100,10 +100,8 @@ virStorageBackendLogicalMakeVol(virStoragePoolObjPtr pool,
         is_new_vol = true;
         vol->type = VIR_STORAGE_VOL_BLOCK;
 
-        if ((vol->name = strdup(groups[0])) == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(vol->name, groups[0]) < 0)
             goto cleanup;
-        }
 
         if (VIR_REALLOC_N(pool->volumes.objs,
                           pool->volumes.count + 1)) {
@@ -137,11 +135,8 @@ virStorageBackendLogicalMakeVol(virStoragePoolObjPtr pool,
         vol->backingStore.format = VIR_STORAGE_POOL_LOGICAL_LVM2;
     }
 
-    if (vol->key == NULL &&
-        (vol->key = strdup(groups[2])) == NULL) {
-        virReportOOMError();
+    if (!vol->key && VIR_STRDUP(vol->key, groups[2]) < 0)
         goto cleanup;
-    }
 
     if (virStorageBackendUpdateVolInfo(vol, 1) < 0)
         goto cleanup;
@@ -179,7 +174,8 @@ virStorageBackendLogicalMakeVol(virStoragePoolObjPtr pool,
     }
 
     /* Now parse the "devices" field separately */
-    regex = strdup(regex_unit);
+    if (VIR_STRDUP(regex, regex_unit) < 0)
+        goto cleanup;
 
     for (i = 1; i < nextents; i++) {
         if (VIR_REALLOC_N(regex, strlen(regex) + strlen(regex_unit) + 2) < 0) {
@@ -229,23 +225,19 @@ virStorageBackendLogicalMakeVol(virStoragePoolObjPtr pool,
     /* vars[0] is skipped */
     for (i = 0; i < nextents; i++) {
         int j, len;
-        const char *offset_str = NULL;
+        char *offset_str = NULL;
 
         j = (i * 2) + 1;
         len = vars[j].rm_eo - vars[j].rm_so;
         p[vars[j].rm_eo] = '\0';
 
-        if ((vol->source.extents[vol->source.nextent].path =
-            strndup(p + vars[j].rm_so, len)) == NULL) {
-            virReportOOMError();
+        if (VIR_STRNDUP(vol->source.extents[vol->source.nextent].path,
+                        p + vars[j].rm_so, len) < 0)
             goto cleanup;
-        }
 
         len = vars[j + 1].rm_eo - vars[j + 1].rm_so;
-        if (!(offset_str = strndup(p + vars[j + 1].rm_so, len))) {
-            virReportOOMError();
+        if (VIR_STRNDUP(offset_str, p + vars[j + 1].rm_so, len) < 0)
             goto cleanup;
-        }
 
         if (virStrToLong_ull(offset_str, NULL, 10, &offset) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -358,13 +350,9 @@ virStorageBackendLogicalFindPoolSourcesFunc(virStoragePoolObjPtr pool ATTRIBUTE_
     virStoragePoolSourceDevicePtr dev;
     virStoragePoolSource *thisSource;
 
-    pvname = strdup(groups[0]);
-    vgname = strdup(groups[1]);
-
-    if (pvname == NULL || vgname == NULL) {
-        virReportOOMError();
+    if (VIR_STRDUP(pvname, groups[0]) < 0 ||
+        VIR_STRDUP(vgname, groups[1]) < 0)
         goto err_no_memory;
-    }
 
     thisSource = NULL;
     for (i = 0 ; i < sourceList->nsources; i++) {
diff --git a/src/storage/storage_backend_mpath.c b/src/storage/storage_backend_mpath.c
index 6e742f7..357c346 100644
--- a/src/storage/storage_backend_mpath.c
+++ b/src/storage/storage_backend_mpath.c
@@ -99,11 +99,8 @@ virStorageBackendMpathNewVol(virStoragePoolObjPtr pool,
     }
 
     /* XXX should use logical unit's UUID instead */
-    vol->key = strdup(vol->target.path);
-    if (vol->key == NULL) {
-        virReportOOMError();
+    if (VIR_STRDUP(vol->key, vol->target.path) < 0)
         goto cleanup;
-    }
 
     if (VIR_REALLOC_N(pool->volumes.objs,
                       pool->volumes.count + 1) < 0) {
diff --git a/src/storage/storage_backend_rbd.c b/src/storage/storage_backend_rbd.c
index 99024e3..953a8ee 100644
--- a/src/storage/storage_backend_rbd.c
+++ b/src/storage/storage_backend_rbd.c
@@ -334,10 +334,9 @@ static int virStorageBackendRBDRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
         if (VIR_ALLOC(vol) < 0)
             goto out_of_memory;
 
-        vol->name = strdup(name);
-        if (vol->name == NULL) {
+        if (VIR_STRDUP(vol->name, name) < 0) {
             VIR_FREE(vol);
-            goto out_of_memory;
+            goto cleanup;
         }
 
         name += strlen(name) + 1;
diff --git a/src/storage/storage_backend_scsi.c b/src/storage/storage_backend_scsi.c
index 154a56a..bd6a2a9 100644
--- a/src/storage/storage_backend_scsi.c
+++ b/src/storage/storage_backend_scsi.c
@@ -188,8 +188,7 @@ virStorageBackendSCSISerial(const char *dev)
             *nl = '\0';
     } else {
         VIR_FREE(serial);
-        if (!(serial = strdup(dev)))
-            virReportOOMError();
+        ignore_value(VIR_STRDUP(serial, dev));
     }
 
 #ifdef WITH_UDEV
@@ -333,10 +332,7 @@ getNewStyleBlockDevice(const char *lun_path,
             continue;
         }
 
-        *block_device = strdup(block_dirent->d_name);
-
-        if (*block_device == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(*block_device, block_dirent->d_name) < 0) {
             closedir(block_dir);
             retval = -1;
             goto out;
@@ -373,10 +369,7 @@ getOldStyleBlockDevice(const char *lun_path ATTRIBUTE_UNUSED,
         retval = -1;
     } else {
         blockp++;
-        *block_device = strdup(blockp);
-
-        if (*block_device == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(*block_device, blockp) < 0) {
             retval = -1;
             goto out;
         }
@@ -630,8 +623,10 @@ getAdapterName(virStoragePoolSourceAdapter adapter)
 {
     char *name = NULL;
 
-    if (adapter.type != VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST)
-        return strdup(adapter.data.name);
+    if (adapter.type != VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
+        ignore_value(VIR_STRDUP(name, adapter.data.name));
+        return name;
+    }
 
     if (!(name = virGetFCHostNameByWWN(NULL,
                                        adapter.data.fchost.wwnn,
diff --git a/src/storage/storage_backend_sheepdog.c b/src/storage/storage_backend_sheepdog.c
index 2e62e6d..b4d7b8e 100644
--- a/src/storage/storage_backend_sheepdog.c
+++ b/src/storage/storage_backend_sheepdog.c
@@ -269,11 +269,7 @@ virStorageBackendSheepdogRefreshVol(virConnectPtr conn ATTRIBUTE_UNUSED,
     }
 
     VIR_FREE(vol->target.path);
-    if (!(vol->target.path = strdup(vol->name))) {
-        virReportOOMError();
-        goto cleanup;
-    }
-
+    ignore_value(VIR_STRDUP(vol->target.path, vol->name));
 cleanup:
     virCommandFree(cmd);
     return ret;
diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index 990f0b1..7cf8193 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -144,8 +144,8 @@ storageStateInitialize(bool privileged,
     storageDriverLock(driverState);
 
     if (privileged) {
-        if ((base = strdup(SYSCONFDIR "/libvirt")) == NULL)
-            goto out_of_memory;
+        if (VIR_STRDUP(base, SYSCONFDIR "/libvirt") < 0)
+            goto error;
     } else {
         base = virGetUserConfigDirectory();
         if (!base)
@@ -336,9 +336,8 @@ storageConnectListStoragePools(virConnectPtr conn,
     for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
         virStoragePoolObjLock(driver->pools.objs[i]);
         if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
-            if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
+            if (VIR_STRDUP(names[got], driver->pools.objs[i]->def->name) < 0) {
                 virStoragePoolObjUnlock(driver->pools.objs[i]);
-                virReportOOMError();
                 goto cleanup;
             }
             got++;
@@ -384,9 +383,8 @@ storageConnectListDefinedStoragePools(virConnectPtr conn,
     for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
         virStoragePoolObjLock(driver->pools.objs[i]);
         if (!virStoragePoolObjIsActive(driver->pools.objs[i])) {
-            if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
+            if (VIR_STRDUP(names[got], driver->pools.objs[i]->def->name) < 0) {
                 virStoragePoolObjUnlock(driver->pools.objs[i]);
-                virReportOOMError();
                 goto cleanup;
             }
             got++;
@@ -1117,10 +1115,8 @@ storagePoolListVolumes(virStoragePoolPtr obj,
     }
 
     for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) {
-        if ((names[n++] = strdup(pool->volumes.objs[i]->name)) == NULL) {
-            virReportOOMError();
+        if (VIR_STRDUP(names[n++], pool->volumes.objs[i]->name) < 0)
             goto cleanup;
-        }
     }
 
     virStoragePoolObjUnlock(pool);
@@ -2340,9 +2336,7 @@ storageVolGetPath(virStorageVolPtr obj) {
         goto cleanup;
     }
 
-    ret = strdup(vol->target.path);
-    if (ret == NULL)
-        virReportOOMError();
+    ignore_value(VIR_STRDUP(ret, vol->target.path));
 
 cleanup:
     if (pool)
-- 
1.8.1.5

--
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]