Use g_try_malloc0() and object_initialize() instead of object_new() to
try letting large hot-add attempts fail without killing a running guest.
This requires obtaining the allocation size with type_get_instance_size().

Aborts can still occur whenever devices use object_new() to create child
devices rather than using object_initialize() on embedded structs. When
allocating dynamic properties fails, chances are there's not enough
memory left to emit Errors either.

Signed-off-by: Andreas Färber <afaer...@suse.de>
---
 qdev-monitor.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/qdev-monitor.c b/qdev-monitor.c
index 51bfec0..c5f504f 100644
--- a/qdev-monitor.c
+++ b/qdev-monitor.c
@@ -447,6 +447,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
     ObjectClass *oc;
     DeviceClass *dc;
     const char *driver, *path, *id;
+    size_t size;
     DeviceState *qdev;
     BusState *bus = NULL;
 
@@ -500,7 +501,12 @@ DeviceState *qdev_device_add(QemuOpts *opts)
     }
 
     /* create device, set properties */
-    qdev = DEVICE(object_new(driver));
+    size = type_get_instance_size(driver);
+    qdev = g_try_malloc0(size);
+    if (qdev == NULL) {
+        return NULL;
+    }
+    object_initialize(qdev, size, driver);
 
     if (bus) {
         qdev_set_parent_bus(qdev, bus);
-- 
1.8.1.4


Reply via email to