usb_mtp_write_metadata() can be reached with s->write_pending set, after
two successive SendObjectInfo commands, or with a NULL parent object.
Those states hit the existing assertions and abort QEMU.

Return RES_INVALID_OBJECTINFO before the assertions.

Cc: [email protected]
Fixes: 53735bef108c ("usb-mtp: Advertise SendObjectInfo for write support")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4529
Suggested-by: Hui Peng <https://gitlab.com/benquike>
Signed-off-by: Tommaso Califano <[email protected]>
---
 hw/usb/dev-mtp.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c
index f6f9bf52ed..d01b38aeac 100644
--- a/hw/usb/dev-mtp.c
+++ b/hw/usb/dev-mtp.c
@@ -1723,7 +1723,7 @@ static void usb_mtp_write_metadata(MTPState *s)
     uint32_t next_handle = s->next_handle;
     size_t filename_chars;
 
-    if (d->length <= offsetof(ObjectInfo, filename)) {
+    if (d->length <= offsetof(ObjectInfo, filename) || s->write_pending || !p) 
{
         usb_mtp_queue_result(s, RES_INVALID_OBJECTINFO, d->trans,
                              0, 0, 0, 0);
         return;
@@ -1738,9 +1738,6 @@ static void usb_mtp_write_metadata(MTPState *s)
      */
     filename_chars /= 2;
 
-    assert(!s->write_pending);
-    assert(p != NULL);
-
     filename = utf16_to_str(MIN(dataset->length, filename_chars),
                             dataset->filename);
 
-- 
2.56.0


Reply via email to