[libvirt] [PATCH 06/17] Use for instead of code duplication when parsing USB port

Ján Tomko jtomko at redhat.com
Fri Jun 17 18:07:08 UTC 2016


We are done if the string ends and move to another nesting
level if we find a dot.
---
 src/conf/device_conf.h |  3 +++
 src/conf/domain_conf.c | 19 +++++++++++--------
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/conf/device_conf.h b/src/conf/device_conf.h
index 847564b..4903839 100644
--- a/src/conf/device_conf.h
+++ b/src/conf/device_conf.h
@@ -118,6 +118,9 @@ typedef struct _virDomainDeviceCcidAddress {
     unsigned int slot;
 } virDomainDeviceCcidAddress, *virDomainDeviceCcidAddressPtr;
 
+/* chosen by fair dice roll */
+# define VIR_DOMAIN_DEVICE_USB_MAX_PORT_DEPTH 4
+
 typedef struct _virDomainDeviceUSBAddress {
     unsigned int bus;
     char *port;
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index bcf832d..dbb7d44 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -5065,17 +5065,20 @@ static int
 virDomainDeviceUSBAddressParsePort(char *port)
 {
     unsigned int p;
-    char *tmp;
+    char *tmp = port;
+    size_t i;
 
-    if ((virStrToLong_uip(port, &tmp, 10, &p) < 0 || (*tmp != '\0' && *tmp != '.')) ||
-        (*tmp == '.' && (virStrToLong_ui(tmp + 1, &tmp, 10, &p) < 0 || (*tmp != '\0' && *tmp != '.'))) ||
-        (*tmp == '.' && (virStrToLong_ui(tmp + 1, &tmp, 10, &p) < 0 || (*tmp != '\0' && *tmp != '.'))) ||
-        (*tmp == '.' && (virStrToLong_ui(tmp + 1, &tmp, 10, &p) < 0 || (*tmp != '\0'))))
-        goto error;
+    for (i = 0; i < VIR_DOMAIN_DEVICE_USB_MAX_PORT_DEPTH; i++) {
+        if (virStrToLong_uip(tmp, &tmp, 10, &p) < 0)
+            break;
 
-    return 0;
+        if (*tmp == '\0')
+            return 0;
+
+        if (*tmp == '.')
+            tmp++;
+    }
 
- error:
     virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("Cannot parse <address> 'port' attribute"));
     return -1;
-- 
2.7.3




More information about the libvir-list mailing list