From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42101) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKNmd-0006MF-C1 for qemu-devel@nongnu.org; Mon, 03 Mar 2014 02:59:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WKNmX-0003el-D6 for qemu-devel@nongnu.org; Mon, 03 Mar 2014 02:59:31 -0500 Received: from mx1.redhat.com ([209.132.183.28]:7287) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKNmX-0003eg-40 for qemu-devel@nongnu.org; Mon, 03 Mar 2014 02:59:25 -0500 From: Amos Kong Date: Mon, 3 Mar 2014 15:57:55 +0800 Message-Id: <1393833475-10179-1-git-send-email-akong@redhat.com> Subject: [Qemu-devel] [PATCH v3] qdev: move the code adding the device out of realize List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: hutao@cn.fujitsu.com, armbru@redhat.com, afaerber@suse.de, aliguori@amazon.com, pbonzini@redhat.com Test steps: (qemu) device_add e1000,addr=adsf Property 'e1000.addr' doesn't take value 'adsf' (qemu) info qtree Then qemu crashed. Currently we set a link to the new device for qdev parent bus, but the device hasn't been added to QOM tree. When it fails to set properties, object_unparent() can't cleanup the device. This patch moves the code adding the device output realize, when it fails to set properties, the device can be cleaned successfully. Signed-off-by: Amos Kong --- V2: fix bz by adjust the initialization order (Paolo) V3: fix bug without making it differs with legacy devices creation (Andreas) --- qdev-monitor.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/qdev-monitor.c b/qdev-monitor.c index 6673e3c..9268c87 100644 --- a/qdev-monitor.c +++ b/qdev-monitor.c @@ -522,7 +522,7 @@ DeviceState *qdev_device_add(QemuOpts *opts) return NULL; } - /* create device, set properties */ + /* create device */ dev = DEVICE(object_new(driver)); if (bus) { @@ -533,11 +533,7 @@ DeviceState *qdev_device_add(QemuOpts *opts) if (id) { dev->id = id; } - if (qemu_opt_foreach(opts, set_property, dev, 1) != 0) { - object_unparent(OBJECT(dev)); - object_unref(OBJECT(dev)); - return NULL; - } + if (dev->id) { object_property_add_child(qdev_get_peripheral(), dev->id, OBJECT(dev), NULL); @@ -549,6 +545,13 @@ DeviceState *qdev_device_add(QemuOpts *opts) g_free(name); } + /* set properties */ + if (qemu_opt_foreach(opts, set_property, dev, 1) != 0) { + object_unparent(OBJECT(dev)); + object_unref(OBJECT(dev)); + return NULL; + } + dev->opts = opts; object_property_set_bool(OBJECT(dev), true, "realized", &err); if (err != NULL) { -- 1.8.5.3