Skip to contentSkip to conversation

usb: fix usb_qdev_init() error handling again

PATCH Applied Stefan Hajnoczi 2 participants 2 messages Dec 15, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: fix usb_qdev_init() error handling again

Commit f462141f18ffdd75847f6459ef83d90b831d12c0 introduced clean up code when usb_qdev_init() fails. Unfortunately it calls .handle_destroy() when .init() was never invoked or failed. This can lead to crashes when .handle_destroy() tries to clean up things that were never initialized.

This patch is careful to undo only those steps that completed along the usb_qdev_init() code path. It's not as pretty as the unified error handling in f462141f18ffdd75847f6459ef83d90b831d12c0 but it's necessary.

---
 hw/usb-bus.c |   12 +++++-------
 1 files changed, 5 insertions(+), 7 deletions(-)
hw/usb-bus.c +5 -7
index 8cafb76..8203390 100644
@@ -77,23 +77,21 @@ static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base)
     QLIST_INIT(&dev->strings);
     rc = usb_claim_port(dev);
     if (rc != 0) {
-        goto err;
+        return rc;
     }
     rc = dev->info->init(dev);
     if (rc != 0) {
-        goto err;
+        usb_release_port(dev);
+        return rc;
     }
     if (dev->auto_attach) {
         rc = usb_device_attach(dev);
         if (rc != 0) {
-            goto err;
+            usb_qdev_exit(qdev);
+            return rc;
         }
     }
     return 0;
-
-err:
-    usb_qdev_exit(qdev);
-    return rc;
 }
 
 static int usb_qdev_exit(DeviceState *qdev)
Signature · 1 line
1.7.7.3
#1 lore raw
Quoted 10 lines · Stefan Hajnoczi

Commit f462141f18ffdd75847f6459ef83d90b831d12c0 introduced clean up code when usb_qdev_init() fails. Unfortunately it calls .handle_destroy() when .init() was never invoked or failed. This can lead to crashes when .handle_destroy() tries to clean up things that were never initialized.

This patch is careful to undo only those steps that completed along the usb_qdev_init() code path. It's not as pretty as the unified error handling in f462141f18ffdd75847f6459ef83d90b831d12c0 but it's necessary.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 37 lines
---
  hw/usb-bus.c |   12 +++++-------
  1 files changed, 5 insertions(+), 7 deletions(-)
hw/usb-bus.c +5 -7
index 8cafb76..8203390 100644
@@ -77,23 +77,21 @@ static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base)
      QLIST_INIT(&dev->strings);
      rc = usb_claim_port(dev);
      if (rc != 0) {
-        goto err;
+        return rc;
      }
      rc = dev->info->init(dev);
      if (rc != 0) {
-        goto err;
+        usb_release_port(dev);
+        return rc;
      }
      if (dev->auto_attach) {
          rc = usb_device_attach(dev);
          if (rc != 0) {
-            goto err;
+            usb_qdev_exit(qdev);
+            return rc;
          }
      }
      return 0;
-
-err:
-    usb_qdev_exit(qdev);
-    return rc;
  }
 
  static int usb_qdev_exit(DeviceState *qdev)