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.
This commit is contained in:
Ján Tomko 2016-06-16 15:23:23 +02:00
parent 0956d9b2a3
commit a921699ad9
2 changed files with 13 additions and 8 deletions

View 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;

View 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;
if (*tmp == '\0')
return 0;
error:
if (*tmp == '.')
tmp++;
}
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Cannot parse <address> 'port' attribute"));
return -1;