Re: [libvirt] [PATCH] esx: Remove redundant checks for esxVI_LookupHostSystemProperties result
Ata Bohra <ata.husain <at> hotmail.com>
2012-08-05 20:00:40 GMT
> Date: Sun, 5 Aug 2012 13:48:01 +0200
> From: matthias.bolte <at> googlemail.com
> To: libvir-list <at> redhat.com
> Subject: [libvirt] [PATCH] esx: Remove redundant checks for esxVI_LookupHostSystemProperties result
>
> esxVI_LookupHostSystemProperties guarantees that hostSystem is non-NULL.
> Remove redundant NULL checks from callers.
>
> Also prefer esxVI_GetStringValue over open-coding the logic.
> ---
> src/esx/esx_driver.c | 82 +++++++++---------------------------------------
> src/esx/esx_vi.c | 4 +-
> src/esx/esx_vi_types.c | 3 +-
> 3 files changed, 19 insertions(+), 70 deletions(-)
>
> diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
> index 47957cc..72a7acc 100644
> --- a/src/esx/esx_driver.c
> +++ b/src/esx/esx_driver.c
> <at> <at> -3,7 +3,7 <at> <at>
> * esx_driver.c: core driver functions for managing VMware ESX hosts
> *
> * Copyright (C) 2010-2012 Red Hat, Inc.
> - * Copyright (C) 2009-2011 Matthias Bolte <matthias.bolte <at> googlemail.com>
> + * Copyright (C) 2009-2012 Matthias Bolte <matthias.bolte <at> googlemail.com>
> * Copyright (C) 2009 Maximilian Wilhelm <max <at> rfc2324.org>
> *
> * This library is free software; you can redistribute it and/or
> <at> <at> -467,12 +467,6 <at> <at> esxSupportsLongMode(esxPrivate *priv)
> goto cleanup;
> }
>
> - if (hostSystem == NULL) {
> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> - _("Could not retrieve the HostSystem object"));
> - goto cleanup;
> - }
> -
> for (dynamicProperty = hostSystem->propSet; dynamicProperty != NULL;
> dynamicProperty = dynamicProperty->_next) {
> if (STREQ(dynamicProperty->name, "hardware.cpuFeature")) {
> <at> <at> -534,7 +528,7 <at> <at> esxLookupHostSystemBiosUuid(esxPrivate *priv, unsigned char *uuid)
> int result = -1;
> esxVI_String *propertyNameList = NULL;
> esxVI_ObjectContent *hostSystem = NULL;
> - esxVI_DynamicProperty *dynamicProperty = NULL;
> + char *uuid_string = NULL;
>
> if (esxVI_EnsureSession(priv->primary) < 0) {
> return -1;
> <at> <at> -543,41 +537,22 <at> <at> esxLookupHostSystemBiosUuid(esxPrivate *priv, unsigned char *uuid)
> if (esxVI_String_AppendValueToList(&propertyNameList,
> "hardware.systemInfo.uuid") < 0 ||
> esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
> - &hostSystem) < 0) {
> - goto cleanup;
> - }
> -
> - if (hostSystem == NULL) {
> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> - _("Could not retrieve the HostSystem object"));
> + &hostSystem) < 0 ||
> + esxVI_GetStringValue(hostSystem, "hardware.systemInfo.uuid",
> + &uuid_string, esxVI_Occurrence_RequiredItem) < 0) {
> goto cleanup;
> }
>
> - for (dynamicProperty = hostSystem->propSet; dynamicProperty != NULL;
> - dynamicProperty = dynamicProperty->_next) {
> - if (STREQ(dynamicProperty->name, "hardware.systemInfo.uuid")) {
> - if (esxVI_AnyType_ExpectType(dynamicProperty->val,
> - esxVI_Type_String) < 0) {
> - goto cleanup;
> - }
> -
> - if (strlen(dynamicProperty->val->string) > 0) {
> - if (virUUIDParse(dynamicProperty->val->string, uuid) < 0) {
> - VIR_WARN("Could not parse host UUID from string '%s'",
> - dynamicProperty->val->string);
> + if (strlen(uuid_string) > 0) {
> + if (virUUIDParse(uuid_string, uuid) < 0) {
> + VIR_WARN("Could not parse host UUID from string '%s'", uuid_string);
>
> - /* HostSystem has an invalid UUID, ignore it */
> - memset(uuid, 0, VIR_UUID_BUFLEN);
> - }
> - } else {
> - /* HostSystem has an empty UUID */
> - memset(uuid, 0, VIR_UUID_BUFLEN);
> - }
> -
> - break;
> - } else {
> - VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
> + /* HostSystem has an invalid UUID, ignore it */
> + memset(uuid, 0, VIR_UUID_BUFLEN);
> }
> + } else {
> + /* HostSystem has an empty UUID */
> + memset(uuid, 0, VIR_UUID_BUFLEN);
> }
>
> result = 0;
> <at> <at> -1176,17 +1151,8 <at> <at> esxSupportsVMotion(esxPrivate *priv)
> if (esxVI_String_AppendValueToList(&propertyNameList,
> "capability.vmotionSupported") < 0 ||
> esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,
> - &hostSystem) < 0) {
> - goto cleanup;
> - }
> -
> - if (hostSystem == NULL) {
> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> - _("Could not retrieve the HostSystem object"));
> - goto cleanup;
> - }
> -
> - if (esxVI_GetBoolean(hostSystem, "capability.vmotionSupported",
> + &hostSystem) < 0 ||
> + esxVI_GetBoolean(hostSystem, "capability.vmotionSupported",
> &priv->supportsVMotion,
> esxVI_Occurrence_RequiredItem) < 0) {
> goto cleanup;
> <at> <at> -1281,12 +1247,6 <at> <at> esxGetHostname(virConnectPtr conn)
> goto cleanup;
> }
>
> - if (hostSystem == NULL) {
> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> - _("Could not retrieve the HostSystem object"));
> - goto cleanup;
> - }
> -
> for (dynamicProperty = hostSystem->propSet; dynamicProperty != NULL;
> dynamicProperty = dynamicProperty->_next) {
> if (STREQ(dynamicProperty->name,
> <at> <at> -1379,12 +1339,6 <at> <at> esxNodeGetInfo(virConnectPtr conn, virNodeInfoPtr nodeinfo)
> goto cleanup;
> }
>
> - if (hostSystem == NULL) {
> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> - _("Could not retrieve the HostSystem object"));
> - goto cleanup;
> - }
> -
> for (dynamicProperty = hostSystem->propSet; dynamicProperty != NULL;
> dynamicProperty = dynamicProperty->_next) {
> if (STREQ(dynamicProperty->name, "hardware.cpuInfo.hz")) {
> <at> <at> -2702,12 +2656,6 <at> <at> esxDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)
> goto cleanup;
> }
>
> - if (hostSystem == NULL) {
> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> - _("Could not retrieve the HostSystem object"));
> - goto cleanup;
> - }
> -
> for (dynamicProperty = hostSystem->propSet; dynamicProperty != NULL;
> dynamicProperty = dynamicProperty->_next) {
> if (STREQ(dynamicProperty->name, "capability.maxSupportedVcpus")) {
> diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c
> index c4217c9..f3a9e91 100644
> --- a/src/esx/esx_vi.c
> +++ b/src/esx/esx_vi.c
> <at> <at> -4570,8 +4570,8 <at> <at> esxVI_ProductVersionToDefaultVirtualHWVersion(esxVI_ProductVersion productVersio
> esxVI_DynamicProperty *dynamicProperty = NULL; \
> \
> if (ptrptr == NULL || *ptrptr != NULL) { \
> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \
> - _("Invalid argument")); \
> + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \
> + _("Invalid argument")); \
> return -1; \
> } \
> \
> diff --git a/src/esx/esx_vi_types.c b/src/esx/esx_vi_types.c
> index 36f3196..2236751 100644
> --- a/src/esx/esx_vi_types.c
> +++ b/src/esx/esx_vi_types.c
> <at> <at> -296,7 +296,8 <at> <at>
> childNode = childNode->next) { \
> if (childNode->type != XML_ELEMENT_NODE) { \
> virReportError(VIR_ERR_INTERNAL_ERROR, \
> - _("Wrong XML element type %d"), childNode->type); \
> + _("Wrong XML element type %d"), \
> + childNode->type); \
> goto failure; \
> } \
> \
> --
> 1.7.4.1
>
> --
> libvir-list mailing list
> libvir-list <at> redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list
ACK, Thanks for the cleanup.
Regards,
Ata
<div><div dir="ltr">> Date: Sun, 5 Aug 2012 13:48:01 +0200<br><div>> From: matthias.bolte <at> googlemail.com<br>> To: libvir-list <at> redhat.com<br>> Subject: [libvirt] [PATCH] esx: Remove redundant checks for esxVI_LookupHostSystemProperties result<br>> <br>> esxVI_LookupHostSystemProperties guarantees that hostSystem is non-NULL.<br>> Remove redundant NULL checks from callers.<br>> <br>> Also prefer esxVI_GetStringValue over open-coding the logic.<br>> ---<br>> src/esx/esx_driver.c | 82 +++++++++---------------------------------------<br>> src/esx/esx_vi.c | 4 +-<br>> src/esx/esx_vi_types.c | 3 +-<br>> 3 files changed, 19 insertions(+), 70 deletions(-)<br>> <br>> diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c<br>> index 47957cc..72a7acc 100644<br>> --- a/src/esx/esx_driver.c<br>> +++ b/src/esx/esx_driver.c<br>> <at> <at> -3,7 +3,7 <at> <at> <br>> * esx_driver.c: core driver functions for managing VMware ESX hosts<br>> *<br>> * Copyright (C) 2010-2012 Red Hat, Inc.<br>> - * Copyright (C) 2009-2011 Matthias Bolte <matthias.bolte <at> googlemail.com><br>> + * Copyright (C) 2009-2012 Matthias Bolte <matthias.bolte <at> googlemail.com><br>> * Copyright (C) 2009 Maximilian Wilhelm <max <at> rfc2324.org><br>> *<br>> * This library is free software; you can redistribute it and/or<br>> <at> <at> -467,12 +467,6 <at> <at> esxSupportsLongMode(esxPrivate *priv)<br>> goto cleanup;<br>> }<br>> <br>> - if (hostSystem == NULL) {<br>> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",<br>> - _("Could not retrieve the HostSystem object"));<br>> - goto cleanup;<br>> - }<br>> -<br>> for (dynamicProperty = hostSystem->propSet; dynamicProperty != NULL;<br>> dynamicProperty = dynamicProperty->_next) {<br>> if (STREQ(dynamicProperty->name, "hardware.cpuFeature")) {<br>> <at> <at> -534,7 +528,7 <at> <at> esxLookupHostSystemBiosUuid(esxPrivate *priv, unsigned char *uuid)<br>> int result = -1;<br>> esxVI_String *propertyNameList = NULL;<br>> esxVI_ObjectContent *hostSystem = NULL;<br>> - esxVI_DynamicProperty *dynamicProperty = NULL;<br>> + char *uuid_string = NULL;<br>> <br>> if (esxVI_EnsureSession(priv->primary) < 0) {<br>> return -1;<br>> <at> <at> -543,41 +537,22 <at> <at> esxLookupHostSystemBiosUuid(esxPrivate *priv, unsigned char *uuid)<br>> if (esxVI_String_AppendValueToList(&propertyNameList,<br>> "hardware.systemInfo.uuid") < 0 ||<br>> esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,<br>> - &hostSystem) < 0) {<br>> - goto cleanup;<br>> - }<br>> -<br>> - if (hostSystem == NULL) {<br>> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",<br>> - _("Could not retrieve the HostSystem object"));<br>> + &hostSystem) < 0 ||<br>> + esxVI_GetStringValue(hostSystem, "hardware.systemInfo.uuid",<br>> + &uuid_string, esxVI_Occurrence_RequiredItem) < 0) {<br>> goto cleanup;<br>> }<br>> <br>> - for (dynamicProperty = hostSystem->propSet; dynamicProperty != NULL;<br>> - dynamicProperty = dynamicProperty->_next) {<br>> - if (STREQ(dynamicProperty->name, "hardware.systemInfo.uuid")) {<br>> - if (esxVI_AnyType_ExpectType(dynamicProperty->val,<br>> - esxVI_Type_String) < 0) {<br>> - goto cleanup;<br>> - }<br>> -<br>> - if (strlen(dynamicProperty->val->string) > 0) {<br>> - if (virUUIDParse(dynamicProperty->val->string, uuid) < 0) {<br>> - VIR_WARN("Could not parse host UUID from string '%s'",<br>> - dynamicProperty->val->string);<br>> + if (strlen(uuid_string) > 0) {<br>> + if (virUUIDParse(uuid_string, uuid) < 0) {<br>> + VIR_WARN("Could not parse host UUID from string '%s'", uuid_string);<br>> <br>> - /* HostSystem has an invalid UUID, ignore it */<br>> - memset(uuid, 0, VIR_UUID_BUFLEN);<br>> - }<br>> - } else {<br>> - /* HostSystem has an empty UUID */<br>> - memset(uuid, 0, VIR_UUID_BUFLEN);<br>> - }<br>> -<br>> - break;<br>> - } else {<br>> - VIR_WARN("Unexpected '%s' property", dynamicProperty->name);<br>> + /* HostSystem has an invalid UUID, ignore it */<br>> + memset(uuid, 0, VIR_UUID_BUFLEN);<br>> }<br>> + } else {<br>> + /* HostSystem has an empty UUID */<br>> + memset(uuid, 0, VIR_UUID_BUFLEN);<br>> }<br>> <br>> result = 0;<br>> <at> <at> -1176,17 +1151,8 <at> <at> esxSupportsVMotion(esxPrivate *priv)<br>> if (esxVI_String_AppendValueToList(&propertyNameList,<br>> "capability.vmotionSupported") < 0 ||<br>> esxVI_LookupHostSystemProperties(priv->primary, propertyNameList,<br>> - &hostSystem) < 0) {<br>> - goto cleanup;<br>> - }<br>> -<br>> - if (hostSystem == NULL) {<br>> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",<br>> - _("Could not retrieve the HostSystem object"));<br>> - goto cleanup;<br>> - }<br>> -<br>> - if (esxVI_GetBoolean(hostSystem, "capability.vmotionSupported",<br>> + &hostSystem) < 0 ||<br>> + esxVI_GetBoolean(hostSystem, "capability.vmotionSupported",<br>> &priv->supportsVMotion,<br>> esxVI_Occurrence_RequiredItem) < 0) {<br>> goto cleanup;<br>> <at> <at> -1281,12 +1247,6 <at> <at> esxGetHostname(virConnectPtr conn)<br>> goto cleanup;<br>> }<br>> <br>> - if (hostSystem == NULL) {<br>> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",<br>> - _("Could not retrieve the HostSystem object"));<br>> - goto cleanup;<br>> - }<br>> -<br>> for (dynamicProperty = hostSystem->propSet; dynamicProperty != NULL;<br>> dynamicProperty = dynamicProperty->_next) {<br>> if (STREQ(dynamicProperty->name,<br>> <at> <at> -1379,12 +1339,6 <at> <at> esxNodeGetInfo(virConnectPtr conn, virNodeInfoPtr nodeinfo)<br>> goto cleanup;<br>> }<br>> <br>> - if (hostSystem == NULL) {<br>> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",<br>> - _("Could not retrieve the HostSystem object"));<br>> - goto cleanup;<br>> - }<br>> -<br>> for (dynamicProperty = hostSystem->propSet; dynamicProperty != NULL;<br>> dynamicProperty = dynamicProperty->_next) {<br>> if (STREQ(dynamicProperty->name, "hardware.cpuInfo.hz")) {<br>> <at> <at> -2702,12 +2656,6 <at> <at> esxDomainGetVcpusFlags(virDomainPtr domain, unsigned int flags)<br>> goto cleanup;<br>> }<br>> <br>> - if (hostSystem == NULL) {<br>> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s",<br>> - _("Could not retrieve the HostSystem object"));<br>> - goto cleanup;<br>> - }<br>> -<br>> for (dynamicProperty = hostSystem->propSet; dynamicProperty != NULL;<br>> dynamicProperty = dynamicProperty->_next) {<br>> if (STREQ(dynamicProperty->name, "capability.maxSupportedVcpus")) {<br>> diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c<br>> index c4217c9..f3a9e91 100644<br>> --- a/src/esx/esx_vi.c<br>> +++ b/src/esx/esx_vi.c<br>> <at> <at> -4570,8 +4570,8 <at> <at> esxVI_ProductVersionToDefaultVirtualHWVersion(esxVI_ProductVersion productVersio<br>> esxVI_DynamicProperty *dynamicProperty = NULL; \<br>> \<br>> if (ptrptr == NULL || *ptrptr != NULL) { \<br>> - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \<br>> - _("Invalid argument")); \<br>> + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \<br>> + _("Invalid argument")); \<br>> return -1; \<br>> } \<br>> \<br>> diff --git a/src/esx/esx_vi_types.c b/src/esx/esx_vi_types.c<br>> index 36f3196..2236751 100644<br>> --- a/src/esx/esx_vi_types.c<br>> +++ b/src/esx/esx_vi_types.c<br>> <at> <at> -296,7 +296,8 <at> <at> <br>> childNode = childNode->next) { \<br>> if (childNode->type != XML_ELEMENT_NODE) { \<br>> virReportError(VIR_ERR_INTERNAL_ERROR, \<br>> - _("Wrong XML element type %d"), childNode->type); \<br>> + _("Wrong XML element type %d"), \<br>> + childNode->type); \<br>> goto failure; \<br>> } \<br>> \<br>> -- <br>> 1.7.4.1<br>> <br>> --<br>> libvir-list mailing list<br>> libvir-list <at> redhat.com<br>> https://www.redhat.com/mailman/listinfo/libvir-list<br><br>ACK, Thanks for the cleanup.<br><br>Regards,<br>Ata<br>
</div> </div></div>