]> xenbits.xensource.com Git - libvirt.git/commitdiff
Use for instead of code duplication when parsing USB port
authorJán Tomko <jtomko@redhat.com>
Thu, 16 Jun 2016 13:23:23 +0000 (15:23 +0200)
committerJán Tomko <jtomko@redhat.com>
Wed, 22 Jun 2016 19:33:57 +0000 (21:33 +0200)
We are done if the string ends and move to another nesting
level if we find a dot.

src/conf/device_conf.h
src/conf/domain_conf.c

index 847564b361e5f6be522dbb229167aabed36c5b64..934afd44456028465c5dd5ee3cb6931f4189606e 100644 (file)
@@ -118,6 +118,8 @@ typedef struct _virDomainDeviceCcidAddress {
     unsigned int slot;
 } virDomainDeviceCcidAddress, *virDomainDeviceCcidAddressPtr;
 
+# define VIR_DOMAIN_DEVICE_USB_MAX_PORT_DEPTH 4
+
 typedef struct _virDomainDeviceUSBAddress {
     unsigned int bus;
     char *port;
index a0feb24b1f1e28acc2eaf39eeb8f9252d5b1af02..a4db372ed2a824f64f5bda703f1b1692ca098557 100644 (file)
@@ -5104,17 +5104,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;