This macro avoids code duplication when checking for arrays of objects. Signed-off-by: Marcos Paulo de Souza <marcos.souza.org@xxxxxxxxx> --- src/esx/esx_vi.c | 189 ++++++++++++----------------------------------- 1 file changed, 46 insertions(+), 143 deletions(-) diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c index 25fbdc7e44..212300dbff 100644 --- a/src/esx/esx_vi.c +++ b/src/esx/esx_vi.c @@ -41,6 +41,16 @@ VIR_LOG_INIT("esx.esx_vi"); +#define esxVI_checkArgList(val) \ + do { \ + if (!val || *val) { \ + virReportError(VIR_ERR_INVALID_ARG, "%s", _("Invalid argument")); \ + return -1; \ + } \ + } while (0) + + + #define ESX_VI__SOAP__RESPONSE_XPATH(_type) \ ((char *)"/soapenv:Envelope/soapenv:Body/" \ "vim:"_type"Response/vim:returnval") @@ -51,11 +61,7 @@ VIR_LOG_INIT("esx.esx_vi"); int \ esxVI_##_type##_Alloc(esxVI_##_type **ptrptr) \ { \ - if (!ptrptr || *ptrptr) { \ - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); \ - return -1; \ - } \ - \ + esxVI_checkArgList(ptrptr); \ if (VIR_ALLOC(*ptrptr) < 0) \ return -1; \ return 0; \ @@ -372,10 +378,7 @@ esxVI_CURL_Download(esxVI_CURL *curl, const char *url, char **content, virBuffer buffer = VIR_BUFFER_INITIALIZER; int responseCode = 0; - if (!content || *content) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(content); if (length && *length > 0) { /* @@ -1762,10 +1765,7 @@ esxVI_List_DeepCopy(esxVI_List **destList, esxVI_List *srcList, esxVI_List *dest = NULL; esxVI_List *src = NULL; - if (!destList || *destList) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(destList); for (src = srcList; src; src = src->_next) { if (deepCopyFunc(&dest, src) < 0 || @@ -2170,10 +2170,7 @@ esxVI_LookupObjectContentByType(esxVI_Context *ctx, bool propertySpec_isAppended = false; esxVI_PropertyFilterSpec *propertyFilterSpec = NULL; - if (!objectContentList || *objectContentList) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(objectContentList); if (esxVI_ObjectSpec_Alloc(&objectSpec) < 0) return -1; @@ -2372,10 +2369,7 @@ esxVI_GetVirtualMachineQuestionInfo { esxVI_DynamicProperty *dynamicProperty; - if (!questionInfo || *questionInfo) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(questionInfo); for (dynamicProperty = virtualMachine->propSet; dynamicProperty; dynamicProperty = dynamicProperty->_next) { @@ -2447,10 +2441,7 @@ esxVI_GetInt(esxVI_ObjectContent *objectContent, const char *propertyName, { esxVI_DynamicProperty *dynamicProperty; - if (!value || *value) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(value); for (dynamicProperty = objectContent->propSet; dynamicProperty; dynamicProperty = dynamicProperty->_next) { @@ -2479,10 +2470,7 @@ esxVI_GetLong(esxVI_ObjectContent *objectContent, const char *propertyName, { esxVI_DynamicProperty *dynamicProperty; - if (!value || *value) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(value); for (dynamicProperty = objectContent->propSet; dynamicProperty; dynamicProperty = dynamicProperty->_next) { @@ -2512,10 +2500,7 @@ esxVI_GetStringValue(esxVI_ObjectContent *objectContent, { esxVI_DynamicProperty *dynamicProperty; - if (!value || *value) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(value); for (dynamicProperty = objectContent->propSet; dynamicProperty; dynamicProperty = dynamicProperty->_next) { @@ -2549,10 +2534,7 @@ esxVI_GetManagedObjectReference(esxVI_ObjectContent *objectContent, { esxVI_DynamicProperty *dynamicProperty; - if (!value || *value) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(value); for (dynamicProperty = objectContent->propSet; dynamicProperty; dynamicProperty = dynamicProperty->_next) { @@ -2863,10 +2845,7 @@ esxVI_GetSnapshotTreeBySnapshot { esxVI_VirtualMachineSnapshotTree *candidate; - if (!snapshotTree || *snapshotTree) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(snapshotTree); for (candidate = snapshotTreeList; candidate; candidate = candidate->_next) { @@ -2928,10 +2907,7 @@ esxVI_LookupVirtualMachineByUuid(esxVI_Context *ctx, const unsigned char *uuid, esxVI_ManagedObjectReference *managedObjectReference = NULL; char uuid_string[VIR_UUID_STRING_BUFLEN] = ""; - if (!virtualMachine || *virtualMachine) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(virtualMachine); virUUIDFormat(uuid, uuid_string); @@ -2983,10 +2959,7 @@ esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name, esxVI_ObjectContent *candidate = NULL; char *name_candidate = NULL; - if (!virtualMachine || *virtualMachine) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(virtualMachine); if (esxVI_String_DeepCopyList(&completePropertyNameList, propertyNameList) < 0 || @@ -3110,10 +3083,7 @@ esxVI_LookupDatastoreByName(esxVI_Context *ctx, const char *name, esxVI_ObjectContent *candidate = NULL; char *name_candidate; - if (!datastore || *datastore) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(datastore); /* Get all datastores */ if (esxVI_String_DeepCopyList(&completePropertyNameList, @@ -3177,10 +3147,7 @@ esxVI_LookupDatastoreByAbsolutePath(esxVI_Context *ctx, esxVI_DatastoreHostMount *datastoreHostMountList = NULL; esxVI_DatastoreHostMount *datastoreHostMount = NULL; - if (!datastore || *datastore) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(datastore); /* Get all datastores */ if (esxVI_String_DeepCopyList(&completePropertyNameList, @@ -3263,10 +3230,7 @@ esxVI_LookupDatastoreHostMount(esxVI_Context *ctx, esxVI_DatastoreHostMount *hostMountList = NULL; esxVI_DatastoreHostMount *candidate = NULL; - if (!hostMount || *hostMount) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(hostMount); if (esxVI_String_AppendValueToList(&propertyNameList, "host") < 0 || esxVI_LookupObjectContentByType(ctx, datastore, "Datastore", @@ -3327,10 +3291,7 @@ esxVI_LookupTaskInfoByTask(esxVI_Context *ctx, esxVI_ObjectContent *objectContent = NULL; esxVI_DynamicProperty *dynamicProperty = NULL; - if (!taskInfo || *taskInfo) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(taskInfo); if (esxVI_String_AppendValueToList(&propertyNameList, "info") < 0 || esxVI_LookupObjectContentByType(ctx, task, "Task", propertyNameList, @@ -3375,10 +3336,7 @@ esxVI_LookupPendingTaskInfoListByVirtualMachine esxVI_DynamicProperty *dynamicProperty = NULL; esxVI_TaskInfo *taskInfo = NULL; - if (!pendingTaskInfoList || *pendingTaskInfoList) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(pendingTaskInfoList); /* Get list of recent tasks */ for (dynamicProperty = virtualMachine->propSet; dynamicProperty; @@ -3481,10 +3439,7 @@ esxVI_LookupRootSnapshotTreeList esxVI_ObjectContent *virtualMachine = NULL; esxVI_DynamicProperty *dynamicProperty = NULL; - if (!rootSnapshotTreeList || *rootSnapshotTreeList) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(rootSnapshotTreeList); if (esxVI_String_AppendValueToList(&propertyNameList, "snapshot.rootSnapshotList") < 0 || @@ -3536,10 +3491,7 @@ esxVI_LookupCurrentSnapshotTree esxVI_VirtualMachineSnapshotTree *rootSnapshotTreeList = NULL; esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL; - if (!currentSnapshotTree || *currentSnapshotTree) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(currentSnapshotTree); if (esxVI_String_AppendValueListToList(&propertyNameList, "snapshot.currentSnapshot\0" @@ -3633,10 +3585,7 @@ esxVI_LookupFileInfoByDatastorePath(esxVI_Context *ctx, esxVI_TaskInfo *taskInfo = NULL; esxVI_HostDatastoreBrowserSearchResults *searchResults = NULL; - if (!fileInfo || *fileInfo) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(fileInfo); if (esxUtil_ParseDatastorePath(datastorePath, &datastoreName, &directoryName, &directoryAndFileName) < 0) { @@ -3829,10 +3778,7 @@ esxVI_LookupDatastoreContentByDatastoreName char *taskInfoErrorMessage = NULL; esxVI_TaskInfo *taskInfo = NULL; - if (!searchResultsList || *searchResultsList) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(searchResultsList); /* Lookup Datastore and HostDatastoreBrowser */ if (esxVI_String_AppendValueToList(&propertyNameList, "browser") < 0 || @@ -3941,10 +3887,7 @@ esxVI_LookupStorageVolumeKeyByDatastorePath(esxVI_Context *ctx, esxVI_FileInfo *fileInfo = NULL; char *uuid_string = NULL; - if (!key || *key) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(key); if (ctx->hasQueryVirtualDiskUuid) { if (esxVI_LookupFileInfoByDatastorePath @@ -3995,10 +3938,7 @@ esxVI_LookupAutoStartDefaults(esxVI_Context *ctx, esxVI_ObjectContent *hostAutoStartManager = NULL; esxVI_DynamicProperty *dynamicProperty = NULL; - if (!defaults || *defaults) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(defaults); /* * Lookup HostAutoStartManagerConfig from the HostAutoStartManager because @@ -4052,10 +3992,7 @@ esxVI_LookupAutoStartPowerInfoList(esxVI_Context *ctx, esxVI_ObjectContent *hostAutoStartManager = NULL; esxVI_DynamicProperty *dynamicProperty = NULL; - if (!powerInfoList || *powerInfoList) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(powerInfoList); /* * Lookup HostAutoStartManagerConfig from the HostAutoStartManager because @@ -4103,10 +4040,7 @@ esxVI_LookupPhysicalNicList(esxVI_Context *ctx, esxVI_ObjectContent *hostSystem = NULL; esxVI_DynamicProperty *dynamicProperty = NULL; - if (!physicalNicList || *physicalNicList) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(physicalNicList); if (esxVI_String_AppendValueToList(&propertyNameList, "config.network.pnic") < 0 || @@ -4147,10 +4081,7 @@ esxVI_LookupPhysicalNicByName(esxVI_Context *ctx, const char *name, esxVI_PhysicalNic *physicalNicList = NULL; esxVI_PhysicalNic *candidate = NULL; - if (!physicalNic || *physicalNic) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(physicalNic); if (esxVI_LookupPhysicalNicList(ctx, &physicalNicList) < 0) goto cleanup; @@ -4194,10 +4125,7 @@ esxVI_LookupPhysicalNicByMACAddress(esxVI_Context *ctx, const char *mac, esxVI_PhysicalNic *physicalNicList = NULL; esxVI_PhysicalNic *candidate = NULL; - if (!physicalNic || *physicalNic) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(physicalNic); if (esxVI_LookupPhysicalNicList(ctx, &physicalNicList) < 0) goto cleanup; @@ -4241,10 +4169,7 @@ esxVI_LookupHostVirtualSwitchList(esxVI_Context *ctx, esxVI_ObjectContent *hostSystem = NULL; esxVI_DynamicProperty *dynamicProperty = NULL; - if (!hostVirtualSwitchList || *hostVirtualSwitchList) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(hostVirtualSwitchList); if (esxVI_String_AppendValueToList(&propertyNameList, "config.network.vswitch") < 0 || @@ -4285,10 +4210,7 @@ esxVI_LookupHostVirtualSwitchByName(esxVI_Context *ctx, const char *name, esxVI_HostVirtualSwitch *hostVirtualSwitchList = NULL; esxVI_HostVirtualSwitch *candidate = NULL; - if (!hostVirtualSwitch || *hostVirtualSwitch) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(hostVirtualSwitch); if (esxVI_LookupHostVirtualSwitchList(ctx, &hostVirtualSwitchList) < 0) goto cleanup; @@ -4336,10 +4258,7 @@ esxVI_LookupHostPortGroupList(esxVI_Context *ctx, esxVI_ObjectContent *hostSystem = NULL; esxVI_DynamicProperty *dynamicProperty = NULL; - if (!hostPortGroupList || *hostPortGroupList) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(hostPortGroupList); if (esxVI_String_AppendValueToList(&propertyNameList, "config.network.portgroup") < 0 || @@ -4513,10 +4432,7 @@ esxVI_WaitForTaskCompletion(esxVI_Context *ctx, bool blocked; esxVI_TaskInfo *taskInfo = NULL; - if (!errorMessage || *errorMessage) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(errorMessage); if (VIR_STRDUP(version, "") < 0) return -1; @@ -4982,10 +4898,7 @@ esxVI_LookupHostScsiTopologyLunListByTargetName bool found = false; esxVI_HostInternetScsiTargetTransport *candidate = NULL; - if (!hostScsiTopologyLunList || *hostScsiTopologyLunList) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(hostScsiTopologyLunList); if (esxVI_String_AppendValueToList (&propertyNameList, @@ -5076,10 +4989,7 @@ esxVI_LookupStoragePoolNameByScsiLunKey(esxVI_Context *ctx, esxVI_HostScsiTopologyLun *hostScsiTopologyLun; bool found = false; - if (!poolName || *poolName) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(poolName); if (esxVI_String_AppendValueToList (&propertyNameList, @@ -5207,11 +5117,7 @@ esxVI_LookupStoragePoolNameByScsiLunKey(esxVI_Context *ctx, esxVI_ObjectContent *objectContentList = NULL; \ esxVI_DynamicProperty *dynamicProperty = NULL; \ \ - if (!ptrptr || *ptrptr) { \ - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \ - _("Invalid argument")); \ - return -1; \ - } \ + esxVI_checkArgList(ptrptr); \ \ propertyNameList = selectedPropertyNameList; \ \ @@ -5287,11 +5193,8 @@ esxVI_LookupManagedObjectHelper(esxVI_Context *ctx, esxVI_ObjectContent *candidate = NULL; char *name_candidate; - if (!objectContent || *objectContent || - !objectContentList || *objectContentList) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); - return -1; - } + esxVI_checkArgList(objectContent); + esxVI_checkArgList(objectContentList); if (!esxVI_String_ListContainsValue(propertyNameList, "name")) { virReportError(VIR_ERR_INTERNAL_ERROR, -- 2.17.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list