Skip to contentSkip to conversation

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

PATCH Applied Peter Maydell 3 participants 3 messages Aug 3, 2011 – Aug 4, 2011

lore
Share & tools
lore
#0 lore raw

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

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.

---
 hw/qdev.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
hw/qdev.c +6 -1
index b4ea8e1..3fec62a 100644
@@ -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;
Signature · 1 line
1.7.4.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Peter Maydell

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.

#2 lore raw
In reply to Peter Maydell
Quoted 8 lines · Peter Maydell

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.

---
 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