[libvirt] [PATCH 0/3] Fix usb device version parsing in redir filters

https://bugzilla.redhat.com/show_bug.cgi?id=1210650 Ján Tomko (3): Do xml->xml test for usb-redir-filter Fix usb device version parsing issues Rewrite usb device version parsing src/conf/domain_conf.c | 68 +++++++--------------- .../qemuxml2argv-usb-redir-filter-version.args | 19 ++++++ .../qemuxml2argv-usb-redir-filter-version.xml | 46 +++++++++++++++ tests/qemuxml2argvtest.c | 6 ++ .../qemuxml2xmlout-usb-redir-filter-version.xml | 46 +++++++++++++++ .../qemuxml2xmlout-usb-redir-filter.xml | 45 ++++++++++++++ tests/qemuxml2xmltest.c | 2 + 7 files changed, 185 insertions(+), 47 deletions(-) create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.args create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.xml create mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter-version.xml create mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter.xml -- 2.0.5

We don't format the default '-1' fields back. --- .../qemuxml2xmlout-usb-redir-filter.xml | 45 ++++++++++++++++++++++ tests/qemuxml2xmltest.c | 1 + 2 files changed, 46 insertions(+) create mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter.xml diff --git a/tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter.xml b/tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter.xml new file mode 100644 index 0000000..7599955 --- /dev/null +++ b/tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter.xml @@ -0,0 +1,45 @@ +<domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219136</memory> + <currentMemory unit='KiB'>219136</currentMemory> + <vcpu placement='static'>1</vcpu> + <os> + <type arch='i686' machine='pc'>hvm</type> + <boot dev='hd'/> + </os> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>destroy</on_crash> + <devices> + <emulator>/usr/bin/qemu</emulator> + <controller type='usb' index='0' model='ich9-ehci1'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci1'> + <master startport='0'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci2'> + <master startport='2'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci3'> + <master startport='4'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/> + </controller> + <controller type='pci' index='0' model='pci-root'/> + <redirdev bus='usb' type='spicevmc'> + <address type='usb' bus='0' port='4'/> + </redirdev> + <redirdev bus='usb' type='spicevmc'> + <address type='usb' bus='0' port='5'/> + </redirdev> + <redirfilter> + <usbdev class='0x08' vendor='0x15E1' product='0x2007' version='1.10' allow='yes'/> + <usbdev allow='no'/> + </redirfilter> + <memballoon model='virtio'/> + </devices> +</domain> diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c index 817e408..93e8add 100644 --- a/tests/qemuxml2xmltest.c +++ b/tests/qemuxml2xmltest.c @@ -503,6 +503,7 @@ mymain(void) DO_TEST("virtio-lun"); DO_TEST("usb-redir"); + DO_TEST_DIFFERENT("usb-redir-filter"); DO_TEST("blkdeviotune"); DO_TEST_FULL("seclabel-dynamic-baselabel", false, WHEN_INACTIVE); -- 2.0.5

On Fri, Apr 10, 2015 at 16:28:22 +0200, Ján Tomko wrote:
We don't format the default '-1' fields back. --- .../qemuxml2xmlout-usb-redir-filter.xml | 45 ++++++++++++++++++++++ tests/qemuxml2xmltest.c | 1 + 2 files changed, 46 insertions(+) create mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter.xml
ACK. Peter

Request that the number be parsed as decimal, to allow 08 and 09. Format it with the leading zero, 1.01 and 1.10 are two different versions. https://bugzilla.redhat.com/show_bug.cgi?id=1210650 --- src/conf/domain_conf.c | 6 +-- .../qemuxml2argv-usb-redir-filter-version.args | 19 +++++++++ .../qemuxml2argv-usb-redir-filter-version.xml | 46 ++++++++++++++++++++++ tests/qemuxml2argvtest.c | 6 +++ .../qemuxml2xmlout-usb-redir-filter-version.xml | 46 ++++++++++++++++++++++ tests/qemuxml2xmltest.c | 1 + 6 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.args create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.xml create mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter-version.xml diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 1763305..65e2bac 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -11347,8 +11347,8 @@ virDomainRedirFilterUSBVersionHelper(const char *version, *temp = '\0'; temp++; - if ((virStrToLong_ui(version_copy, NULL, 0, &major)) < 0 || - (virStrToLong_ui(temp, NULL, 0, &minor)) < 0) { + if ((virStrToLong_ui(version_copy, NULL, 10, &major)) < 0 || + (virStrToLong_ui(temp, NULL, 10, &minor)) < 0) { virReportError(VIR_ERR_XML_ERROR, _("Cannot parse USB version %s"), version); goto cleanup; @@ -20209,7 +20209,7 @@ virDomainRedirFilterDefFormat(virBufferPtr buf, virBufferAsprintf(buf, " product='0x%04X'", usbdev->product); if (usbdev->version >= 0) - virBufferAsprintf(buf, " version='%d.%d'", + virBufferAsprintf(buf, " version='%d.%02d'", ((usbdev->version & 0xf000) >> 12) * 10 + ((usbdev->version & 0x0f00) >> 8), ((usbdev->version & 0x00f0) >> 4) * 10 + diff --git a/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.args b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.args new file mode 100644 index 0000000..7656ac4 --- /dev/null +++ b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.args @@ -0,0 +1,19 @@ +LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test QEMU_AUDIO_DRV=none \ +/usr/bin/qemu -S \ +-M pc -m 214 -smp 1 -nographic -nodefconfig -nodefaults \ +-chardev socket,id=charmonitor,path=/tmp/test-monitor,server,nowait \ +-mon chardev=charmonitor,id=monitor,mode=readline -no-acpi -boot c \ +-device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x4.0x7 \ +-device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,\ +multifunction=on,addr=0x4 \ +-device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x4.0x1 \ +-device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x4.0x2 \ +-chardev spicevmc,id=charredir0,name=usbredir \ +-device 'usb-redir,chardev=charredir0,id=redir0,\ +filter=0x08:0x15E1:0x2007:0x0109:1|0x08:0x15E1:0x2007:0x0940:1|\ +-1:-1:-1:-1:0,bus=usb.0,port=4' \ +-chardev spicevmc,id=charredir1,name=usbredir \ +-device 'usb-redir,chardev=charredir1,id=redir1,\ +filter=0x08:0x15E1:0x2007:0x0109:1|0x08:0x15E1:0x2007:0x0940:1|\ +-1:-1:-1:-1:0,bus=usb.0,port=5' \ +-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3 diff --git a/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.xml b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.xml new file mode 100644 index 0000000..f1189c9 --- /dev/null +++ b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.xml @@ -0,0 +1,46 @@ +<domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219136</memory> + <currentMemory unit='KiB'>219136</currentMemory> + <vcpu placement='static'>1</vcpu> + <os> + <type arch='i686' machine='pc'>hvm</type> + <boot dev='hd'/> + </os> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>destroy</on_crash> + <devices> + <emulator>/usr/bin/qemu</emulator> + <controller type='usb' index='0' model='ich9-ehci1'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci1'> + <master startport='0'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci2'> + <master startport='2'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci3'> + <master startport='4'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/> + </controller> + <controller type='pci' index='0' model='pci-root'/> + <redirdev bus='usb' type='spicevmc'> + <address type='usb' bus='0' port='4'/> + </redirdev> + <redirdev bus='usb' type='spicevmc'> + <address type='usb' bus='0' port='5'/> + </redirdev> + <redirfilter> + <usbdev class='0x08' vendor='0x15E1' product='0x2007' version='1.09' allow='yes'/> + <usbdev class='0x08' vendor='0x15E1' product='0x2007' version='9.4' allow='yes'/> + <usbdev allow='no'/> + </redirfilter> + <memballoon model='virtio'/> + </devices> +</domain> diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index c02555d..f2694c1 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -1153,6 +1153,12 @@ mymain(void) QEMU_CAPS_ICH9_USB_EHCI1, QEMU_CAPS_USB_REDIR, QEMU_CAPS_SPICE, QEMU_CAPS_CHARDEV_SPICEVMC, QEMU_CAPS_USB_REDIR_FILTER); + DO_TEST("usb-redir-filter-version", + QEMU_CAPS_CHARDEV, QEMU_CAPS_DEVICE, QEMU_CAPS_NODEFCONFIG, + QEMU_CAPS_PCI_MULTIFUNCTION, QEMU_CAPS_USB_HUB, + QEMU_CAPS_ICH9_USB_EHCI1, QEMU_CAPS_USB_REDIR, + QEMU_CAPS_SPICE, QEMU_CAPS_CHARDEV_SPICEVMC, + QEMU_CAPS_USB_REDIR_FILTER); DO_TEST("usb1-usb2", QEMU_CAPS_CHARDEV, QEMU_CAPS_DEVICE, QEMU_CAPS_NODEFCONFIG, QEMU_CAPS_PCI_MULTIFUNCTION, QEMU_CAPS_PIIX3_USB_UHCI, diff --git a/tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter-version.xml b/tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter-version.xml new file mode 100644 index 0000000..cc7cbea --- /dev/null +++ b/tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter-version.xml @@ -0,0 +1,46 @@ +<domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219136</memory> + <currentMemory unit='KiB'>219136</currentMemory> + <vcpu placement='static'>1</vcpu> + <os> + <type arch='i686' machine='pc'>hvm</type> + <boot dev='hd'/> + </os> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>destroy</on_crash> + <devices> + <emulator>/usr/bin/qemu</emulator> + <controller type='usb' index='0' model='ich9-ehci1'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci1'> + <master startport='0'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci2'> + <master startport='2'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci3'> + <master startport='4'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/> + </controller> + <controller type='pci' index='0' model='pci-root'/> + <redirdev bus='usb' type='spicevmc'> + <address type='usb' bus='0' port='4'/> + </redirdev> + <redirdev bus='usb' type='spicevmc'> + <address type='usb' bus='0' port='5'/> + </redirdev> + <redirfilter> + <usbdev class='0x08' vendor='0x15E1' product='0x2007' version='1.09' allow='yes'/> + <usbdev class='0x08' vendor='0x15E1' product='0x2007' version='9.40' allow='yes'/> + <usbdev allow='no'/> + </redirfilter> + <memballoon model='virtio'/> + </devices> +</domain> diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c index 93e8add..bf385af 100644 --- a/tests/qemuxml2xmltest.c +++ b/tests/qemuxml2xmltest.c @@ -504,6 +504,7 @@ mymain(void) DO_TEST("usb-redir"); DO_TEST_DIFFERENT("usb-redir-filter"); + DO_TEST_DIFFERENT("usb-redir-filter-version"); DO_TEST("blkdeviotune"); DO_TEST_FULL("seclabel-dynamic-baselabel", false, WHEN_INACTIVE); -- 2.0.5

On Fri, Apr 10, 2015 at 16:28:23 +0200, Ján Tomko wrote:
Request that the number be parsed as decimal, to allow 08 and 09.
Format it with the leading zero, 1.01 and 1.10 are two different versions.
https://bugzilla.redhat.com/show_bug.cgi?id=1210650 --- src/conf/domain_conf.c | 6 +-- .../qemuxml2argv-usb-redir-filter-version.args | 19 +++++++++ .../qemuxml2argv-usb-redir-filter-version.xml | 46 ++++++++++++++++++++++ tests/qemuxml2argvtest.c | 6 +++ .../qemuxml2xmlout-usb-redir-filter-version.xml | 46 ++++++++++++++++++++++ tests/qemuxml2xmltest.c | 1 + 6 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.args create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.xml create mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-usb-redir-filter-version.xml
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 1763305..65e2bac 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -11347,8 +11347,8 @@ virDomainRedirFilterUSBVersionHelper(const char *version, *temp = '\0'; temp++;
- if ((virStrToLong_ui(version_copy, NULL, 0, &major)) < 0 || - (virStrToLong_ui(temp, NULL, 0, &minor)) < 0) { + if ((virStrToLong_ui(version_copy, NULL, 10, &major)) < 0 || + (virStrToLong_ui(temp, NULL, 10, &minor)) < 0) { virReportError(VIR_ERR_XML_ERROR, _("Cannot parse USB version %s"), version); goto cleanup;
lol, funny bug
@@ -20209,7 +20209,7 @@ virDomainRedirFilterDefFormat(virBufferPtr buf, virBufferAsprintf(buf, " product='0x%04X'", usbdev->product);
if (usbdev->version >= 0) - virBufferAsprintf(buf, " version='%d.%d'", + virBufferAsprintf(buf, " version='%d.%02d'", ((usbdev->version & 0xf000) >> 12) * 10 + ((usbdev->version & 0x0f00) >> 8), ((usbdev->version & 0x00f0) >> 4) * 10 +
This too
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.args b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.args new file mode 100644 index 0000000..7656ac4 --- /dev/null +++ b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.args @@ -0,0 +1,19 @@ +LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test QEMU_AUDIO_DRV=none \ +/usr/bin/qemu -S \ +-M pc -m 214 -smp 1 -nographic -nodefconfig -nodefaults \ +-chardev socket,id=charmonitor,path=/tmp/test-monitor,server,nowait \ +-mon chardev=charmonitor,id=monitor,mode=readline -no-acpi -boot c \ +-device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x4.0x7 \ +-device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,\ +multifunction=on,addr=0x4 \ +-device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x4.0x1 \ +-device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x4.0x2 \ +-chardev spicevmc,id=charredir0,name=usbredir \ +-device 'usb-redir,chardev=charredir0,id=redir0,\ +filter=0x08:0x15E1:0x2007:0x0109:1|0x08:0x15E1:0x2007:0x0940:1|\ +-1:-1:-1:-1:0,bus=usb.0,port=4' \ +-chardev spicevmc,id=charredir1,name=usbredir \ +-device 'usb-redir,chardev=charredir1,id=redir1,\ +filter=0x08:0x15E1:0x2007:0x0109:1|0x08:0x15E1:0x2007:0x0940:1|\ +-1:-1:-1:-1:0,bus=usb.0,port=5' \ +-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3 diff --git a/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.xml b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.xml new file mode 100644 index 0000000..f1189c9 --- /dev/null +++ b/tests/qemuxml2argvdata/qemuxml2argv-usb-redir-filter-version.xml @@ -0,0 +1,46 @@ +<domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219136</memory> + <currentMemory unit='KiB'>219136</currentMemory> + <vcpu placement='static'>1</vcpu> + <os> + <type arch='i686' machine='pc'>hvm</type> + <boot dev='hd'/> + </os> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>destroy</on_crash> + <devices> + <emulator>/usr/bin/qemu</emulator> + <controller type='usb' index='0' model='ich9-ehci1'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci1'> + <master startport='0'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci2'> + <master startport='2'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/> + </controller> + <controller type='usb' index='0' model='ich9-uhci3'> + <master startport='4'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/> + </controller>
Does the test need all the controllers above?
+ <controller type='pci' index='0' model='pci-root'/> + <redirdev bus='usb' type='spicevmc'> + <address type='usb' bus='0' port='4'/> + </redirdev> + <redirdev bus='usb' type='spicevmc'> + <address type='usb' bus='0' port='5'/> + </redirdev> + <redirfilter> + <usbdev class='0x08' vendor='0x15E1' product='0x2007' version='1.09' allow='yes'/> + <usbdev class='0x08' vendor='0x15E1' product='0x2007' version='9.4' allow='yes'/> + <usbdev allow='no'/> + </redirfilter> + <memballoon model='virtio'/> + </devices> +</domain>
ACK, Peter

Simplify the function by leaving out the local copy and checking return values of virStrToLong. --- src/conf/domain_conf.c | 66 +++++++++++++++----------------------------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 65e2bac..bea98a1 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -11306,66 +11306,40 @@ virDomainRedirdevDefParseXML(xmlNodePtr node, } /* - * This is the helper function to convert USB version from a + * This is the helper function to convert USB device version from a * format of JJ.MN to a format of 0xJJMN where JJ is the major * version number, M is the minor version number and N is the * sub minor version number. - * e.g. USB 2.0 is reported as 0x0200, - * USB 1.1 as 0x0110 and USB 1.0 as 0x0100. + * e.g. USB version 2.0 is reported as 0x0200, + * USB version 4.07 as 0x0407 */ static int virDomainRedirFilterUSBVersionHelper(const char *version, virDomainRedirFilterUSBDevDefPtr def) { - char *version_copy = NULL; - char *temp = NULL; - int ret = -1; - size_t len; - size_t fraction_len; - unsigned int major; - unsigned int minor; - unsigned int hex; - - if (VIR_STRDUP(version_copy, version) < 0) - return -1; + unsigned int major, minor; + char *s = NULL; - len = strlen(version_copy); - /* - * The valid format of version is like 01.10, 1.10, 1.1, etc. - */ - if (len > 5 || - !(temp = strchr(version_copy, '.')) || - temp - version_copy < 1 || - temp - version_copy > 2 || - !(fraction_len = strlen(temp + 1)) || - fraction_len > 2) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Incorrect USB version format %s"), version); - goto cleanup; - } + if ((virStrToLong_ui(version, &s, 10, &major)) < 0 || + *s++ != '.' || + (virStrToLong_ui(s, NULL, 10, &minor)) < 0) + goto error; - *temp = '\0'; - temp++; + if (major >= 100 || minor >= 100) + goto error; - if ((virStrToLong_ui(version_copy, NULL, 10, &major)) < 0 || - (virStrToLong_ui(temp, NULL, 10, &minor)) < 0) { - virReportError(VIR_ERR_XML_ERROR, - _("Cannot parse USB version %s"), version); - goto cleanup; - } + if (strlen(s) == 1) + minor *= 10; - hex = (major / 10) << 12 | (major % 10) << 8; - if (fraction_len == 1) - hex |= (minor % 10) << 4; - else - hex |= (minor / 10) << 4 | (minor % 10) << 0; + def->version = (major / 10) << 12 | (major % 10) << 8 | + (minor / 10) << 4 | (minor % 10) << 0; - def->version = hex; - ret = 0; + return 0; - cleanup: - VIR_FREE(version_copy); - return ret; + error: + virReportError(VIR_ERR_XML_ERROR, + _("Cannot parse USB device version %s"), version); + return -1; } static virDomainRedirFilterUSBDevDefPtr -- 2.0.5

On Fri, Apr 10, 2015 at 16:28:24 +0200, Ján Tomko wrote:
Simplify the function by leaving out the local copy and checking return values of virStrToLong. --- src/conf/domain_conf.c | 66 +++++++++++++++----------------------------------- 1 file changed, 20 insertions(+), 46 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 65e2bac..bea98a1 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -11306,66 +11306,40 @@ virDomainRedirdevDefParseXML(xmlNodePtr node, }
/* - * This is the helper function to convert USB version from a + * This is the helper function to convert USB device version from a * format of JJ.MN to a format of 0xJJMN where JJ is the major * version number, M is the minor version number and N is the * sub minor version number. - * e.g. USB 2.0 is reported as 0x0200, - * USB 1.1 as 0x0110 and USB 1.0 as 0x0100. + * e.g. USB version 2.0 is reported as 0x0200, + * USB version 4.07 as 0x0407 */ static int virDomainRedirFilterUSBVersionHelper(const char *version, virDomainRedirFilterUSBDevDefPtr def) { - char *version_copy = NULL; - char *temp = NULL; - int ret = -1; - size_t len; - size_t fraction_len; - unsigned int major; - unsigned int minor; - unsigned int hex; - - if (VIR_STRDUP(version_copy, version) < 0) - return -1; + unsigned int major, minor; + char *s = NULL;
- len = strlen(version_copy); - /* - * The valid format of version is like 01.10, 1.10, 1.1, etc. - */ - if (len > 5 || - !(temp = strchr(version_copy, '.')) || - temp - version_copy < 1 || - temp - version_copy > 2 || - !(fraction_len = strlen(temp + 1)) || - fraction_len > 2) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Incorrect USB version format %s"), version); - goto cleanup; - } + if ((virStrToLong_ui(version, &s, 10, &major)) < 0 || + *s++ != '.' || + (virStrToLong_ui(s, NULL, 10, &minor)) < 0) + goto error;
- *temp = '\0'; - temp++; + if (major >= 100 || minor >= 100) + goto error;
- if ((virStrToLong_ui(version_copy, NULL, 10, &major)) < 0 || - (virStrToLong_ui(temp, NULL, 10, &minor)) < 0) { - virReportError(VIR_ERR_XML_ERROR, - _("Cannot parse USB version %s"), version); - goto cleanup; - } + if (strlen(s) == 1) + minor *= 10;
Humm, do we really want to fix user input in this case? I think that it makes sense but a comment explaining what that part does would be actually helpful.
- hex = (major / 10) << 12 | (major % 10) << 8; - if (fraction_len == 1) - hex |= (minor % 10) << 4; - else - hex |= (minor / 10) << 4 | (minor % 10) << 0; + def->version = (major / 10) << 12 | (major % 10) << 8 | + (minor / 10) << 4 | (minor % 10) << 0;
- def->version = hex; - ret = 0; + return 0;
- cleanup: - VIR_FREE(version_copy); - return ret; + error: + virReportError(VIR_ERR_XML_ERROR, + _("Cannot parse USB device version %s"), version); + return -1; }
ACK with the comment added. It looks much better now. Peter
participants (2)
-
Ján Tomko
-
Peter Krempa