From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43400) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VD1Hs-0003CB-Rd for qemu-devel@nongnu.org; Fri, 23 Aug 2013 20:01:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VD1Hj-0000mk-E3 for qemu-devel@nongnu.org; Fri, 23 Aug 2013 20:01:04 -0400 Received: from cantor2.suse.de ([195.135.220.15]:36506 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VD1Hj-0000mX-89 for qemu-devel@nongnu.org; Fri, 23 Aug 2013 20:00:55 -0400 From: =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Sat, 24 Aug 2013 02:00:36 +0200 Message-Id: <1377302436-25193-17-git-send-email-afaerber@suse.de> In-Reply-To: <1377302436-25193-1-git-send-email-afaerber@suse.de> References: <1377302436-25193-1-git-send-email-afaerber@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 16/16] qdev-monitor: Avoid aborting on out-of-memory in qdev_device_add() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Andreas=20F=C3=A4rber?= , anthony@codemonkey.ws 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=C3=A4rber --- 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 =3D NULL; =20 @@ -500,7 +501,12 @@ DeviceState *qdev_device_add(QemuOpts *opts) } =20 /* create device, set properties */ - qdev =3D DEVICE(object_new(driver)); + size =3D type_get_instance_size(driver); + qdev =3D g_try_malloc0(size); + if (qdev =3D=3D NULL) { + return NULL; + } + object_initialize(qdev, size, driver); =20 if (bus) { qdev_set_parent_bus(qdev, bus); --=20 1.8.1.4