From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36985) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a9wzi-0004Be-0r for qemu-devel@nongnu.org; Fri, 18 Dec 2015 10:30:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a9wzg-0001kW-Jp for qemu-devel@nongnu.org; Fri, 18 Dec 2015 10:30:57 -0500 Received: from mx1.redhat.com ([209.132.183.28]:58858) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a9wzg-0001kR-FA for qemu-devel@nongnu.org; Fri, 18 Dec 2015 10:30:56 -0500 From: Igor Mammedov Date: Fri, 18 Dec 2015 16:30:47 +0100 Message-Id: <1450452647-118105-3-git-send-email-imammedo@redhat.com> In-Reply-To: <1450452647-118105-1-git-send-email-imammedo@redhat.com> References: <1450452647-118105-1-git-send-email-imammedo@redhat.com> Subject: [Qemu-devel] [PATCH 2/2] qdev: safely fail device_add if unable to allocate device List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, afaerber@suse.de, armbru@redhat.com 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 --- 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