[Qemu-devel] [PATCH for-4.0] hw/usb/bus.c: Handle "no speed matched" case in usb_mask_to_str()

Peter Maydell posted 1 patch 6 years, 10 months ago
Test docker-mingw@fedora passed
Test docker-clang@ubuntu passed
Test checkpatch passed
Test asan passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20190328133503.6490-1-peter.maydell@linaro.org
Maintainers: Gerd Hoffmann <kraxel@redhat.com>
hw/usb/bus.c | 4 ++++
1 file changed, 4 insertions(+)
[Qemu-devel] [PATCH for-4.0] hw/usb/bus.c: Handle "no speed matched" case in usb_mask_to_str()
Posted by Peter Maydell 6 years, 10 months ago
In usb_mask_to_str() we convert a mask of USB speeds into
a human-readable string (like "full+high") for use in
tracing and error messages. However the conversion code
doesn't do anything to the string buffer if the passed in
speedmask doesn't match any of the recognized speeds,
which means that the tracing and error messages will
end up with random garbage in them. This can happen if
we're doing USB device passthrough.

Handle the "unrecognized speed" case by using the
string "unknown".

Fixes: https://bugs.launchpad.net/qemu/+bug/1603785
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
Tested by temporarily fiddling with the callsites to
pass in a 0 speedmask, since I don't have any USB passthrough
stuff handy.
---
 hw/usb/bus.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/hw/usb/bus.c b/hw/usb/bus.c
index 6fffab7bfa4..9a74dc95601 100644
--- a/hw/usb/bus.c
+++ b/hw/usb/bus.c
@@ -500,6 +500,10 @@ static void usb_mask_to_str(char *dest, size_t size,
                             speeds[i].name);
         }
     }
+
+    if (pos == 0) {
+        snprintf(dest, size, "unknown");
+    }
 }
 
 void usb_check_attach(USBDevice *dev, Error **errp)
-- 
2.20.1


Re: [Qemu-devel] [PATCH for-4.0] hw/usb/bus.c: Handle "no speed matched" case in usb_mask_to_str()
Posted by Philippe Mathieu-Daudé 6 years, 10 months ago
Le jeu. 28 mars 2019 14:36, Peter Maydell <peter.maydell@linaro.org> a
écrit :

> In usb_mask_to_str() we convert a mask of USB speeds into
> a human-readable string (like "full+high") for use in
> tracing and error messages. However the conversion code
> doesn't do anything to the string buffer if the passed in
> speedmask doesn't match any of the recognized speeds,
> which means that the tracing and error messages will
> end up with random garbage in them. This can happen if
> we're doing USB device passthrough.
>
> Handle the "unrecognized speed" case by using the
> string "unknown".
>
> Fixes: https://bugs.launchpad.net/qemu/+bug/1603785
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> Tested by temporarily fiddling with the callsites to
> pass in a 0 speedmask, since I don't have any USB passthrough
> stuff handy.
> ---
>  hw/usb/bus.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/hw/usb/bus.c b/hw/usb/bus.c
> index 6fffab7bfa4..9a74dc95601 100644
> --- a/hw/usb/bus.c
> +++ b/hw/usb/bus.c
> @@ -500,6 +500,10 @@ static void usb_mask_to_str(char *dest, size_t size,
>                              speeds[i].name);
>          }
>      }
> +
> +    if (pos == 0) {
> +        snprintf(dest, size, "unknown");
> +    }
>  }
>
>  void usb_check_attach(USBDevice *dev, Error **errp)
> --
> 2.20.1
>

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

>