[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 2/2] qdev: safely fail device_add if unable to alloc
From: |
Igor Mammedov |
Subject: |
[Qemu-devel] [PATCH 2/2] qdev: safely fail device_add if unable to allocate device |
Date: |
Fri, 18 Dec 2015 16:30:47 +0100 |
qdev_device_add() currently uses object_new() which
will abort if there memory allocation for device instance
fails. While it's fine it startup, it is not desirable
diring hotplug.
Try to allocate memory for object first and fail safely
if allocation fails.
Signed-off-by: Igor Mammedov <address@hidden>
---
It's just a step in making hotplug safer wrt object allocation.
To make it more safer, hotplugged class constructor
shouldn't allocate memory either, but that should be
addressed on per device basis providing we fix QOM
internals to avoid dynamic allocations.
---
qdev-monitor.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/qdev-monitor.c b/qdev-monitor.c
index a35098f..a70262e 100644
--- a/qdev-monitor.c
+++ b/qdev-monitor.c
@@ -514,6 +514,7 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
DeviceClass *dc;
const char *driver, *path, *id;
DeviceState *dev;
+ size_t obj_size;
BusState *bus = NULL;
Error *err = NULL;
@@ -555,7 +556,13 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
}
/* create device */
- dev = DEVICE(object_new(driver));
+ obj_size = object_class_get_instance_size(OBJECT_CLASS(dc));
+ dev = g_try_malloc0(obj_size);
+ if (dev == NULL) {
+ error_setg(errp, "Not enough memory for Device '%s'", driver);
+ return NULL;
+ }
+ object_initialize(dev, obj_size, driver);
if (bus) {
qdev_set_parent_bus(dev, bus);
--
1.8.3.1