[libvirt] [PATCH v2 12/37] Adapt to VIR_STRDUP in src/node_device/*

Michal Privoznik mprivozn at redhat.com
Mon Apr 29 13:50:34 UTC 2013


---
 src/node_device/node_device_driver.c |  28 ++++------
 src/node_device/node_device_hal.c    |  13 ++---
 src/node_device/node_device_udev.c   | 102 +++++++++++++++--------------------
 3 files changed, 60 insertions(+), 83 deletions(-)

diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c
index 95df2e5..2ef05ab 100644
--- a/src/node_device/node_device_driver.c
+++ b/src/node_device/node_device_driver.c
@@ -36,6 +36,7 @@
 #include "node_device_conf.h"
 #include "node_device_hal.h"
 #include "node_device_driver.h"
+#include "virstring.h"
 
 #define VIR_FROM_THIS VIR_FROM_NODEDEV
 
@@ -88,12 +89,9 @@ static int update_driver_name(virNodeDeviceObjPtr dev)
     }
 
     p = strrchr(devpath, '/');
-    if (p) {
-        dev->def->driver = strdup(p+1);
-        if (!dev->def->driver) {
-            virReportOOMError();
-            goto cleanup;
-        }
+    if (p && VIR_STRDUP(dev->def->driver, p+1) < 0) {
+        virReportOOMError();
+        goto cleanup;
     }
     ret = 0;
 
@@ -161,7 +159,7 @@ nodeListDevices(virConnectPtr conn,
         virNodeDeviceObjLock(driver->devs.objs[i]);
         if (cap == NULL ||
             virNodeDeviceHasCap(driver->devs.objs[i], cap)) {
-            if ((names[ndevs++] = strdup(driver->devs.objs[i]->def->name)) == NULL) {
+            if (VIR_STRDUP(names[ndevs++], driver->devs.objs[i]->def->name) < 0) {
                 virNodeDeviceObjUnlock(driver->devs.objs[i]);
                 virReportOOMError();
                 goto failure;
@@ -321,8 +319,7 @@ nodeDeviceGetParent(virNodeDevicePtr dev)
     }
 
     if (obj->def->parent) {
-        ret = strdup(obj->def->parent);
-        if (!ret)
+        if (VIR_STRDUP(ret, obj->def->parent) < 0)
             virReportOOMError();
     } else {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -388,8 +385,7 @@ nodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
     }
 
     for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
-        names[ncaps] = strdup(virNodeDevCapTypeToString(caps->type));
-        if (names[ncaps++] == NULL) {
+        if (VIR_STRDUP(names[ncaps], virNodeDevCapTypeToString(caps->type)) < 0) {
             virReportOOMError();
             goto cleanup;
         }
@@ -554,7 +550,10 @@ nodeDeviceDestroy(virNodeDevicePtr dev)
         goto out;
     }
 
-    parent_name = strdup(obj->def->parent);
+    if (VIR_STRDUP(parent_name, obj->def->parent) < 0) {
+        virReportOOMError();
+        goto out;
+    }
 
     /* virNodeDeviceGetParentHost will cause the device object's lock to be
      * taken, so we have to dup the parent's name and drop the lock
@@ -563,11 +562,6 @@ nodeDeviceDestroy(virNodeDevicePtr dev)
     virNodeDeviceObjUnlock(obj);
     obj = NULL;
 
-    if (parent_name == NULL) {
-        virReportOOMError();
-        goto out;
-    }
-
     if (virNodeDeviceGetParentHost(&driver->devs,
                                    dev->name,
                                    parent_name,
diff --git a/src/node_device/node_device_hal.c b/src/node_device/node_device_hal.c
index 63245a9..1077445 100644
--- a/src/node_device/node_device_hal.c
+++ b/src/node_device/node_device_hal.c
@@ -445,10 +445,10 @@ static void dev_create(const char *udi)
     virNodeDeviceDefPtr def = NULL;
     const char *name = hal_name(udi);
     int rv;
-    char *privData = strdup(udi);
+    char *privData;
     char *devicePath = NULL;
 
-    if (!privData)
+    if (VIR_STRDUP(privDatam udi) < 0)
         return;
 
     nodeDeviceLock(driverState);
@@ -457,14 +457,15 @@ static void dev_create(const char *udi)
     if (VIR_ALLOC(def) < 0)
         goto failure;
 
-    if ((def->name = strdup(name)) == NULL)
+    if (VIR_STRDUP(def->name, name) < 0)
         goto failure;
 
     if (get_str_prop(ctx, udi, "info.parent", &parent_key) == 0) {
-        def->parent = strdup(hal_name(parent_key));
-        VIR_FREE(parent_key);
-        if (def->parent == NULL)
+        if (VIR_STRDUP(def->parent, hal_name(parent_key)) < 0) {
+            VIR_FREE(parent_key);
             goto failure;
+        }
+        VIR_FREE(parent_key);
     }
 
     rv = gather_capabilities(ctx, udi, &def->caps);
diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index c3c97d7..9bc4496 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -125,8 +125,7 @@ static int udevGetDeviceProperty(struct udev_device *udev_device,
 
     /* If this allocation is changed, the comment at the beginning
      * of the function must also be changed. */
-    *property_value = strdup(udev_value);
-    if (*property_value == NULL) {
+    if (VIR_STRDUP(*property_value, udev_value) < 0) {
         VIR_ERROR(_("Failed to allocate memory for property value for "
                     "property key '%s' on device with sysname '%s'"),
                   property_key, udev_device_get_sysname(udev_device));
@@ -214,8 +213,7 @@ static int udevGetDeviceSysfsAttr(struct udev_device *udev_device,
 
     /* If this allocation is changed, the comment at the beginning
      * of the function must also be changed. */
-    *attr_value = strdup(udev_value);
-    if (*attr_value == NULL) {
+    if (VIR_STRDUP(*attr_value, udev_value) < 0) {
         VIR_ERROR(_("Failed to allocate memory for sysfs attribute value for "
                     "sysfs attribute '%s' on device with sysname '%s'"),
                   attr_name, udev_device_get_sysname(udev_device));
@@ -387,20 +385,14 @@ static int udevTranslatePCIIds(unsigned int vendor,
                     NULL,
                     NULL);
 
-    if (vendor_name != NULL) {
-        *vendor_string = strdup(vendor_name);
-        if (*vendor_string == NULL) {
-            virReportOOMError();
-            goto out;
-        }
+    if (vendor_name && VIR_STRDUP(*vendor_string, vendor_name) < 0) {
+        virReportOOMError();
+        goto out;
     }
 
-    if (device_name != NULL) {
-        *product_string = strdup(device_name);
-        if (*product_string == NULL) {
-            virReportOOMError();
-            goto out;
-        }
+    if (device_name && VIR_STRDUP(*product_string, device_name) < 0) {
+        virReportOOMError();
+        goto out;
     }
 
     ret = 0;
@@ -691,8 +683,7 @@ static int udevProcessSCSITarget(struct udev_device *device ATTRIBUTE_UNUSED,
 
     sysname = udev_device_get_sysname(device);
 
-    data->scsi_target.name = strdup(sysname);
-    if (data->scsi_target.name == NULL) {
+    if (VIR_STRDUP(data->scsi_target.name, sysname) < 0) {
         virReportOOMError();
         goto out;
     }
@@ -718,34 +709,34 @@ static int udevGetSCSIType(virNodeDeviceDefPtr def ATTRIBUTE_UNUSED,
 
     switch (type) {
     case TYPE_DISK:
-        *typestring = strdup("disk");
+        ret = VIR_STRDUP(*typestring, "disk");
         break;
     case TYPE_TAPE:
-        *typestring = strdup("tape");
+        ret = VIR_STRDUP(*typestring, "tape");
         break;
     case TYPE_PROCESSOR:
-        *typestring = strdup("processor");
+        ret = VIR_STRDUP(*typestring, "processor");
         break;
     case TYPE_WORM:
-        *typestring = strdup("worm");
+        ret = VIR_STRDUP(*typestring, "worm");
         break;
     case TYPE_ROM:
-        *typestring = strdup("cdrom");
+        ret = VIR_STRDUP(*typestring, "cdrom");
         break;
     case TYPE_SCANNER:
-        *typestring = strdup("scanner");
+        ret = VIR_STRDUP(*typestring, "scanner");
         break;
     case TYPE_MOD:
-        *typestring = strdup("mod");
+        ret = VIR_STRDUP(*typestring, "mod");
         break;
     case TYPE_MEDIUM_CHANGER:
-        *typestring = strdup("changer");
+        ret = VIR_STRDUP(*typestring, "changer");
         break;
     case TYPE_ENCLOSURE:
-        *typestring = strdup("enclosure");
+        ret = VIR_STRDUP(*typestring, "enclosure");
         break;
     case TYPE_RAID:
-        *typestring = strdup("raid");
+        ret = VIR_STRDUP(*typestring, "raid");
         break;
     case TYPE_NO_LUN:
     default:
@@ -753,14 +744,12 @@ static int udevGetSCSIType(virNodeDeviceDefPtr def ATTRIBUTE_UNUSED,
         break;
     }
 
-    if (*typestring == NULL) {
-        if (foundtype == 1) {
-            ret = -1;
+    if (foundtype == 1) {
+        if (ret < 0)
             virReportOOMError();
-        } else {
-            VIR_DEBUG("Failed to find SCSI device type %d for %s",
-                      type, def->sysfs_path);
-        }
+    } else {
+        VIR_DEBUG("Failed to find SCSI device type %d for %s",
+                  type, def->sysfs_path);
     }
 
     return ret;
@@ -917,8 +906,7 @@ static int udevProcessCDROM(struct udev_device *device,
      * change it to cdrom to preserve compatibility with earlier
      * versions of libvirt.  */
     VIR_FREE(def->caps->data.storage.drive_type);
-    def->caps->data.storage.drive_type = strdup("cdrom");
-    if (def->caps->data.storage.drive_type == NULL) {
+    if (VIR_STRDUP(def->caps->data.storage.drive_type, "cdrom") < 0) {
         virReportOOMError();
         goto out;
     }
@@ -996,10 +984,7 @@ static int udevKludgeStorageType(virNodeDeviceDefPtr def)
 
     if (STRPREFIX(def->caps->data.storage.block, "/dev/vd")) {
         /* virtio disk */
-        def->caps->data.storage.drive_type = strdup("disk");
-        if (def->caps->data.storage.drive_type != NULL) {
-            ret = 0;
-        }
+        ret = VIR_STRDUP(def->caps->data.storage.drive_type, "disk");
     }
 
     if (ret != 0) {
@@ -1043,7 +1028,9 @@ static int udevProcessStorage(struct udev_device *device,
         VIR_DEBUG("No devnode for '%s'", udev_device_get_devpath(device));
         goto out;
     }
-    data->storage.block = strdup(devnode);
+
+    if (VIR_STRDUP(data->storage.block, devnode) < 0)
+        goto out;
 
     if (udevGetStringProperty(device,
                               "ID_BUS",
@@ -1083,15 +1070,13 @@ static int udevProcessStorage(struct udev_device *device,
                                 &tmp_int, 0) == PROPERTY_FOUND) &&
             (tmp_int == 1)) {
 
-            data->storage.drive_type = strdup("floppy");
-            if (!data->storage.drive_type)
+            if (VIR_STRDUP(data->storage.drive_type, "floppy") < 0)
                 goto out;
         } else if ((udevGetIntProperty(device, "ID_DRIVE_FLASH_SD",
                                        &tmp_int, 0) == PROPERTY_FOUND) &&
                    (tmp_int == 1)) {
 
-            data->storage.drive_type = strdup("sd");
-            if (!data->storage.drive_type)
+            if (VIR_STRDUP(data->storage.drive_type, "sd") < 0)
                 goto out;
         } else {
 
@@ -1294,16 +1279,14 @@ static int udevSetParent(struct udev_device *device,
         dev = virNodeDeviceFindBySysfsPath(&driverState->devs,
                                            parent_sysfs_path);
         if (dev != NULL) {
-            def->parent = strdup(dev->def->name);
-            virNodeDeviceObjUnlock(dev);
-
-            if (def->parent == NULL) {
+            if (VIR_STRDUP(def->parent, dev->def->name) < 0) {
+                virNodeDeviceObjUnlock(dev);
                 virReportOOMError();
                 goto out;
             }
+            virNodeDeviceObjUnlock(dev);
 
-            def->parent_sysfs_path = strdup(parent_sysfs_path);
-            if (def->parent_sysfs_path == NULL) {
+            if (VIR_STRDUP(def->parent_sysfs_path, parent_sysfs_path) < 0) {
                 virReportOOMError();
                 goto out;
             }
@@ -1312,11 +1295,7 @@ static int udevSetParent(struct udev_device *device,
 
     } while (def->parent == NULL && parent_device != NULL);
 
-    if (def->parent == NULL) {
-        def->parent = strdup("computer");
-    }
-
-    if (def->parent == NULL) {
+    if (!def->parent && VIR_STRDUP(def->parent, "computer") < 0) {
         virReportOOMError();
         goto out;
     }
@@ -1339,7 +1318,11 @@ static int udevAddOneDevice(struct udev_device *device)
         goto out;
     }
 
-    def->sysfs_path = strdup(udev_device_get_syspath(device));
+    if (VIR_STRDUP(def->sysfs_path, udev_device_get_syspath(device)) < 0) {
+        virReportOOMError();
+        goto out;
+    }
+
     if (udevGetStringProperty(device,
                               "DRIVER",
                               &def->driver) == PROPERTY_ERROR) {
@@ -1617,8 +1600,7 @@ static int udevSetupSystemDev(void)
         goto out;
     }
 
-    def->name = strdup("computer");
-    if (def->name == NULL) {
+    if (VIR_STRDUP(def->name, "computer") < 0) {
         virReportOOMError();
         goto out;
     }
-- 
1.8.1.5




More information about the libvir-list mailing list