diff mbox

hw/qdev: Don't crash if qdev_create(NULL, ...) fails

Message ID 1312411744-9292-1-git-send-email-peter.maydell@linaro.org
State New
Headers show

Commit Message

Peter Maydell Aug. 3, 2011, 10:49 p.m. UTC
If an attempt to create a qdev device on the default sysbus (by passing
NULL as the bus to qdev_create) fails, print a useful error message
rather than crashing trying to dereference a NULL pointer.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 hw/qdev.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)

Comments

Markus Armbruster Aug. 4, 2011, 6:55 a.m. UTC | #1
Peter Maydell <peter.maydell@linaro.org> writes:

> If an attempt to create a qdev device on the default sysbus (by passing
> NULL as the bus to qdev_create) fails, print a useful error message
> rather than crashing trying to dereference a NULL pointer.

The special case bus == NULL is ugly, and should be removed.  But
rejecting simple bug fixes because we want some cleanup would be
blackmail, and only hurts users.

Reviewed-by: Markus Armbruster <armbru@redhat.com>
Stefan Hajnoczi Aug. 4, 2011, 9:15 a.m. UTC | #2
On Wed, Aug 03, 2011 at 11:49:04PM +0100, Peter Maydell wrote:
> If an attempt to create a qdev device on the default sysbus (by passing
> NULL as the bus to qdev_create) fails, print a useful error message
> rather than crashing trying to dereference a NULL pointer.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>  hw/qdev.c |    7 ++++++-
>  1 files changed, 6 insertions(+), 1 deletions(-)

Thanks, applied to the trivial patches tree:
http://repo.or.cz/w/qemu/stefanha.git/shortlog/refs/heads/trivial-patches-next

Stefan
diff mbox

Patch

diff --git a/hw/qdev.c b/hw/qdev.c
index b4ea8e1..3fec62a 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -110,7 +110,12 @@  DeviceState *qdev_create(BusState *bus, const char *name)
 
     dev = qdev_try_create(bus, name);
     if (!dev) {
-        hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
+        if (bus) {
+            hw_error("Unknown device '%s' for bus '%s'\n", name,
+                     bus->info->name);
+        } else {
+            hw_error("Unknown device '%s' for default sysbus\n", name);
+        }
     }
 
     return dev;