[libvirt] [PATCH v3 11/34] Adapt to VIR_STRDUP and VIR_STRNDUP in src/node_device/*

Michal Privoznik mprivozn at redhat.com
Fri May 3 14:53:16 UTC 2013


---
 src/node_device/node_device_driver.c | 33 ++++--------
 src/node_device/node_device_hal.c    | 13 ++---
 src/node_device/node_device_udev.c   | 99 ++++++++++--------------------------
 3 files changed, 46 insertions(+), 99 deletions(-)

diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c
index 05dc49c..8a84094 100644
--- a/src/node_device/node_device_driver.c
+++ b/src/node_device/node_device_driver.c
@@ -89,13 +89,8 @@ 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)
+        goto cleanup;
     ret = 0;
 
 cleanup:
@@ -162,9 +157,8 @@ 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;
             }
         }
@@ -322,9 +316,8 @@ nodeDeviceGetParent(virNodeDevicePtr dev)
     }
 
     if (obj->def->parent) {
-        ret = strdup(obj->def->parent);
-        if (!ret)
-            virReportOOMError();
+        if (VIR_STRDUP(ret, obj->def->parent) < 0)
+            goto cleanup;
     } else {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("no parent for this device"));
@@ -389,11 +382,8 @@ 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) {
-            virReportOOMError();
+        if (VIR_STRDUP(names[ncaps], virNodeDevCapTypeToString(caps->type)) < 0)
             goto cleanup;
-        }
     }
     ret = ncaps;
 
@@ -555,19 +545,18 @@ nodeDeviceDestroy(virNodeDevicePtr dev)
         goto out;
     }
 
-    parent_name = strdup(obj->def->parent);
 
     /* virNodeDeviceGetParentHost will cause the device object's lock to be
      * taken, so we have to dup the parent's name and drop the lock
      * before calling it.  We don't need the reference to the object
      * any more once we have the parent's name.  */
-    virNodeDeviceObjUnlock(obj);
-    obj = NULL;
-
-    if (parent_name == NULL) {
-        virReportOOMError();
+    if (VIR_STRDUP(parent_name, obj->def->parent) < 0) {
+        virNodeDeviceObjUnlock(obj);
+        obj = NULL;
         goto out;
     }
+    virNodeDeviceObjUnlock(obj);
+    obj = NULL;
 
     if (virNodeDeviceGetParentHost(&driver->devs,
                                    dev->name,
diff --git a/src/node_device/node_device_hal.c b/src/node_device/node_device_hal.c
index 8c77cc3..1a3b507 100644
--- a/src/node_device/node_device_hal.c
+++ b/src/node_device/node_device_hal.c
@@ -446,10 +446,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(privData, udi) < 0)
         return;
 
     nodeDeviceLock(driverState);
@@ -458,14 +458,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..7d7718e 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -125,12 +125,10 @@ 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));
-        virReportOOMError();
         ret = PROPERTY_ERROR;
         goto out;
     }
@@ -214,12 +212,10 @@ 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));
-        virReportOOMError();
         ret = PROPERTY_ERROR;
         goto out;
     }
@@ -387,21 +383,9 @@ 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 (device_name != NULL) {
-        *product_string = strdup(device_name);
-        if (*product_string == NULL) {
-            virReportOOMError();
-            goto out;
-        }
-    }
+    if ((vendor_name && VIR_STRDUP(*vendor_string, vendor_name) < 0) ||
+        (device_name && VIR_STRDUP(*product_string, device_name) < 0))
+        goto out;
 
     ret = 0;
 
@@ -691,11 +675,8 @@ 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) {
-        virReportOOMError();
+    if (VIR_STRDUP(data->scsi_target.name, sysname) < 0)
         goto out;
-    }
 
     if (udevGenerateDeviceName(device, def, NULL) != 0) {
         goto out;
@@ -718,34 +699,34 @@ static int udevGetSCSIType(virNodeDeviceDefPtr def ATTRIBUTE_UNUSED,
 
     switch (type) {
     case TYPE_DISK:
-        *typestring = strdup("disk");
+        ignore_value(VIR_STRDUP(*typestring, "disk"));
         break;
     case TYPE_TAPE:
-        *typestring = strdup("tape");
+        ignore_value(VIR_STRDUP(*typestring, "tape"));
         break;
     case TYPE_PROCESSOR:
-        *typestring = strdup("processor");
+        ignore_value(VIR_STRDUP(*typestring, "processor"));
         break;
     case TYPE_WORM:
-        *typestring = strdup("worm");
+        ignore_value(VIR_STRDUP(*typestring, "worm"));
         break;
     case TYPE_ROM:
-        *typestring = strdup("cdrom");
+        ignore_value(VIR_STRDUP(*typestring, "cdrom"));
         break;
     case TYPE_SCANNER:
-        *typestring = strdup("scanner");
+        ignore_value(VIR_STRDUP(*typestring, "scanner"));
         break;
     case TYPE_MOD:
-        *typestring = strdup("mod");
+        ignore_value(VIR_STRDUP(*typestring, "mod"));
         break;
     case TYPE_MEDIUM_CHANGER:
-        *typestring = strdup("changer");
+        ignore_value(VIR_STRDUP(*typestring, "changer"));
         break;
     case TYPE_ENCLOSURE:
-        *typestring = strdup("enclosure");
+        ignore_value(VIR_STRDUP(*typestring, "enclosure"));
         break;
     case TYPE_RAID:
-        *typestring = strdup("raid");
+        ignore_value(VIR_STRDUP(*typestring, "raid"));
         break;
     case TYPE_NO_LUN:
     default:
@@ -756,7 +737,6 @@ static int udevGetSCSIType(virNodeDeviceDefPtr def ATTRIBUTE_UNUSED,
     if (*typestring == NULL) {
         if (foundtype == 1) {
             ret = -1;
-            virReportOOMError();
         } else {
             VIR_DEBUG("Failed to find SCSI device type %d for %s",
                       type, def->sysfs_path);
@@ -917,11 +897,8 @@ 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) {
-        virReportOOMError();
+    if (VIR_STRDUP(def->caps->data.storage.drive_type, "cdrom") < 0)
         goto out;
-    }
 
     if ((udevGetIntProperty(device, "ID_CDROM_MEDIA",
                             &tmp_int, 0) == PROPERTY_FOUND))
@@ -996,10 +973,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 +1017,7 @@ 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);
+    ignore_value(VIR_STRDUP(data->storage.block, devnode));
 
     if (udevGetStringProperty(device,
                               "ID_BUS",
@@ -1083,15 +1057,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,32 +1266,20 @@ 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) {
-                virReportOOMError();
+            if (VIR_STRDUP(def->parent, dev->def->name) < 0) {
                 goto out;
+                virNodeDeviceObjUnlock(dev);
             }
+            virNodeDeviceObjUnlock(dev);
 
-            def->parent_sysfs_path = strdup(parent_sysfs_path);
-            if (def->parent_sysfs_path == NULL) {
-                virReportOOMError();
+            if (VIR_STRDUP(def->parent_sysfs_path, parent_sysfs_path) < 0)
                 goto out;
-            }
-
         }
 
     } while (def->parent == NULL && parent_device != NULL);
 
-    if (def->parent == NULL) {
-        def->parent = strdup("computer");
-    }
-
-    if (def->parent == NULL) {
-        virReportOOMError();
+    if (!def->parent && VIR_STRDUP(def->parent, "computer") < 0)
         goto out;
-    }
 
     ret = 0;
 
@@ -1339,7 +1299,7 @@ static int udevAddOneDevice(struct udev_device *device)
         goto out;
     }
 
-    def->sysfs_path = strdup(udev_device_get_syspath(device));
+    ignore_value(VIR_STRDUP(def->sysfs_path, udev_device_get_syspath(device)));
     if (udevGetStringProperty(device,
                               "DRIVER",
                               &def->driver) == PROPERTY_ERROR) {
@@ -1617,11 +1577,8 @@ static int udevSetupSystemDev(void)
         goto out;
     }
 
-    def->name = strdup("computer");
-    if (def->name == NULL) {
-        virReportOOMError();
+    if (VIR_STRDUP(def->name, "computer") < 0)
         goto out;
-    }
 
     if (VIR_ALLOC(def->caps) != 0) {
         virReportOOMError();
-- 
1.8.1.5




More information about the libvir-list mailing list