From be2636fd0bac051ff92a95e7f2fa33e042693c2c Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Fri, 3 May 2013 14:44:20 +0200 Subject: [PATCH] Adapt to VIR_STRDUP and VIR_STRNDUP in src/node_device/* --- src/node_device/node_device_driver.c | 33 +++------ src/node_device/node_device_hal.c | 13 ++-- src/node_device/node_device_udev.c | 105 +++++++++------------------ 3 files changed, 51 insertions(+), 100 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index 00f7aafe0d..c596901747 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 8c0620ca55..eecbffad63 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 b7bd919016..7036c8ca02 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 (VIR_STRDUP(*vendor_string, vendor_name) < 0|| + 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,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 +1059,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 +1268,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) { + virNodeDeviceObjUnlock(dev); goto out; } + 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 +1301,9 @@ 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) + goto out; + if (udevGetStringProperty(device, "DRIVER", &def->driver) == PROPERTY_ERROR) { @@ -1379,7 +1343,7 @@ static int udevAddOneDevice(struct udev_device *device) out: if (ret != 0) { VIR_DEBUG("Discarding device %d %p %s", ret, def, - def ? def->sysfs_path : ""); + def ? NULLSTR(def->sysfs_path) : ""); virNodeDeviceDefFree(def); } @@ -1617,11 +1581,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(); -- 2.39.5