Skip to contentSkip to conversation

Avoid segmentation fault for qdev device not found

PATCH In review Glauber Costa 2 participants 2 messages May 12, 2011 – May 13, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Avoid segmentation fault for qdev device not found

qdev_try_create will cope well with a NULL bus, since it will assume the main system bus by default. qdev_create, however, wants to print a message, in which it instantiates the bus name. That simple and at first inoffensive message will generate a segmentation found if the reason for failure is a NULL bus.

I propose we avoid that - thus generating the normal hw_error by always passing a valid bus to qdev_try_create - if none, be it the main system bus.

The code for testing a NULL bus is not remove from qdev_try_create because it is a externally visible function, and we want it to continue working fine.

---
 hw/qdev.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
hw/qdev.c +4 -0
index 1aa1ea0..21ef075 100644
@@ -108,6 +108,10 @@ DeviceState *qdev_create(BusState *bus, const char *name)
 {
     DeviceState *dev;
 
+    if (!bus) {
+        bus = sysbus_get_default();
+    }
+
     dev = qdev_try_create(bus, name);
     if (!dev) {
         hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
Signature · 1 line
1.7.4.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Glauber Costa

qdev_try_create will cope well with a NULL bus, since it will assume the main system bus by default. qdev_create, however, wants to print a message, in which it instantiates the bus name. That simple and at first inoffensive message will generate a segmentation found if the reason for failure is a NULL bus.

I propose we avoid that - thus generating the normal hw_error by always passing a valid bus to qdev_try_create - if none, be it the main system bus.

Quoted 5 lines

The code for testing a NULL bus is not remove from qdev_try_create because it is a externally visible function, and we want it to continue working fine.

"NULL means implicit main system bus" is too clever. See also commit ec990eb6. Beyond the scope of your simple fix, so: