From: Ján Tomko Date: Thu, 16 Jun 2016 13:23:23 +0000 (+0200) Subject: Use for instead of code duplication when parsing USB port X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=a921699ad97808319990a9a8aa80e528df901405;p=libvirt.git Use for instead of code duplication when parsing USB port We are done if the string ends and move to another nesting level if we find a dot. --- diff --git a/src/conf/device_conf.h b/src/conf/device_conf.h index 847564b361..934afd4445 100644 --- a/src/conf/device_conf.h +++ b/src/conf/device_conf.h @@ -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; diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index a0feb24b1f..a4db372ed2 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -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
'port' attribute")); return -1;