[PATCH 2/3] esx: Remove unnecessary NULL comparisons (2/3)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Code cleanup: remove explicit NULL comparisons like ptr == NULL and
ptr != NULL from the ESX code, replacing them with the simpler ptr
and !ptr.

Part two of three.
---
 src/esx/esx_interface_driver.c | 10 +++----
 src/esx/esx_network_driver.c   | 64 +++++++++++++++++++++---------------------
 src/esx/esx_util.c             | 48 +++++++++++++++----------------
 3 files changed, 61 insertions(+), 61 deletions(-)

diff --git a/src/esx/esx_interface_driver.c b/src/esx/esx_interface_driver.c
index 2cee3b7..dcb9f03 100644
--- a/src/esx/esx_interface_driver.c
+++ b/src/esx/esx_interface_driver.c
@@ -82,7 +82,7 @@ esxConnectNumOfInterfaces(virConnectPtr conn)
         return -1;
     }
 
-    for (physicalNic = physicalNicList; physicalNic != NULL;
+    for (physicalNic = physicalNicList; physicalNic;
          physicalNic = physicalNic->_next) {
         ++count;
     }
@@ -113,7 +113,7 @@ esxConnectListInterfaces(virConnectPtr conn, char **const names, int maxnames)
         return -1;
     }
 
-    for (physicalNic = physicalNicList; physicalNic != NULL;
+    for (physicalNic = physicalNicList; physicalNic;
          physicalNic = physicalNic->_next) {
         if (VIR_STRDUP(names[count], physicalNic->device) < 0)
             goto cleanup;
@@ -237,15 +237,15 @@ esxInterfaceGetXMLDesc(virInterfacePtr iface, unsigned int flags)
     def.startmode = VIR_INTERFACE_START_ONBOOT;
 
     /* FIXME: Add support for IPv6, requires to use vSphere API 4.0 */
-    if (physicalNic->spec->ip != NULL) {
+    if (physicalNic->spec->ip) {
         protocol.family = (char *)"ipv4";
 
         if (physicalNic->spec->ip->dhcp == esxVI_Boolean_True) {
             protocol.dhcp = 1;
         }
 
-        if (physicalNic->spec->ip->ipAddress != NULL &&
-            physicalNic->spec->ip->subnetMask != NULL &&
+        if (physicalNic->spec->ip->ipAddress &&
+            physicalNic->spec->ip->subnetMask &&
             strlen(physicalNic->spec->ip->ipAddress) > 0 &&
             strlen(physicalNic->spec->ip->subnetMask) > 0) {
             hasAddress = true;
diff --git a/src/esx/esx_network_driver.c b/src/esx/esx_network_driver.c
index 24059c1..c8b53b1 100644
--- a/src/esx/esx_network_driver.c
+++ b/src/esx/esx_network_driver.c
@@ -89,7 +89,7 @@ esxConnectNumOfNetworks(virConnectPtr conn)
         return -1;
     }
 
-    for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch != NULL;
+    for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch;
          hostVirtualSwitch = hostVirtualSwitch->_next) {
         ++count;
     }
@@ -121,7 +121,7 @@ esxConnectListNetworks(virConnectPtr conn, char **const names, int maxnames)
         return -1;
     }
 
-    for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch != NULL;
+    for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch;
          hostVirtualSwitch = hostVirtualSwitch->_next) {
         if (VIR_STRDUP(names[count], hostVirtualSwitch->name) < 0)
             goto cleanup;
@@ -183,7 +183,7 @@ esxNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
         return NULL;
     }
 
-    for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch != NULL;
+    for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch;
          hostVirtualSwitch = hostVirtualSwitch->_next) {
         md5_buffer(hostVirtualSwitch->key, strlen(hostVirtualSwitch->key), md5);
 
@@ -192,7 +192,7 @@ esxNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
         }
     }
 
-    if (hostVirtualSwitch == NULL) {
+    if (!hostVirtualSwitch) {
         virUUIDFormat(uuid, uuid_string);
 
         virReportError(VIR_ERR_NO_NETWORK,
@@ -252,12 +252,12 @@ esxBandwidthToShapingPolicy(virNetDevBandwidthPtr bandwidth,
 {
     int result = -1;
 
-    if (shapingPolicy == NULL || *shapingPolicy != NULL) {
+    if (!shapingPolicy || *shapingPolicy) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
         return -1;
     }
 
-    if (bandwidth->in == NULL || bandwidth->out == NULL ||
+    if (!bandwidth->in || !bandwidth->out ||
         bandwidth->in->average != bandwidth->out->average ||
         bandwidth->in->peak != bandwidth->out->peak ||
         bandwidth->in->burst != bandwidth->out->burst) {
@@ -341,7 +341,7 @@ esxNetworkDefineXML(virConnectPtr conn, const char *xml)
     /* Parse network XML */
     def = virNetworkDefParseString(xml);
 
-    if (def == NULL) {
+    if (!def) {
         return NULL;
     }
 
@@ -352,7 +352,7 @@ esxNetworkDefineXML(virConnectPtr conn, const char *xml)
         goto cleanup;
     }
 
-    if (hostVirtualSwitch != NULL) {
+    if (hostVirtualSwitch) {
         /* FIXME */
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("HostVirtualSwitch already exists, editing existing "
@@ -383,7 +383,7 @@ esxNetworkDefineXML(virConnectPtr conn, const char *xml)
         }
 
         for (i = 0; i < def->nPortGroups; ++i) {
-            for (hostPortGroup = hostPortGroupList; hostPortGroup != NULL;
+            for (hostPortGroup = hostPortGroupList; hostPortGroup;
                  hostPortGroup = hostPortGroup->_next) {
                 if (STREQ(def->portGroups[i].name, hostPortGroup->spec->name)) {
                     virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -427,7 +427,7 @@ esxNetworkDefineXML(virConnectPtr conn, const char *xml)
                 goto cleanup;
             }
 
-            for (physicalNic = physicalNicList; physicalNic != NULL;
+            for (physicalNic = physicalNicList; physicalNic;
                  physicalNic = physicalNic->_next) {
                 if (STREQ(def->forward.ifs[i].device.dev, physicalNic->device)) {
                     if (esxVI_String_AppendValueToList
@@ -452,7 +452,7 @@ esxNetworkDefineXML(virConnectPtr conn, const char *xml)
 
     hostVirtualSwitchSpec->numPorts->value = 128;
 
-    if (def->bandwidth != NULL) {
+    if (def->bandwidth) {
         if (esxVI_HostNetworkPolicy_Alloc(&hostVirtualSwitchSpec->policy) < 0) {
             goto cleanup;
         }
@@ -485,7 +485,7 @@ esxNetworkDefineXML(virConnectPtr conn, const char *xml)
 
         hostPortGroupSpec->vlanId->value = 0;
 
-        if (def->portGroups[i].bandwidth != NULL) {
+        if (def->portGroups[i].bandwidth) {
             if (esxBandwidthToShapingPolicy
                   (def->portGroups[i].bandwidth,
                    &hostPortGroupSpec->policy->shapingPolicy) < 0) {
@@ -550,14 +550,14 @@ esxNetworkUndefine(virNetworkPtr network)
 
     /* Verify that the HostVirtualSwitch is connected to virtual machines only */
     for (hostPortGroupKey = hostVirtualSwitch->portgroup;
-         hostPortGroupKey != NULL; hostPortGroupKey = hostPortGroupKey->_next) {
+         hostPortGroupKey; hostPortGroupKey = hostPortGroupKey->_next) {
         bool found = false;
 
-        for (hostPortGroup = hostPortGroupList; hostPortGroup != NULL;
+        for (hostPortGroup = hostPortGroupList; hostPortGroup;
              hostPortGroup = hostPortGroup->_next) {
             if (STREQ(hostPortGroupKey->value, hostPortGroup->key)) {
                 for (hostPortGroupPort = hostPortGroup->port;
-                     hostPortGroupPort != NULL;
+                     hostPortGroupPort;
                      hostPortGroupPort = hostPortGroupPort->_next) {
                     if (STRNEQ(hostPortGroupPort->type, "virtualMachine")) {
                         virReportError(VIR_ERR_OPERATION_INVALID,
@@ -582,10 +582,10 @@ esxNetworkUndefine(virNetworkPtr network)
 
     /* Remove all HostPortGroups from the HostVirtualSwitch */
     for (hostPortGroupKey = hostVirtualSwitch->portgroup;
-         hostPortGroupKey != NULL; hostPortGroupKey = hostPortGroupKey->_next) {
+         hostPortGroupKey; hostPortGroupKey = hostPortGroupKey->_next) {
         bool found = false;
 
-        for (hostPortGroup = hostPortGroupList; hostPortGroup != NULL;
+        for (hostPortGroup = hostPortGroupList; hostPortGroup;
              hostPortGroup = hostPortGroup->_next) {
             if (STREQ(hostPortGroupKey->value, hostPortGroup->key)) {
                 if (esxVI_RemovePortGroup
@@ -631,12 +631,12 @@ static int
 esxShapingPolicyToBandwidth(esxVI_HostNetworkTrafficShapingPolicy *shapingPolicy,
                             virNetDevBandwidthPtr *bandwidth)
 {
-    if (bandwidth == NULL || *bandwidth != NULL) {
+    if (!bandwidth || *bandwidth) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
         return -1;
     }
 
-    if (shapingPolicy == NULL || shapingPolicy->enabled != esxVI_Boolean_True) {
+    if (!shapingPolicy || shapingPolicy->enabled != esxVI_Boolean_True) {
         return 0;
     }
 
@@ -645,19 +645,19 @@ esxShapingPolicyToBandwidth(esxVI_HostNetworkTrafficShapingPolicy *shapingPolicy
         VIR_ALLOC((*bandwidth)->out) < 0)
         return -1;
 
-    if (shapingPolicy->averageBandwidth != NULL) {
+    if (shapingPolicy->averageBandwidth) {
         /* Scale bits per second to kilobytes per second */
         (*bandwidth)->in->average = shapingPolicy->averageBandwidth->value / 8 / 1000;
         (*bandwidth)->out->average = shapingPolicy->averageBandwidth->value / 8 / 1000;
     }
 
-    if (shapingPolicy->peakBandwidth != NULL) {
+    if (shapingPolicy->peakBandwidth) {
         /* Scale bits per second to kilobytes per second */
         (*bandwidth)->in->peak = shapingPolicy->peakBandwidth->value / 8 / 1000;
         (*bandwidth)->out->peak = shapingPolicy->peakBandwidth->value / 8 / 1000;
     }
 
-    if (shapingPolicy->burstSize != NULL) {
+    if (shapingPolicy->burstSize) {
         /* Scale bytes to kilobytes */
         (*bandwidth)->in->burst = shapingPolicy->burstSize->value / 1024;
         (*bandwidth)->out->burst = shapingPolicy->burstSize->value / 1024;
@@ -713,7 +713,7 @@ esxNetworkGetXMLDesc(virNetworkPtr network_, unsigned int flags)
     count = 0;
 
     for (physicalNicKey = hostVirtualSwitch->pnic;
-         physicalNicKey != NULL; physicalNicKey = physicalNicKey->_next) {
+         physicalNicKey; physicalNicKey = physicalNicKey->_next) {
         ++count;
     }
 
@@ -729,10 +729,10 @@ esxNetworkGetXMLDesc(virNetworkPtr network_, unsigned int flags)
         }
 
         for (physicalNicKey = hostVirtualSwitch->pnic;
-             physicalNicKey != NULL; physicalNicKey = physicalNicKey->_next) {
+             physicalNicKey; physicalNicKey = physicalNicKey->_next) {
             bool found = false;
 
-            for (physicalNic = physicalNicList; physicalNic != NULL;
+            for (physicalNic = physicalNicList; physicalNic;
                  physicalNic = physicalNic->_next) {
                 if (STREQ(physicalNicKey->value, physicalNic->key)) {
                     def->forward.ifs[def->forward.nifs].type
@@ -761,7 +761,7 @@ esxNetworkGetXMLDesc(virNetworkPtr network_, unsigned int flags)
     count = 0;
 
     for (hostPortGroupKey = hostVirtualSwitch->portgroup;
-         hostPortGroupKey != NULL; hostPortGroupKey = hostPortGroupKey->_next) {
+         hostPortGroupKey; hostPortGroupKey = hostPortGroupKey->_next) {
         ++count;
     }
 
@@ -776,7 +776,7 @@ esxNetworkGetXMLDesc(virNetworkPtr network_, unsigned int flags)
             goto cleanup;
         }
 
-        for (network = networkList; network != NULL; network = network->_next) {
+        for (network = networkList; network; network = network->_next) {
             char *tmp = NULL;
 
             if (esxVI_GetStringValue(network, "name", &tmp,
@@ -792,21 +792,21 @@ esxNetworkGetXMLDesc(virNetworkPtr network_, unsigned int flags)
         }
 
         for (hostPortGroupKey = hostVirtualSwitch->portgroup;
-             hostPortGroupKey != NULL; hostPortGroupKey = hostPortGroupKey->_next) {
+             hostPortGroupKey; hostPortGroupKey = hostPortGroupKey->_next) {
             bool found = false;
 
-            for (hostPortGroup = hostPortGroupList; hostPortGroup != NULL;
+            for (hostPortGroup = hostPortGroupList; hostPortGroup;
                  hostPortGroup = hostPortGroup->_next) {
                 if (STREQ(hostPortGroupKey->value, hostPortGroup->key)) {
                     /* Find Network for HostPortGroup, there might be none */
-                    for (networkName = networkNameList; networkName != NULL;
+                    for (networkName = networkNameList; networkName;
                          networkName = networkName->_next) {
                         if (STREQ(networkName->value, hostPortGroup->spec->name)) {
                             if (VIR_STRDUP(def->portGroups[def->nPortGroups].name,
                                            networkName->value) < 0)
                                 goto cleanup;
 
-                            if (hostPortGroup->spec->policy != NULL) {
+                            if (hostPortGroup->spec->policy) {
                                 if (esxShapingPolicyToBandwidth
                                       (hostPortGroup->spec->policy->shapingPolicy,
                                        &def->portGroups[def->nPortGroups].bandwidth) < 0) {
@@ -834,7 +834,7 @@ esxNetworkGetXMLDesc(virNetworkPtr network_, unsigned int flags)
         }
     }
 
-    if (hostVirtualSwitch->spec->policy != NULL) {
+    if (hostVirtualSwitch->spec->policy) {
         if (esxShapingPolicyToBandwidth
               (hostVirtualSwitch->spec->policy->shapingPolicy,
                &def->bandwidth) < 0) {
diff --git a/src/esx/esx_util.c b/src/esx/esx_util.c
index 2716bdf..39b7c0e 100644
--- a/src/esx/esx_util.c
+++ b/src/esx/esx_util.c
@@ -49,7 +49,7 @@ esxUtil_ParseUri(esxUtil_ParsedUri **parsedUri, virURIPtr uri)
     int autoAnswer;
     char *tmp;
 
-    if (parsedUri == NULL || *parsedUri != NULL) {
+    if (!parsedUri || *parsedUri) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
         return -1;
     }
@@ -106,16 +106,16 @@ esxUtil_ParseUri(esxUtil_ParsedUri **parsedUri, virURIPtr uri)
             VIR_FREE((*parsedUri)->proxy_hostname);
             (*parsedUri)->proxy_port = 1080;
 
-            if ((tmp = STRSKIP(queryParam->value, "http://";)) != NULL) {
+            if ((tmp = STRSKIP(queryParam->value, "http://";))) {
                 (*parsedUri)->proxy_type = CURLPROXY_HTTP;
-            } else if ((tmp = STRSKIP(queryParam->value, "socks://")) != NULL ||
-                       (tmp = STRSKIP(queryParam->value, "socks5://")) != NULL) {
+            } else if ((tmp = STRSKIP(queryParam->value, "socks://")) ||
+                       (tmp = STRSKIP(queryParam->value, "socks5://"))) {
                 (*parsedUri)->proxy_type = CURLPROXY_SOCKS5;
-            } else if ((tmp = STRSKIP(queryParam->value, "socks4://")) != NULL) {
+            } else if ((tmp = STRSKIP(queryParam->value, "socks4://"))) {
                 (*parsedUri)->proxy_type = CURLPROXY_SOCKS4;
-            } else if ((tmp = STRSKIP(queryParam->value, "socks4a://")) != NULL) {
+            } else if ((tmp = STRSKIP(queryParam->value, "socks4a://"))) {
                 (*parsedUri)->proxy_type = CURLPROXY_SOCKS4A;
-            } else if ((tmp = strstr(queryParam->value, "://")) != NULL) {
+            } else if ((tmp = strstr(queryParam->value, "://"))) {
                 *tmp = '\0';
 
                 virReportError(VIR_ERR_INVALID_ARG,
@@ -130,7 +130,7 @@ esxUtil_ParseUri(esxUtil_ParsedUri **parsedUri, virURIPtr uri)
             if (VIR_STRDUP((*parsedUri)->proxy_hostname, tmp) < 0)
                 goto cleanup;
 
-            if ((tmp = strchr((*parsedUri)->proxy_hostname, ':')) != NULL) {
+            if ((tmp = strchr((*parsedUri)->proxy_hostname, ':'))) {
                 if (tmp == (*parsedUri)->proxy_hostname) {
                     virReportError(VIR_ERR_INVALID_ARG, "%s",
                                    _("Query parameter 'proxy' doesn't contain a "
@@ -180,7 +180,7 @@ esxUtil_ParseUri(esxUtil_ParsedUri **parsedUri, virURIPtr uri)
 void
 esxUtil_FreeParsedUri(esxUtil_ParsedUri **parsedUri)
 {
-    if (parsedUri == NULL || *parsedUri == NULL) {
+    if (!parsedUri || !(*parsedUri)) {
         return;
     }
 
@@ -228,9 +228,9 @@ esxUtil_ParseDatastorePath(const char *datastorePath, char **datastoreName,
     char *preliminaryDatastoreName = NULL;
     char *preliminaryDirectoryAndFileName = NULL;
 
-    if ((datastoreName != NULL && *datastoreName != NULL) ||
-        (directoryName != NULL && *directoryName != NULL) ||
-        (directoryAndFileName != NULL && *directoryAndFileName != NULL)) {
+    if ((datastoreName && *datastoreName) ||
+        (directoryName && *directoryName) ||
+        (directoryAndFileName && *directoryAndFileName)) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
         return -1;
     }
@@ -240,38 +240,38 @@ esxUtil_ParseDatastorePath(const char *datastorePath, char **datastoreName,
     }
 
     /* Expected format: '[<datastore>] <path>' where <path> is optional */
-    if ((tmp = STRSKIP(copyOfDatastorePath, "[")) == NULL || *tmp == ']' ||
-        (preliminaryDatastoreName = strtok_r(tmp, "]", &saveptr)) == NULL) {
+    if (!(tmp = STRSKIP(copyOfDatastorePath, "[")) || *tmp == ']' ||
+        !(preliminaryDatastoreName = strtok_r(tmp, "]", &saveptr))) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Datastore path '%s' doesn't have expected format "
                          "'[<datastore>] <path>'"), datastorePath);
         goto cleanup;
     }
 
-    if (datastoreName != NULL &&
+    if (datastoreName &&
         VIR_STRDUP(*datastoreName, preliminaryDatastoreName) < 0) {
         goto cleanup;
     }
 
     preliminaryDirectoryAndFileName = strtok_r(NULL, "", &saveptr);
 
-    if (preliminaryDirectoryAndFileName == NULL) {
+    if (!preliminaryDirectoryAndFileName) {
         preliminaryDirectoryAndFileName = (char *)"";
     } else {
         preliminaryDirectoryAndFileName +=
           strspn(preliminaryDirectoryAndFileName, " ");
     }
 
-    if (directoryAndFileName != NULL &&
+    if (directoryAndFileName &&
         VIR_STRDUP(*directoryAndFileName, preliminaryDirectoryAndFileName) < 0) {
         goto cleanup;
     }
 
-    if (directoryName != NULL) {
+    if (directoryName) {
         /* Split <path> into <directory>/<file> and remove /<file> */
         tmp = strrchr(preliminaryDirectoryAndFileName, '/');
 
-        if (tmp != NULL) {
+        if (tmp) {
             *tmp = '\0';
         }
 
@@ -284,15 +284,15 @@ esxUtil_ParseDatastorePath(const char *datastorePath, char **datastoreName,
 
   cleanup:
     if (result < 0) {
-        if (datastoreName != NULL) {
+        if (datastoreName) {
             VIR_FREE(*datastoreName);
         }
 
-        if (directoryName != NULL) {
+        if (directoryName) {
             VIR_FREE(*directoryName);
         }
 
-        if (directoryAndFileName != NULL) {
+        if (directoryAndFileName) {
             VIR_FREE(*directoryAndFileName);
         }
     }
@@ -328,7 +328,7 @@ esxUtil_ResolveHostname(const char *hostname,
         return -1;
     }
 
-    if (result == NULL) {
+    if (!result) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("No IP address for host '%s' found: %s"), hostname,
                        gai_strerror(errcode));
@@ -477,7 +477,7 @@ esxUtil_EscapeDatastoreItem(const char *string)
 
     escaped1 = virVMXEscapeHexPercent(replaced);
 
-    if (escaped1 == NULL) {
+    if (!escaped1) {
         goto cleanup;
     }
 
-- 
1.8.1.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]